1

What I have

I have a frame df of the following style, where each row represents a malfunction occured with specimen:

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.

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.

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. Unfortunately it can't, and I can't figure out how to reshape my data to plot it using pandas.plot.bar(stacked=True)

Dmitrii Begal
  • 75
  • 1
  • 5

2 Answers2

0

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.

             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()

enter image description here

powerPixie
  • 718
  • 9
  • 20
0

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

  • Thanks. Actually, I found this answer a little bit later here: https://stackoverflow.com/questions/23415500/pandas-plotting-a-stacked-bar-chart So my question is kind of a duplicate. – Dmitrii Begal Nov 07 '19 at 14:30