Histogram with breaking axis and interlaced colorbar

前端 未结 1 1311
失恋的感觉
失恋的感觉 2021-02-04 21:06

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


        
1条回答
  •  攒了一身酷
    2021-02-04 21:56

    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

    0 讨论(0)
提交回复
热议问题