Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matplotlib bar chart - overlay bars similar to stacked

I want to create a matplotlib bar plot that has the look of a stacked plot without being additive from a multi-index pandas dataframe.

The below code gives the basic behaviour

%matplotlib notebook
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import io

data = io.StringIO('''Fruit,Color,Price
Apple,Red,1.5
Apple,Green,1.0
Pear,Red,2.5
Pear,Green,2.3
Lime,Green,0.5
Lime, Red, 3.0
''')
df_unindexed = pd.read_csv(data)
df_unindexed
df = df_unindexed.set_index(['Fruit', 'Color'])
df.unstack().plot(kind='bar')

The plot command df.unstack().plot(kind='bar') shows all the apple prices grouped next to each other. If you choose the option df.unstack().plot(kind='bar',stacked=True) - it adds the prices for Red and Green together and stacks them.

I am wanting a plot that is halfway between the two - it shows each group as a single bar, but overlays the values so you can see them all. The below figure (done in powerpoint) shows what behaviour I am looking for -> I want the image on the right.

Short of calculating all the values and then using the stacked option, is this possible?

example bar plot

like image 499
Esme_ Avatar asked Jan 04 '19 09:01

Esme_


People also ask

How is a stacked bar different from other bar graphs?

A traditional bar chart displays a single categorical variable in the form of bars. A stacked bar chart shows two categorical variables. The first (and primary) variable is shown along the entire length of the bar, and the second variable is represented as stacks within each categorical bar.

What is another name for a stacked bar chart?

Each bar in the chart represents a whole, and the segments represent parts of that whole. Different colors used for the segments distinguish the categories. Stacked bar charts are also known as stacked bar graphs.

When would you use a stacked bar graph instead of a double bar graph?

We want to move to a stacked bar chart when we care about the relative decomposition of each primary bar based on the levels of a second categorical variable. Each bar is now comprised of a number of sub-bars, each one corresponding with a level of a secondary categorical variable.

What is the best reason to use a stacked bar chart?

Advantages of stacked bar chartsYou can see data points more clearly when they are on top of each other, and you can quickly see the percentage of each data point compared to the total value. You can also easily track multiple trend lines.


1 Answers

This seems (to me) like a bad idea, since this representation leads to several problem. Will a reader understand that those are not staked bars? What happens when the front bar is taller than the ones behind?

In any case, to accomplish what you want, I would simply repeatedly call plot() on each subset of the data and using the same axes so that the bars are drawn on top of each other. In your example, the "Red" prices are always higher, so I had to adjust the order to plot them in the back, or they would hide the "Green" bars.

fig,ax = plt.subplots()

my_groups = ['Red','Green']
df_group = df_unindexed.groupby("Color")

for color in my_groups:
    temp_df = df_group.get_group(color)
    temp_df.plot(kind='bar', ax=ax, x='Fruit', y='Price', color=color, label=color)

enter image description here

like image 60
Diziet Asahi Avatar answered Sep 28 '22 05:09

Diziet Asahi