Matplotlib bar charts: Aligning two different y axes to zero

后端 未结 1 902
温柔的废话
温柔的废话 2021-01-29 02:25

I have two sets of data in a barchart which have very different axes: one is very negative (-7500) and one is slightly positive (+5).

How can I have the two y axes alig

1条回答
  •  南笙
    南笙 (楼主)
    2021-01-29 02:33

    If I had carried on reading the the other post I would have found the adjust_yaxis which solved my problem

    The code given on that answer:

    def align_yaxis(ax1, v1, ax2, v2):
    """adjust ax2 ylimit so that v2 in ax2 is aligned to v1 in ax1"""
    _, y1 = ax1.transData.transform((0, v1))
    _, y2 = ax2.transData.transform((0, v2))
    adjust_yaxis(ax2,(y1-y2)/2,v2)
    adjust_yaxis(ax1,(y2-y1)/2,v1)
    
    def adjust_yaxis(ax,ydif,v):
        """shift axis ax by ydiff, maintaining point v at the same location"""
        inv = ax.transData.inverted()
        _, dy = inv.transform((0, 0)) - inv.transform((0, ydif))
        miny, maxy = ax.get_ylim()
        miny, maxy = miny - v, maxy - v
        if -miny>maxy or (-miny==maxy and dy > 0):
            nminy = miny
            nmaxy = miny*(maxy+dy)/(miny+dy)
        else:
            nmaxy = maxy
            nminy = maxy*(miny+dy)/(maxy+dy)
        ax.set_ylim(nminy+v, nmaxy+v)
    

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