如何使用Python的plt库在一张画布上通过subplot同时绘制多张图表?

2026-04-13 21:111阅读0评论SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计488个文字,预计阅读时间需要2分钟。

如何使用Python的plt库在一张画布上通过subplot同时绘制多张图表?

pythonsubplot函数用于在同一垂直方向或水平方向上绘制多张图。参数说明如下:- arg1:在垂直方向上同时绘制几幅图。- arg2:在水平方向上同时绘制几幅图。- arg3:当前命令修改的是第几幅图。plt.figure()之后开始新的一张图。

subplot(arg1, arg2, arg3)

arg1: 在垂直方向同时画几张图

arg2: 在水平方向同时画几张图

arg3: 当前命令修改的是第几张图

plt.figure()另起一张新的画布 from PIL import Image import matplotlib.pyplot as plt image1 = Image.open('1.jpg') image2 = Image.open('2.jpg') plt.subplot(121) plt.imshow(image1) plt.subplot(122) plt.imshow(image2) plt.show()

补充:matplotlib 同一个画布绘制多张图,主次刻度,竖线

如何使用Python的plt库在一张画布上通过subplot同时绘制多张图表?

我就废话不多说了,大家还是直接看代码吧~

import matplotlib.pyplot as plt import seaborn as sns sns.set() # 要分析的数据 profit = df_profit.groupby('release_year')['profit'].agg(['mean','sum','count']) # 在同一个画布中绘制两张图 plt.figure(figsize=(15,15)) # 图一:每年上映电影的总收入 ax = plt.subplot(211) # 设置x轴 范围 ax.set_xlim(1958,2018) # 设置x轴 主刻度,(次刻度设置minor=True) ax.set_xticks(np.arange(1960,2018,5), minor=False) # 画图 ax.plot(profit['sum'], linestyle='--', marker='o', markersize=5) ax.set_title('The Sum of Movies\' Revenue v.s. Release Year') ax.set_ylabel('Revenue(USD)') # 增加竖线 ax.axvline(x=1977, color='#d46061', linewidth=1); # 图二:每年上映电影的平均收入 ax = plt.subplot(212) # 设置x轴 范围 ax.set_xlim(1958,2018) # 设置x轴 主刻度 ax.set_xticks(np.arange(1960,2018,5)) # 画图 ax.plot(profit['mean'], linestyle='--', marker='o', markersize=5); ax.set_title('The Mean of Movies\' Revenue v.s. Release Year') ax.set_xlabel('Release Year') ax.set_ylabel('Revenue(USD)') # 增加竖线 ax.axvline(x=1977, color='#d46061', linewidth=1);

以上为个人经验,希望能给大家一个参考,也希望大家多多支持易盾网络。如有错误或未考虑完全的地方,望不吝赐教。

本文共计488个文字,预计阅读时间需要2分钟。

如何使用Python的plt库在一张画布上通过subplot同时绘制多张图表?

pythonsubplot函数用于在同一垂直方向或水平方向上绘制多张图。参数说明如下:- arg1:在垂直方向上同时绘制几幅图。- arg2:在水平方向上同时绘制几幅图。- arg3:当前命令修改的是第几幅图。plt.figure()之后开始新的一张图。

subplot(arg1, arg2, arg3)

arg1: 在垂直方向同时画几张图

arg2: 在水平方向同时画几张图

arg3: 当前命令修改的是第几张图

plt.figure()另起一张新的画布 from PIL import Image import matplotlib.pyplot as plt image1 = Image.open('1.jpg') image2 = Image.open('2.jpg') plt.subplot(121) plt.imshow(image1) plt.subplot(122) plt.imshow(image2) plt.show()

补充:matplotlib 同一个画布绘制多张图,主次刻度,竖线

如何使用Python的plt库在一张画布上通过subplot同时绘制多张图表?

我就废话不多说了,大家还是直接看代码吧~

import matplotlib.pyplot as plt import seaborn as sns sns.set() # 要分析的数据 profit = df_profit.groupby('release_year')['profit'].agg(['mean','sum','count']) # 在同一个画布中绘制两张图 plt.figure(figsize=(15,15)) # 图一:每年上映电影的总收入 ax = plt.subplot(211) # 设置x轴 范围 ax.set_xlim(1958,2018) # 设置x轴 主刻度,(次刻度设置minor=True) ax.set_xticks(np.arange(1960,2018,5), minor=False) # 画图 ax.plot(profit['sum'], linestyle='--', marker='o', markersize=5) ax.set_title('The Sum of Movies\' Revenue v.s. Release Year') ax.set_ylabel('Revenue(USD)') # 增加竖线 ax.axvline(x=1977, color='#d46061', linewidth=1); # 图二:每年上映电影的平均收入 ax = plt.subplot(212) # 设置x轴 范围 ax.set_xlim(1958,2018) # 设置x轴 主刻度 ax.set_xticks(np.arange(1960,2018,5)) # 画图 ax.plot(profit['mean'], linestyle='--', marker='o', markersize=5); ax.set_title('The Mean of Movies\' Revenue v.s. Release Year') ax.set_xlabel('Release Year') ax.set_ylabel('Revenue(USD)') # 增加竖线 ax.axvline(x=1977, color='#d46061', linewidth=1);

以上为个人经验,希望能给大家一个参考,也希望大家多多支持易盾网络。如有错误或未考虑完全的地方,望不吝赐教。