python - 如何使用变量中存储的图形作为子图

标签 python matplotlib figure

假设一组 4 个 matplotlib 图形存储在变量 fig1fig4 中。

import matplotlib.pyplot as plt

fig1 = plt.figure()
plt.plot([1, 2, 3, 4])

fig2 = plt.figure()
plt.plot([2, 2, 2, 2])

fig3 = plt.figure()
plt.plot([1, 3, 1, 4])

fig4 = plt.figure()
plt.plot([4, 3, 2, 1])

# code to create subplots using the fig1 to fig4 variables
# i.e. without recreating the original 4 plots

使用 matplotlib(或者可能是另一个包),如何创建子图图形(例如下面的示例),子图为 fig1fig4

enter image description here

最佳答案

import matplotlib.pyplot as plt

fig = plt.figure(figsize=(10,10))
#Set First Axis
ax1 = fig.add_subplot(121)
ax1.plot([1, 3, 1, 4])


#Set Second Axis
ax2 = fig.add_subplot(122)
ax2.plot([2, 2, 2, 2])

ax3 = fig.add_subplot(221)
ax3.plot([1, 3, 1, 4])

ax4 = fig.add_subplot(222)
ax4.plot([4, 3, 2, 1])

enter image description here

关于python - 如何使用变量中存储的图形作为子图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55623612/

相关文章:

python - Matplotlib 多值条形图定制

python - 尝试使用 uWSGI 部署 python Pyramid 应用程序

matplotlib 使用 twinx 和 twiny 一起使用(如 twinxy)

python - 如何在带有误差条的条形图中绘制 Pandas 数据框的特定行和列(基于行名和列名)?

python - plt.savefig() : ValueError: All values in the dash list must be positive

matlab - 设置大于屏幕的 matlab 图

python - 循环浏览文件夹以将多个 Excel 工作表合并为一列

python - 属性错误 : 'module' object has no attribute 'setdefaultencoding'

Python TA-lib安装错误,如何解决?

html - 在 Twitter Bootstrap 的 Carousel 模板中将字幕居中对齐并置于特写图像下方