matplotlib 常用小技巧

https://mp.weixin.qq.com/s/Ux66-omtEU6EWEWhjQnnyw

添加标题

plt.title('示例标题')

添加图上面的文字

x y 坐标,文本内容。


plt.text(1,2,'function y=x*x')

注释

xy :备注的坐标点

xytext :备注文字的坐标(默认为xy的位置)

arrowprops :在 xy 和 xytext 之间绘制一个箭头。


plt.annotate('示例注释',xy=(0,1),xytext=(1,2),arrowprops={'headwidth':10,'facecolor':'r'})

设置标题

plt.xlabel('示例x轴')

plt.ylabel('示例y轴')

添加图例

plt.legend(['1','2'])

color 可以为缩写 以及 RGB 十六进制数

plt.plot(x1,color='g')

plt.plot(x2,color='#FF00FF')

该表线条样式

marker = 'o' , '>' , 's'


plt.plot(x,marker='o')

plt.plot(x+1,marker='>')

plt.plot(x+2,marker='s')

marker 支持的类型:

‘.’:点(point marker)

‘,’:像素点(pixel marker)

‘o’:圆形(circle marker)

‘v’:朝下三角形(triangle_down marker)

‘^’:朝上三角形(triangle_up marker)

‘<‘:朝左三角形(triangle_left marker)

‘>’:朝右三角形(triangle_right marker)

‘1’:(tri_down marker)

‘2’:(tri_up marker)

‘3’:(tri_left marker)

‘4’:(tri_right marker)

‘s’:正方形(square marker)

‘p’:五边星(pentagon marker)

‘*’:星型(star marker)

‘h’:1号六角形(hexagon1 marker)

‘H’:2号六角形(hexagon2 marker)

‘+’:+号标记(plus marker)

‘x’:x号标记(x marker)

‘D’:菱形(diamond marker)

‘d’:小型菱形(thin_diamond marker)

‘|’:垂直线形(vline marker)

‘_’:水平线形(hline marker)

显示数学公式
使用 $ 内容 $


plt.text(2,4,r'$ alpha eta pi lambda omega $',size=25)
plt.text(4,4,r'$ sin(0)=cos(frac{pi}{2}) $',size=25)
plt.text(2,2,r'$ lim_{x 
ightarrow y} frac{1}{x^3} $',size=25)
plt.text(4,2,r'$ sqrt[4]{x}=sqrt{y} $',size=25)

网格线
plt.grid()


plt.grid(color='g',linewidth='1',linestyle='-.')

坐标轴刻度

x 和 y 轴
plt.locator_params(nbins=30)



x 轴

plt.locator_params("x",nbins=30)


 y 轴

plt.locator_params("y",nbins=20)

当 x 轴为日期时,进行日期自适应

x=pd.date_range('2020/01/01',periods=30)

plt.gcf().autofmt_xdate()

如果觉得文章不错,可以分享给其他人哟~
原文地址:https://www.cnblogs.com/hany-postq473111315/p/15204600.html