我想要一个水平堆叠的条形图,在y轴上带有层次结构标签。我搜索了一下,发现以下漂亮的示例和代码。但这是垂直堆叠的条形图。我想将其应用于水平条形图,因此我只是进行了更改kind='barh',但这是行不通的。我设法通过在最后几行中将所有x更改为y来删除默认的ylabel。但是在定义的函数中将x更改为y并没有给我我想要的:层次结构标签仍在x轴上。有人可以帮忙吗?谢谢。PS:把事情少凌乱,我贴出的原代码我从第2答案发现了这个问题,而不是一个我试图修改import pandas as pdimport numpy as npfrom matplotlib import pyplot as pltfrom itertools import groupbydef test_table():data_table = pd.DataFrame({'Room': ['Room A'] * 4 + ['Room B'] * 3, 'Shelf': ['Shelf 1'] * 2 + ['Shelf 2'] * 2 + ['Shelf 1'] * 2 + ['Shelf 2'], 'Staple':['Milk', 'Water', 'Sugar', 'Honey', 'Wheat', 'Corn', 'Chicken'], 'Quantity': [10, 20, 5, 6, 4, 7, 2,], 'Ordered': np.random.randint(0, 10, 7) })data_tabledef add_line(ax, xpos, ypos):line = plt.Line2D([xpos, xpos], [ypos + .1, ypos], transform=ax.transAxes, color='black')line.set_clip_on(False)ax.add_line(line)def label_len(my_index,level):labels = my_index.get_level_values(level)return [(k, sum(1 for i in g)) for k,g in groupby(labels)]def label_group_bar_table(ax, df):ypos = -.1scale = 1./df.index.sizefor level in range(df.index.nlevels)[::-1]: pos = 0 for label, rpos in label_len(df.index,level): lxpos = (pos + .5 * rpos)*scale ax.text(lxpos, ypos, label, ha='center', transform=ax.transAxes) add_line(ax, pos*scale, ypos) pos += rpos add_line(ax, pos*scale , ypos) ypos -= .1df = test_table().groupby(['Room','Shelf','Staple']).sum()fig = plt.figure()ax = fig.add_subplot(111)df.plot(kind='bar',stacked=True,ax=fig.gca())#Below 3 lines remove default labelslabels = ['' for item in ax.get_xticklabels()]ax.set_xticklabels(labels)ax.set_xlabel('')label_group_bar_table(ax, df)fig.subplots_adjust(bottom=.1*df.index.nlevels)plt.show()Python
添加回答
举报
0/150
提交
取消