Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Histogram with breaking axis and interlaced colorbar

I have data as those ones

        a       b       c       d       e
alpha   5.51    0.60    -0.12   26.90   76284.53
beta    3.39    0.94    -0.17   -0.20   -0.20
gamma   7.98    3.34    -1.41   7.74    28394.93
delta   2.29    1.24    0.40    0.29    0.28

I want to do a nice publishable histogram as this one interlaced colorbar histogram

but with a break in the y axis so we can figure out the variation of a , b , c , d and e so that data will not be squashed by extreme values in e column as this one but using interlaced colorbar histogram: breaking y axis

I would like to do that in python (matplotlib, pandas, numpy/scipy) or in mathematica... or any other open and free high-level language (R, scilab, ...). Thanks for your help.

edit: using matplotlib through pandas allows to adjust the space between the two subgraph using option button at bottom left "hspace".

like image 649
sol Avatar asked Dec 15 '22 18:12

sol


1 Answers

Have you seen this example? It's for a broken y-axis plot in matplotlib.

Hope this helps.

Combining with pandas this gives:

import pandas as pd
import matplotlib.pyplot as plt
from StringIO import StringIO

data = """\
        a       b       c       d       e
alpha   5.51    0.60    -0.12   26.90   76284.53
beta    3.39    0.94    -0.17   -0.20   -0.20
gamma   7.98    3.34    -1.41   7.74    28394.93
delta   2.29    1.24    0.40    0.29    0.28
"""

df = pd.read_csv(StringIO(data), sep='\s+')

f, axis = plt.subplots(2, 1, sharex=True)
df.plot(kind='bar', ax=axis[0])
df.plot(kind='bar', ax=axis[1])
axis[0].set_ylim(20000, 80000)
axis[1].set_ylim(-2, 30)
axis[1].legend().set_visible(False)

axis[0].spines['bottom'].set_visible(False)
axis[1].spines['top'].set_visible(False)
axis[0].xaxis.tick_top()
axis[0].tick_params(labeltop='off')
axis[1].xaxis.tick_bottom()
d = .015
kwargs = dict(transform=axis[0].transAxes, color='k', clip_on=False)
axis[0].plot((-d,+d),(-d,+d), **kwargs)
axis[0].plot((1-d,1+d),(-d,+d), **kwargs)
kwargs.update(transform=axis[1].transAxes)
axis[1].plot((-d,+d),(1-d,1+d), **kwargs)
axis[1].plot((1-d,1+d),(1-d,1+d), **kwargs)
plt.show()

enter image description here

like image 179
dmcdougall Avatar answered Jan 23 '23 07:01

dmcdougall