gpt4 book ai didi

python - 如何将图聚合成带有子图的图形

转载 作者:行者123 更新时间:2023-11-28 21:46:44 26 4
gpt4 key购买 nike

我已经阅读了与此相关的答案并遵循了以下内容:

fig = plt.figure()
fig.add_subplot(221) #top left
fig.add_subplot(222) #top right
fig.add_subplot(223) #bottom left
fig.add_subplot(224) #bottom right
plt.show()

太棒了,现在我有一个包含四个空子图的图形。

我想不通的是如何设置 #top left 子图来显示我的图 x 其中 x 是一个 matplotlib.axes.AxesSubplot 我已经生成了。

我的代码总共是:

plt1 = plot.scatter(foo,bar)
plt2 = plot.line(bar, baz)
plt3 = plot.scatter(you, get)
plt4 = plot.line(the, picture)

fig = plt.figure()
fig.add_subplot(221) #top left
fig.add_subplot(222) #top right
fig.add_subplot(223) #bottom left
fig.add_subplot(224) #bottom right

# Here is where I got lost
# I want figure subplot 221 to show plt1 etc...

最佳答案

您需要跟踪 add_subplot 返回的 Axes 对象。

fig = plt.figure()
ax1 = fig.add_subplot(221) #top left
ax2 = fig.add_subplot(222) #top right
ax3 = fig.add_subplot(223) #bottom left
ax4 = fig.add_subplot(224) #bottom right

然后你可以做:

ax1.plot(...)
ax2.boxplot(...)
ax3.hist(...)
# etc

但我会这样做:

fig, (ax1, ax2, ax3, ax4) = plt.subplots(nrows=2, ncols=2)

然后从那里开始。

关于python - 如何将图聚合成带有子图的图形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37472398/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com