Matplotlib数据可视化——设置标注annotation
今天主要记录annotation标注的用法
import matplotlib.pyplot as plt
import numpy as np
plt.rcParams['font.sans-serif']=['SimHei'] #用来正常显示中文标签
plt.rcParams['axes.unicode_minus']=False #用来正常显示负号
x = np.linspace(-3,3,50)
y = 2*x+1
#绘制在同一个figure中
plt.figure(num=1, figsize=(8, 5),)
l1, = plt.plot(x, y,)
# 将上和右的梁删除
ax = plt.gca()
ax.spines['right'].set_color('None')
ax.spines['top'].set_color('None')
# 设置x轴的梁和原点
ax.xaxis.set_ticks_position('bottom')
ax.spines['bottom'].set_position(('data', 0))
# 设置y轴的梁和原点
ax.yaxis.set_ticks_position('left')
ax.spines['left'].set_position(('data', 0))
x0 = 1
y0 = 2*x0+1
plt.scatter(x0, y0, s=100, color='r')# scatter方法显示散点图或某单一点
plt.plot([x0, x0], [y0, 0], 'k--', lw=2.5)
# method1
###################
plt.annotate(r'2x+1=3', xy=(x0, y0),xycoords='data', xytext=(+30, -30), textcoords='offset points',
fontsize=24, arrowprops=dict(arrowstyle='->', connectionstyle='arc3, rad=.2'))
# method2
###################
plt.text(-3.2, 3, r'$This\ is\ new\ world.\ \mu\ \sigma_1\ \alpha_t$',
fontdict={'size': 16, 'color': 'r'})
plt.show()