harijay
harijay

Reputation: 11883

pandas "stacked" bar plot with values not added to give height

I am trying to display a bar plot in pandas 0.18.1 where the values for the different columns are displayed on top of each other but not added. So this is I think a stacked bar chart without the "stacking" which adds all stack values.

So in the example below

import pandas
from pandas import DataFrame

so_example = DataFrame( [(15 , 0 , 0 , 4),(16,  0,  1,  4),(17 , 0 , 0 , 6)]).set_index(0)
so_example.plot.bar(stacked=True)

This gives the Dataframe

>>> so_example
    1  2  3
0          
15  0  0  4
16  0  1  4
17  0  0  6

I get for the second point "16" a max height of 1 + 4 = 5 . Instead I want the max height to be 4 and the "1" shown in green like it is now.

stacked bar plot

How do I achieve this without subtracting artificially. Sorry I dont know what these "stacked" plots are called so all my searching failed to yield a simple solution.

Upvotes: 3

Views: 647

Answers (1)

MaThMaX
MaThMaX

Reputation: 2015

Please check out the following code, it is not a comprehensive solution, but basically achieve what you want.

import pandas as pd
import matplotlib.pyplot as plt

so_example = pd.DataFrame( [(15 , 0 , 0 , 4),(16,  0,  1,  4),(17 , 0 , 0 , 6)]).set_index(0)
fig = plt.figure()
ax = fig.add_subplot(111)
_max = so_example.values.max()+1
ax.set_ylim(0, _max)
so_example.ix[:,1].plot(kind='bar', alpha=0.8, ax=ax, color='r')
ax2 = ax.twinx()
ax2.set_ylim(0, _max)
so_example.ix[:,2].plot(kind='bar', alpha=0.8, ax=ax2, color='b')
ax3 = ax.twinx()
ax3.set_ylim(0, _max)
so_example.ix[:,3].plot(kind='bar', alpha=0.8, ax=ax3, color='g')

fig.savefig('c:\haha.png')
fig.show()

enter image description here


Here is my thinking:

  1. First of all, I tried the same thing as you did, tried finding some plug and play solutions, but it seems no
  2. Then I tried to play with the values, but you clearly said you don't want to artificially play with the values. I personally think it really depends on how you define artifical, I mean do some data processing for the Dataframe before plotting it would not be that difficult.
  3. Anyway, here we jump into the third solution, which is to play with the axis. Since basically, your request is to make the bar plot in stacked way but with overlapping. I mean usually the stacked bar means you stack everthing on top of each other without any overlapping, that is why it is called stack. But since you want to organize the bar in a way that the smallest value is at the very front, the second smallest value is at the 2nd, so on so forth...

So here, I use twinx() to create different axis layer for each data set, and to make things a bit easier for me, I didn't sort them but just use alpha=0.8 to change the transparency only. and I didn't use functions for the whole thing. Anyway, I think this is one approach.

Upvotes: 2

Related Questions