简体   繁体   English

重塑 dataframe 和 plot 堆叠条形图

[英]Reshape dataframe and plot stacked bar graph

What I have我有的

I have a frame df of the following style, where each row represents a malfunction occured with specimen:我有以下样式的框架df ,其中每一行代表样本发生的故障:

index  specimen   malfunction  
1      'first'    'cracked'
2      'first'    'cracked'
3      'first'    'bent'
4      'second'   'bent'
5      'second'   'bent'
6      'second'   'bent'
7      'second'   'cracked'
8      'third'    'cracked'
9      'third'    'broken'

In real dataset I have about 15 different specimens and about 10 types of different malfunctions.在真实数据集中,我有大约 15 个不同的样本和大约 10 种不同的故障。

What I need我需要的

I want to plot a bar graph which represents how many malfunctions occured with specimen (so x-axis for specimen label, y-axis for number of malfunctions occured. I need a stacked bar chart so malfunctions must be separated by color.我想要 plot 一个条形图,它表示样本发生了多少故障(因此样本 label 的 x 轴,发生故障的数量的 y 轴。我需要一个堆叠条形图,因此必须用颜色分隔故障。

What I tried我试过的

I tried to use seaborn's catplot(kind='count') which would be exactly what I need if only it could plot a stacked chart.我尝试使用 seaborn 的catplot(kind='count')如果它可以 plot 堆叠图表,这正是我所需要的。 Unfortunately it can't, and I can't figure out how to reshape my data to plot it using pandas.plot.bar(stacked=True)不幸的是它不能,我不知道如何使用pandas.plot.bar(stacked=True)将我的数据重塑为 plot

The 1st step is to convert your categorial data in numeric:第一步是将分类数据转换为数字:

    import matplotlib.pyplot as plt

    df_toPlot = df #another dataframe keep original data in df

    df_toPlot['mapMal'] = df_toPlot.malfunction.astype("category").cat.codes

This is the print of df_toPlot.这是 df_toPlot 的打印。

             index specimen malfunction  mapMal
    0      1    first     cracked       2
    1      2    first     cracked       2
    2      3    first        bent       0
    3      4   second        bent       0
    4      5   second        bent       0
    5      6   second        bent       0
    6      7   second     cracked       2
    7      8    third     cracked       2
    8      9    third      broken       1


    df_toPlot.groupby(['specimen', 'mapMal']).size().unstack().plot(kind='bar', stacked=True)
    plt.show()

在此处输入图像描述

Try something like this:尝试这样的事情:

from matplotlib.pyplot import *
import pandas as pd

df = df.groupby(['specimen', 'malfunction']).count().unstack()

This generates the following table:这会生成下表:

Generated table生成的表

fig, ax = subplots()
df.plot(kind='bar', stacked=True, ax=ax)
ax.legend(["bent", "broken", "cracked"]);

The result is this graph: Result结果是这个图:结果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM