是否可以分层标记matplotlib(pyplot)条形图? [英] Is it possible to hierarchically label a matplotlib (pyplot) bar plot?
问题描述
我设法使用matplotlib.pyplot制作了以下条形图.
I've managed to make the following bar plot using matplotlib.pyplot.
该图来自汇总的PANDAS DataFrame,如下所示.请注意,条形图中的每个条形对应于mean
列中的值.还请注意,值不为零,但当浮点非常小时,PANDAS将输出0
和-0
.
The plot comes from an aggregated PANDAS DataFrame, printed below. Note that each bar in the bar-plot corresponds to a value in the mean
column. Also note that the values are not zero, but that PANDAS outputs 0
and -0
when floats are very small.
Group Local Global Attn mean
ASub LD GD Attn -0
Dist -0
GS Attn -0
Dist -0
LS GD Attn -0
Dist -0
GS Attn -0
Dist -0
DSub LD GD Attn -0
Dist 0
GS Attn -0
Dist -0
LS GD Attn -0
Dist -0
GS Attn -0
Dist -0
我想以与上述标签相对应的方式对条形图的x轴进行分层标记.换句话说,x轴的左半部分对应于ASub
组. ASub
组的左半部分对应于Local
因子的LD
级别,依此类推...
I would like to label the x-axis of my bar plot hierarchically, in a manner corresponding to the labels above. In other words, the left half of the x-axis corresponds to the ASub
group. The left half of the ASub
group corresponds to the LD
level of the Local
factor, and so on...
可以做到吗?
我想我可能应该确切说明我想要什么.我希望有几个标签,从最一般的(组)到最具体的(Attn),类似于上面DataFrame左4列中的组织.
I think I should probably clarify exactly what I want. I'd like for there to be several labels, progressing from most general (Group) to most specific (Attn), similar to the organization on the left 4 columns of the DataFrame above.
推荐答案
在主机上已实现(我将随机数据用于均值"),请参见底部的图像. 如果您不想升级,也可以手动设置:
On master this is implemented (i used random data for 'mean'), see image at the bottom. If you prefer not to upgrade you can also set it manually:
In [143]: ax = df.plot(kind='bar')
In [144]: ax.set_xticklabels(['|'.join(t) for t in df.index])
In [167]: ax = df.plot(kind='bar')
In [168]: ax.set_xticklabels(df.index.format(names=False))
这篇关于是否可以分层标记matplotlib(pyplot)条形图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!