如何使用存储在变量中的数字作为matplotlib中的子图?

问题描述 投票:0回答:1

假设存储在变量fig1至fig4中的一组4个matplotlib数字。

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(或者可能是另一个包),如何创建子图的图形(例如下面的示例),子图是fig1到fig4?

enter image description here

python matplotlib figure
1个回答
0
投票
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

© www.soinside.com 2019 - 2024. All rights reserved.