Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用matplotlib子批次按输出打印数据_Python_Pandas_Matplotlib_Plot_Group By - Fatal编程技术网

Python 使用matplotlib子批次按输出打印数据

Python 使用matplotlib子批次按输出打印数据,python,pandas,matplotlib,plot,group-by,Python,Pandas,Matplotlib,Plot,Group By,我有一个数据帧,df2,它有6行和1591列 0.0.0 10.1.21 1.5.12 3.7.8 3.5.8 1.7.8 ... June 1 1 4 0 0 4 July 0 0 0 0 0 0 August 54 0 9 0 5

我有一个数据帧,df2,它有6行和1591列

           0.0.0   10.1.21  1.5.12   3.7.8  3.5.8  1.7.8 ...        
 June       1        1         4      0       0     4
 July       0        0         0      0       0     0
 August     54       0         9      0       5     0
 September  22       0         6      0       0     1
 October    0        9         5      1       4     0
我想将图形中每个面板中3列的多个绘制为堆叠条。即在单独的面板中绘制列:0.0.0至1.5.12,在另一个面板中绘制列:3.7.8至1.7.8。代码如下:

df= df2
df['key1'] = 0
df.key1.loc[:, ['0.0.0', '10.1.21', '1.5.12']].values = 1  
df.key1.loc[:,['3.7.8', '3.5.8', '1.7.8']].values = 2
df.key1.loc[:,['4.4.3', '2.2.0', '2.8.0']].values = 3

# Plot in Three Panels
distinct_keys = df['key1'].unique()
fig, axes = pyplot.subplots(len(distinct_keys), 1, sharex=True, figsize=  (3,5)) 

#{df_subset groups the rows with the same key in other to plot them in the same panel}

for i, key in enumerate(distinct_keys):
df_subset =df[df['key1']==key]

 # plot
axes[i] = df_subset.plot(kind='bar', stacked=True)
pyplot.legend(bbox_to_anchor=(1.04,1), loc="upper right")
pyplot.subplots_adjust(right=0.7)
pyplot.tight_layout(rect=[0,0,0.75,1])
pyplot.savefig("output.png", bbox_inches="tight")

但是我得到:索引错误:太多的索引器初始化子批-

fig, axs = plt.subplots(len(df.columns) // 3, 1, sharex=True) 
接下来,沿第一个轴执行
groupby
,但不要打印

gs = df.groupby(np.arange(len(df.columns)) // 3, axis=1)

最后,
zip
向上移动轴和
groupby
输出,并一次绘制每个轴

for (_, g), ax in zip(gs, axs):
     g.plot.bar(stacked=True, ax=ax)

plt.show()

非常感谢!!!!!如果没有子地块,有没有一种方法可以将某个特定地块内堆叠的钢筋的数量放在其顶部?@Bode,除非我误解,
stacked=True
是独立完成的。不,没有,我的意思是,每个钢筋顶部都有一个数字,表示该地块内堆叠钢筋的总数bar@Bode哦我懂了。。。对这是一个有趣的问题。我不知道怎么解决它!不过,你可以提出一个新问题。