使用 for 循环和 python/matplotlib 在图中添加子图
Add a subplot within a figure using a for loop and python/matplotlib
我认为您不需要所有代码来帮助我,这是 3 个月前的工作,但现在子图没有添加到同一个图中。即我想要一个 2 x 2 的数字,其中为我的 table 中的 4 个国家/地区中的每一个都填充了不同的折线图。我目前得到的是一个带有 4 个空图表和 4 个后续单独图表的数字。为什么现在不能正确地添加到同一个图表?
从那时起我升级了 python 和 pandas 以及我使用的 anaconda(spyder)。我目前使用的是以下版本:
Python 2.7.5 |蟒蛇 1.8.0(64 位)
Matplotlib 1.3.1
Pandas 0.15.2
for i, group in df.groupby('Country'):
chrt += 1
fig1.add_subplot(2,2, chrt)
#fig1.subplots_adjust(hspace=1)
group.plot(x='Month', y='Value (USD)',title= str(i))
我想到了一些事情。确保 fig1
和 chrt
已正确初始化。然后,您可以使用 ax
关键字指定它在哪个轴上绘制。
import matplotlib.pyplot as plt
fig1 = plt.figure()
chrt = 0
for i, group in df.groupby('Country'):
chrt += 1
ax = fig1.add_subplot(2,2, chrt)
group.plot(x='Month', y='Value (USD)',title=str(i), ax=ax)
我认为您不需要所有代码来帮助我,这是 3 个月前的工作,但现在子图没有添加到同一个图中。即我想要一个 2 x 2 的数字,其中为我的 table 中的 4 个国家/地区中的每一个都填充了不同的折线图。我目前得到的是一个带有 4 个空图表和 4 个后续单独图表的数字。为什么现在不能正确地添加到同一个图表?
从那时起我升级了 python 和 pandas 以及我使用的 anaconda(spyder)。我目前使用的是以下版本:
Python 2.7.5 |蟒蛇 1.8.0(64 位) Matplotlib 1.3.1 Pandas 0.15.2
for i, group in df.groupby('Country'):
chrt += 1
fig1.add_subplot(2,2, chrt)
#fig1.subplots_adjust(hspace=1)
group.plot(x='Month', y='Value (USD)',title= str(i))
我想到了一些事情。确保 fig1
和 chrt
已正确初始化。然后,您可以使用 ax
关键字指定它在哪个轴上绘制。
import matplotlib.pyplot as plt
fig1 = plt.figure()
chrt = 0
for i, group in df.groupby('Country'):
chrt += 1
ax = fig1.add_subplot(2,2, chrt)
group.plot(x='Month', y='Value (USD)',title=str(i), ax=ax)