Matplotlib 子地块堆叠条形图

Matplotlib 子地块堆叠条形图,matplotlib,ipython,bar-chart,subplot,stacked-chart,Matplotlib,Ipython,Bar Chart,Subplot,Stacked Chart,我有三个堆叠的条形图,我已经正确地显示了它们,但是我想把它们并排显示(与三个不同的图形(一个在另一个下面)相反) 分别显示三个图形的当前代码是(其中axX_组是包含我的数据的数据帧) 我知道我必须使用subplot函数在一个图形上绘制图形,但似乎无法正确分配当前图形 我的主要问题是,我在网上遇到的所有示例都使用子图函数来指定图形的位置(我理解),但随后将图形绘制为X和Y的函数-如下所示: figure = plt.figure() ax1 = figure.add_subplot(311)

我有三个堆叠的条形图,我已经正确地显示了它们,但是我想把它们并排显示(与三个不同的图形(一个在另一个下面)相反)

分别显示三个图形的当前代码是(其中axX_组是包含我的数据的数据帧)

我知道我必须使用subplot函数在一个图形上绘制图形,但似乎无法正确分配当前图形

我的主要问题是,我在网上遇到的所有示例都使用子图函数来指定图形的位置(我理解),但随后将图形绘制为X和Y的函数-如下所示:

 figure = plt.figure()
 ax1 = figure.add_subplot(311)
 ax2 = figure.add_subplot(312)
 ax3 = figure.add_subplot(313)

 ax1.plot(x1,y1)
 ax2.plot(x2,y2)
 ax3.plot(x3,y3)
我需要弄清楚如何使用ax1.plot(X,Y)部分,以便调用.plot(堆叠条形图)

所以我已经做到了,但它不起作用:

fig = plt.figure()
ax1_Group = fig.add_subplot(131)
ax2_Group = fig.add_subplot(132)
ax3_Group = fig.add_subplot(133)
ax1_Group.plot(ax1_Group.plot(kind='barh', stacked=True, figsize=(6,15), title='Breakdown of Makes and Years (Pre 2000)'))
ax2_Group.plot(ax2_Group.plot(kind='barh', stacked=True, figsize=(6,20), title='Breakdown of Makes and Years (2000-2009)'))
ax3_Group.plot(ax3_Group.plot(kind='barh', stacked=True, figsize=(6,20), title='Breakdown of Makes and Years (2010+)'))

我试过很多东西,但都不太明白。任何帮助都将不胜感激

我想你的问题的答案基本上是这样的

ax = ax1_Group.plot(kind='barh', 
                    stacked=True, 
                    figsize=(6,15), 
                    title='Makes and Years (Pre 2000)')
ax2_Group.plot(ax=ax, 
               kind='barh', 
               stacked=True, 
               title='Makes and Years (2000-2009)')
ax3_Group.plot(ax=ax, 
               kind='barh', 
               stacked=True, 
               title='Makes and Years (2010+)')

您可以使用以下代码:

plt.figure()
ax1 = plt.subplot(131)
ax1_Group.plot(ax1_Group.plot(ax = ax1, kind='barh', stacked=True, figsize=(6,15), title='Breakdown of Makes and Years (Pre 2000)'))

ax2 = plt.subplot(132)
ax2_Group.plot(ax2_Group.plot(ax = ax2, kind='barh', stacked=True, figsize=(6,20), title='Breakdown of Makes and Years (2000-2009)'))

ax3 = plt.subplot(133)
ax3_Group.plot(ax3_Group.plot(ax = ax3, kind='barh', stacked=True, figsize=(6,20), title='Breakdown of Makes and Years (2010+)'))

添加关键字
subplots=True
是否符合您的要求?如果没有,你有没有可能包括一个你想要的草图?另外,正如您所说的dataframe,我假设您使用的是Pandas?值得添加为标签。最糟糕的情况是,您可以使用类似于
x1,y1=ax1\u Group.values的内容提取数据,并按照第二个示例进行绘图。请为OP和未来的读者解释以下代码。
ax = ax1_Group.plot(kind='barh', 
                    stacked=True, 
                    figsize=(6,15), 
                    title='Makes and Years (Pre 2000)')
ax2_Group.plot(ax=ax, 
               kind='barh', 
               stacked=True, 
               title='Makes and Years (2000-2009)')
ax3_Group.plot(ax=ax, 
               kind='barh', 
               stacked=True, 
               title='Makes and Years (2010+)')
plt.figure()
ax1 = plt.subplot(131)
ax1_Group.plot(ax1_Group.plot(ax = ax1, kind='barh', stacked=True, figsize=(6,15), title='Breakdown of Makes and Years (Pre 2000)'))

ax2 = plt.subplot(132)
ax2_Group.plot(ax2_Group.plot(ax = ax2, kind='barh', stacked=True, figsize=(6,20), title='Breakdown of Makes and Years (2000-2009)'))

ax3 = plt.subplot(133)
ax3_Group.plot(ax3_Group.plot(ax = ax3, kind='barh', stacked=True, figsize=(6,20), title='Breakdown of Makes and Years (2010+)'))