1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > 【matplotlib】21.多图合并【python3 numpy pandas matplotlib完结】

【matplotlib】21.多图合并【python3 numpy pandas matplotlib完结】

时间:2022-06-26 14:13:17

相关推荐

【matplotlib】21.多图合并【python3 numpy pandas matplotlib完结】

#【matplotlib】21.多图合并

.1.20

python3、numpy、pandas、matplotlib完结

新年快乐@!!

21.1 多合一显示

21.1.1 均匀分布

方法很简单 就是一张图 分几块 第一张占几块 从哪开始;第二张…

plt.subplot()

# 打开一个窗口import matplotlib.pyplot as pltplt.figure()

<Figure size 432x288 with 0 Axes>

#表示将整个图像窗口分为2行2列, 当前位置为1.plt.subplot(2,2,1)#上面这个未知的图像plt.plot([0,1],[0,1])#图2plt.subplot(2,2,2)plt.plot([0,1],[0,2])#图3 plt.subplot(223)=plt.subplot(2,2,3)plt.subplot(223)plt.plot([0,1],[0,3])#图4plt.subplot(224)plt.plot([0,1],[0,4])plt.show() # 展示

21.1.2 不均匀分布

plt.figure()# 图像窗口分为2行1列, 当前位置为1.plt.subplot(2,1,1)plt.plot([0,1],[0,1])#图像窗口分为2行3列, 当前位置为4plt.subplot(2,3,4)plt.plot([0,1],[0,2])# 图像窗口分为2行3列, 当前位置为5,6plt.subplot(235)plt.plot([0,1],[0,3])plt.subplot(236)plt.plot([0,1],[0,4])plt.show() # 展示

21.2 分隔显示

plt.subplot2grid()

也是图分几块,从哪个开始,夸几格

import matplotlib.pyplot as pltplt.figure()# 3*3的图,(0,0)开始画 跨3列ax1 = plt.subplot2grid((3, 3), (0, 0), colspan=3)ax1.plot([1, 2], [1, 2]) # 画小图ax1.set_title('ax1_title') # 设置小图的标题ax2 = plt.subplot2grid((3, 3), (1, 0), colspan=2)ax3 = plt.subplot2grid((3, 3), (1, 2), rowspan=2)ax4 = plt.subplot2grid((3, 3), (2, 0))ax5 = plt.subplot2grid((3, 3), (2, 1))# 上面画好格子了 我们在其中ax4里面画好相应的图ax4.scatter([1, 2], [2, 2])ax4.set_xlabel('ax4_x')ax4.set_ylabel('ax4_y')

Text(0, 0.5, 'ax4_y')

这里面要注意的就是:colspan,rowspan,方向别弄错

colspan:是跨列,所以是横着走了rowspan`:是跨行,是纵着走

2. 方法2:gridspec这个是用切片方法,说每个图位置

import matplotlib.pyplot as pltimport matplotlib.gridspec as gridspecplt.figure()# 3*3 的图gs = gridspec.GridSpec(3, 3)# gs[0, :]表示这个图占第0行和所有列ax6 = plt.subplot(gs[0, :])# 其他类似ax7 = plt.subplot(gs[1, :2])ax8 = plt.subplot(gs[1:, 2])ax9 = plt.subplot(gs[-1, 0])ax10 = plt.subplot(gs[-1, -2])# 后面可以添加每个图的描述,例如ax10ax10.plot([1, 2], [3, 2])

[<matplotlib.lines.Line2D at 0x1d9908c6c40>]

21.3 共享坐标轴

# 2行2列 ((ax11, ax12), (ax13, ax14))表示第1行从左至右 从上往下f, ((ax11, ax12), (ax13, ax14)) = plt.subplots(2, 2, sharex=True, sharey=True)# 创建图形 ax12,13,14ax11.scatter([1,2], [1,2])ax12.scatter([2,3], [3,4])# plt.tight_layout()表示紧凑显示图像plt.tight_layout()plt.show()

21.4 次坐标轴

也就是一个图两个y轴

第一个坐标轴

import matplotlib.pyplot as pltimport numpy as npx = np.arange(0, 10, 0.1)y1 = 0.05 * x**2y2 = -1 * y1# figure默认的坐标系 ax1fig, ax1 = plt.subplots()

第二个坐标轴

import matplotlib.pyplot as pltimport numpy as npx = np.arange(0, 10, 0.1)y1 = 0.05 * x**2y2 = 1* x+5# figure默认的坐标系 ax1fig, ax1 = plt.subplots()# 对ax1调用twinx()方法,生成如同镜面效果后的ax2,放到了右侧ax2 = ax1.twinx()ax1.plot(x, y1, 'g-') # 第一个轴产生图像green, solid lineax1.set_xlabel('X data') # 设置x轴名字ax1.set_ylabel('Y1 data', color='g')# 设置y1轴名字# 第二个 图像和y2名字颜色ax2.plot(x, y2, 'b-') # blueax2.set_ylabel('Y2 data', color='b')plt.show()

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。