Python:(使用matplotlib画图)在坐标图中进行注释:如添加箭头,添加文本等

先上个图Python:(使用matplotlib画图)在坐标图中进行注释:如添加箭头,添加文本等_第1张图片

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(-5,5,100)
y1 = 2 * x + 1
# y2 = x**2

plt.figure(num=3,figsize=(9,6))

# line_1 = plt.plot(x,y2,label='up')
line_2 = plt.plot(x,y1,color='red',linewidth=1,linestyle='--',label='down')

# plt.xlim((-1,2))
# plt.ylim((-1,2))
plt.xlabel(r'$\alpha$')
plt.ylabel(r'$\beta$')

# new_ticks = np.linspace(-1,2,10)
# print(new_ticks)
# plt.xticks(new_ticks)
# plt.yticks([-2,-1.8,-1,1.22,3],[r'$really\ bad$',r'$bad$',r'$normal$',r'$good$',r'$really good$'])

ax = plt.gca()
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')

ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')

ax.spines['bottom'].set_position(('data',0))
ax.spines['left'].set_position(('data',0))

x0 = 1
y0 = 2* x0 + 1
plt.scatter(x0,y0,s=50,color='b')
plt.plot([x0,x0],[y0,0],'k--',lw=1.5)

#method1
##############
plt.annotate(r'$2x+1=3$', xy=(x0,y0),xycoords='data',xytext=(+30,-30),textcoords='offset points',
             fontsize=16,arrowprops=dict(arrowstyle='->',connectionstyle='arc3,rad=.2'))

#method2
plt.text(-4,3,r'$This\ is\ the\ some\ text.\ \mu_j\ \sigma_i\ \alpha_t$')


plt.legend()
plt.show()

注:内容仅用于学习参考

你可能感兴趣的:(Python学习)