首页  

matplotlib 技巧     所属分类 python 浏览量 54
matplotlib 主要对象 
画布 图形  标题 子图 坐标轴 图例 

Figure (图窗)
Axes (笛卡尔坐标区) axis 的复数 
Axis (坐标轴) x轴  y轴  xAxis yAxis
Subplot (子图)
Plot (线图)

画布 整张图片设置,参数:画布大小,像素密度,背景颜色
plt.figure(figsize=(5,4),dpi=300,facecolor="r")

子图 在一个画布上绘制多个子图
ax1 = plt.subplot(221)#2行2列中的第1个图

# 2行1列 2个子图
fig, (ax1, ax2) = plt.subplots(2, 1)

标题 参数包括字体、粗细、类型、对齐方式、背景、外框等
plt.title('title name',fontsize='large',fontweight='bold')  

坐标轴:包括x轴,y轴,刻度线、标签、范围等

# 设置x轴和y轴的范围
ax.set_xlim(0, 10)
ax.set_ylim(0, 10)
 
# 设置x轴和y轴的标签
ax.set_xlabel('X axis')
ax.set_ylabel('Y axis')
 
# 设置x轴和y轴的刻度
ax.set_xticks([0, 2, 4, 6, 8, 10])
ax.set_yticks([0, 2, 4, 6, 8, 10])

图形 折线图 条形图、柱状图、线条等


两种画图形式,面向对象 和 pyplot

plt.subplots() 返回fig和ax,分别是Figure对象和Axes对象
前者代表画布,后者代表画布上的绘图区域,画布和绘图区域是一对多的关系


plt.plot(x, y, color='r', linewidth=1.5, linestyle='-', label='女生购物欲望')



import matplotlib.pyplot as plt import numpy as np # x = np.array([0,1, 2,3,4,5,6]) # x = np.arange(0,2,0.1) x = np.linspace(0,2,20) # g green plt.plot(x, x,'g-',label='y=x',marker='*') plt.plot(x, x**2,color='r',linestyle='--',label='y=x*x') plt.xlabel("x") plt.ylabel("y") plt.grid(True, linestyle='--', alpha=0.5) plt.title('title') plt.legend() # 将x轴文字旋转45度 plt.xticks(rotation=45) # 添加文字 plt.text(1.0,0.5,'hello') # 添加注释 plt.annotate('hi',xy=(1,1),xytext=(0.5,2.0),arrowprops={'headwidth':9,'facecolor':'r'}) plt.show() import matplotlib.pyplot as plt # 2行1列 2个子图 fig, (ax1, ax2) = plt.subplots(2, 1) ax1.plot(x,x,'r--') ax2.plot(x,x**2,'g-') # 添加阈值线 ax2.axhline(0.5, color='red', linestyle='--') ax2.axhline(3.5, color='red', linestyle='--') plt.plot() # 1行2列 2个子图 fig, (ax1, ax2) = plt.subplots(1, 2) # 红色 点虚线 ax1.plot(x,x,'r:') # 黑色点划线 ax2.plot(x,x**2,'k-.') plt.plot()
plt.函数名() axes.set_方法名() color r红色 g绿色 b蓝色 w白色 c青色 m洋红 y黄色 k黑色 linestyle -实线 --虚线 -.点划线 :点虚线 空字符串 不显示线条 plt.savefig("./test.png") plt.legend() 显示 plt.plot()中设置的label plt.legend(loc="best") Location 'best' 0 'upper right' 1 'upper left' 2 'lower left' 3 'lower right' 4 'right' 5 'center left' 6 'center right' 7 'lower center' 8 'upper center' 9 'center' 10 线条样式-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)
fig = plt.figure() ax = fig.add_subplot(111) ax.set(xlim=[0.5, 4.5], ylim=[-2, 8], title='An Example Axes', ylabel='Y-Axis', xlabel='X-Axis') plt.show() plt.plot() ax.plot() plt.legend() ax.legend() plt.xlabel() ax.set_xlabel() plt.ylabel() ax.set_ylabel() plt.xlim() ax.set_xlim() plt.ylim() ax.set_ylim() plt.title() ax.set_title() ax.set(xlim=(0, 10), ylim=(-2, 2),xlabel='x', ylabel='sin(x)',title='A Simple Plot'); fig = plt.figure() ax3 = fig.add_axes([0.1, 0.1, 0.8, 0.8]) ax4 = fig.add_axes([0.72, 0.72, 0.16, 0.16]) print(type(ax3) ) plt.show() 显示数学公式 fig = plt.figure() plt.xlim([1,8]) plt.ylim([1,5]) plt.text(2,4,r'$ \alpha \beta \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 \rightarrow y} \frac{1}{x^3} $',size=25) plt.text(4,2,r'$ \sqrt[4]{x}=\sqrt{y} $',size=25) plt.show() 调整坐标轴刻度-locator_params locator_params 调整显示颗粒 同时调整 x 轴和 y 轴:plt.locator_params(nbins=20) 只调整 x 轴:plt.locator_params(‘'x',nbins=20) 只调整 y 轴:plt.locator_params(‘'y',nbins=20) 调整坐标轴范围-axis/xlim/ylim axis:[0,5,0,10],x从0到5,y从0到10 xlim:对应参数有xmin和xmax ylim:同xlim用法 plt.xlim(xmin=10,xmax=25) 日期自适应-autofmt_xdate 日期自动调整角度,避免重叠 plt.gcf().autofmt_xdate() 添加双坐标轴-twinx x=np.arange(1,20) y1=x*x y2=np.log(x) plt.plot(x,y1) # 添加一个坐标轴,默认0到1 plt.twinx() plt.plot(x,y2,'r') plt.show() 填充区域-fill/fill_beween x=np.linspace(0,5*np.pi,1000) y1=np.sin(x) y2=np.sin(2*x) plt.plot(x,y1) plt.plot(x,y2) # 填充 plt.fill(x,y1,'g') plt.fill(x,y2,'r') # plt.fill_between(x,y1,y2,where=y1>y2,interpolate=True) plt.title('title') plt.show() 画一个填充好的形状-matplotlib.patche import matplotlib.patches as mptaches xy1=np.array([0.2,0.2]) xy2=np.array([0.2,0.8]) xy3=np.array([0.8,0.2]) xy4=np.array([0.8,0.8]) fig,ax=plt.subplots() #圆形,指定坐标和半径 circle=mptaches.Circle(xy1,0.15) ax.add_patch(circle) #长方形 rect=mptaches.Rectangle(xy2,0.2,0.1,color='r') ax.add_patch(rect) #多边形 polygon=mptaches.RegularPolygon(xy3,6,0.1,color='g') ax.add_patch(polygon) # 椭圆 ellipse=mptaches.Ellipse(xy4,0.4,0.2,color='c') ax.add_patch(ellipse) ax.axis('equal') plt.show()
https://matplotlib.org/stable/users/explain/quick_start.html

上一篇     下一篇
Python统计学极简入门笔记 02 描述性统计

Python统计学极简入门笔记 03 数据分布

jupyter 使用 技巧

matplot 基本用法

pandas matplot 读取日线数据,滚动计算bias250 添加阈值线

pandas matplot 画图 超出阈值 变颜色