python - Pandas - 带有用于堆叠的列值的堆叠条形图

标签 python pandas bar-chart

我有一个包含三组数据的数据集:类类型、邻域和可见性。

sample table

我正在尝试创建一个堆叠和非堆叠的条形图 - 按可见性堆叠,按邻域排列。所以基本上,我正在寻找此图表的非堆叠性的组合:

sample chart 1

nbvis_gb = nbvis.sort_values(by=['visibility'],ascending=False).groupby(by='visibility',sort=False)
fig, ax = plt.subplots(nrows=1,ncols=2,figsize=(14,8),sharey=True)

for (i, j), ax,color in zip(nbvis_gb,ax.flatten(),colors_hood):
    print(j['class'].values)
    title = str(i)
    j.plot.bar(ax=ax,colors=colors_hood)
    ax.set_title(title, fontsize=20)
    #ax.set_ylim(0,1.05)
    ax.tick_params(labelsize=16)
    ax.set_xticklabels(j['class'].values)
    ax.legend_.remove()


ax.legend(loc=8,fontsize=20,ncol=4,bbox_to_anchor=(0,-.45))
fig.tight_layout(h_pad=2)
fig.suptitle('Visibility of containers by class and neighborhood',y=1.03,fontsize=24)

以及此图表的堆叠度:

enter image description here

nbvis.unstack()['Neighborhood 1'].plot.bar(stacked=True)

任何帮助将不胜感激!

干杯, 伊丽莎白

最佳答案

考虑数据帧的meltpivot_table来创建与图形维度对齐的多索引数据帧。下面将图形输出到屏幕,并使用 seaborn 的配色方案将图形保存到同一文件夹中的 png 图像。当然,根据需要调整图表设置。

数据

import numpy as np
import pandas as pd
from itertools import product

from matplotlib import pyplot as plt
import seaborn

np.random.seed(444)
df = pd.DataFrame(list(product(['bucket (1)', 'flower pot (2)', 'tarp (3)', 'trash (6)', 'toy (7)',
                                'piping/tubing (9)', 'other (10)'],
                               ['visible containers', 'partial or not visible containers'])), 
                  columns=['class', 'visibility']).assign(Neighborhood1 = abs(np.random.randn(14)),
                                                          Neighborhood2 = abs(np.random.randn(14)),
                                                          Neighborhood3 = abs(np.random.randn(14)),
                                                          Neighborhood4 = abs(np.random.randn(14)))

绘图

seaborn.set()

def runplot(pvtdf):        
    fig, axes = plt.subplots(nrows=1, ncols=len(mdf['Neighborhood'].unique()))

    for i, n in enumerate(mdf['Neighborhood'].unique()):
        pvtdf.xs(n).plot(ax=axes[i], kind='bar', stacked=True, edgecolor='w', 
                figsize=(20,8), width=0.5, fontsize = 12, 
                title='{} - Visibility of containers \n by class and neighborhood'.format(n))
        axes[i].title.set_size(16)

    plt.tight_layout()
    fig.savefig('Output.png')
    plt.show()
    plt.clf()

# MELT LONG
mdf = pd.melt(df, id_vars = ['class', 'visibility'], var_name='Neighborhood')

# PIVOT WIDE
pvtdf = mdf.pivot_table(index= ['Neighborhood', 'class'], columns='visibility', values='value')

runplot(pvtdf, n)

plt.close()

输出

Neighborhood Graphs

关于python - Pandas - 带有用于堆叠的列值的堆叠条形图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47916154/

相关文章:

python - NoReverseMatch 位于/polls/(django 教程)

python - 总是在异常时调用 python 调试器的好通用方法

r - R中条形图中的文本

python - 如何根据配置文件(文本或CSV)中的用户输入过滤pandas数据框配置将告诉过滤器值和过滤列

matlab - 位置条分组条形图matlab

javascript - 水平条形图 D3,.txt 文件

python - 无法从多个 NumPy 数组中检索所需的索引

python - Webdriver 能以 Selenium IDE 的速度运行吗?

python - 如何在 df pandas 的行之间删除或填充相等的单元格值 0?

python - 如何在 Python 中迭代 2 个 DataFrame 并填充一列