Histogram with breaking axis and interlaced colorbar

荒凉一梦 提交于 2019-12-03 10:16:41

问题


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

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:

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".


回答1:


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



来源:https://stackoverflow.com/questions/13027147/histogram-with-breaking-axis-and-interlaced-colorbar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!