Matplotlib条形图:将两个不同的y轴对齐为零 [英] Matplotlib bar charts: Aligning two different y axes to zero

查看:35
本文介绍了Matplotlib条形图:将两个不同的y轴对齐为零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在条形图中有两组数据,它们的轴非常不同:一组数据非常负(-7500),而另一组数据正有些(+5).

如何将两个y轴对齐为0,但尺寸仍然合适?使用 set_ylim 意味着您看不到第二个数据集.

我正在使用的当前代码:

  A165H = [-4915,-7037]B167H = [-6927,-4105]B186H = [-5597,0]CH = [0,0]ConH = [0,0]#dS值列表A165S = [6.28,-4.91]B167S = [-3.25,6.7]B186S = [3.93,0]CS = [0,0]ConS = [0,0]N1H = [A165H [0],B167H [0],B186H [0],CH [0],ConH [0]]N1S = [A165S [0],B167S [0],B186S [0],CS [0],ConS [0]]打印(N1H)打印(N1S)N2H = [A165H [1],B167H [1],B186H [1],CH [1],ConH [1]]N2S = [A165S [1],B167S [1],B186S [1],CS [1],ConS [1]]宽度= 0.35无花果,ax1 = plt.subplots()ind = np.arange(len(N1H))rects1 = ax1.bar(ind,N1H,width,color ='b')ax1.set_xticks(ind + width)ax1.set_xticklabels(('A165','B167','B186','C','Con'))ax1.set_ylabel('dH',color ='b')对于ax1.get_yticklabels()中的tl:tl.set_color('b')ax2 = ax1.twinx()rects2 = ax2.bar(ind + width,N1S,width,color ='r')ax2.set_ylabel('dS',color ='r')对于ax2.get_yticklabels()中的tl:tl.set_color('r')plt.show() 

这是我的标准图片

使用

解决方案

如果我继续阅读

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 aligned at 0, yet still be a good size? Using set_ylim means you can't see the second data set.

Current code I'm using:

A165H = [-4915, -7037]
B167H = [-6927, -4105]
B186H = [-5597, 0]
CH =[0, 0] 
ConH = [0, 0]

# Lists of dS values

A165S = [6.28,-4.91]
B167S = [-3.25, 6.7]
B186S = [3.93, 0]
CS = [0, 0]
ConS = [0, 0]

N1H = [A165H[0], B167H[0], B186H[0], CH[0], ConH[0]]
N1S = [A165S[0], B167S[0], B186S[0], CS[0], ConS[0]]
print(N1H)
print(N1S)

N2H = [A165H[1], B167H[1], B186H[1], CH[1], ConH[1]]
N2S = [A165S[1], B167S[1], B186S[1], CS[1], ConS[1]]


width = 0.35
fig, ax1 = plt.subplots()
ind = np.arange(len(N1H))
rects1 = ax1.bar(ind, N1H, width, color = 'b')
ax1.set_xticks(ind+width)
ax1.set_xticklabels(('A165', 'B167', 'B186', 'C', 'Con'))
ax1.set_ylabel('dH', color='b')
for tl in ax1.get_yticklabels():
    tl.set_color('b')


ax2 = ax1.twinx()
rects2 = ax2.bar(ind + width, N1S, width, color = 'r')
ax2.set_ylabel('dS', color='r')
for tl in ax2.get_yticklabels():
   tl.set_color('r')

plt.show()

Here is my standard image

EDIT:

using the align_yaxis() from this question only shows me the negative values of the second data set:

解决方案

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)

这篇关于Matplotlib条形图:将两个不同的y轴对齐为零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆