有没有办法轻松压缩matplotlib中轴的一部分?

时间:2015-07-31 08:57:49

标签: python matplotlib axis scaling

我有this graph虽然低于0%的数据非常重要,但需要显示,但这种情况太少,不足以保证整个图表的倾斜。是否有办法使其低于0%时缩放/压缩比例,因此相同距离的%变化较大。 (我不想使用断轴,因为可能有其他数据< 0%)

我已经看到this是可行的方法,但是我想要的是< 0%,只是具有不同的缩放值。有没有简单的方法呢?

2 个答案:

答案 0 :(得分:0)

I think your only option would be indeed to create a custom projection for your axes. In theory it shouldn't be too difficult since you want a linear scale with just a different scale for positive and negative values, but you'll still have to to write quite a bit of code to create the relevant class.

http://matplotlib.org/devel/add_new_projection.html and as you already linked: http://matplotlib.org/examples/api/custom_scale_example.html

答案 1 :(得分:0)

所以我找到了一个解决方案,但这有点像软糖。 This is what it looks like.

以下是代码:

fig, ax = plt.subplots()
fig.canvas.set_window_title("Frame-to-frame pump-to-probe")
plt.xlabel(r'$t/ps$')
plt.ylabel(r'%')
min = 0
for i in range(len(ftf_PtmPlist)):
    if ftf_PtmPlist[i] < min:
        min = ftf_PtmPlist[i]
    if ftf_PtmPlist[i] < 0:
        ftf_PtmPlist[i] = 2*ftf_PtmPlist[i]/10.0
for i in range(len(ftf_PttPlist)):
    if ftf_PttPlist[i] < min:
        min = ftf_PttPlist[i]
    if ftf_PttPlist[i] < 0:
        ftf_PttPlist[i] = 2*ftf_PttPlist[i]/10.0
lables = [0,20,40,60,80,100]
ymin = int(20*math.floor(min/100.0))
for y in range(ymin,-19,20):
    lables.append(y)
lables = sorted(lables)
for i in range(-ymin/20):
    lables[i] = str(int(lables[i]*10/2.0))
ax.set_yticklabels(lables)
plt.plot(timelist,ftf_PtmPlist,'magenta', label='pump to main probe efficiency')
plt.plot(timelist,ftf_PttPlist,'teal', label='pump to total probe efficiency')
plt.plot(timelist,p_deplist,'orange', label='pump energy depletion')
plt.gca().set_ylim(top=105)
plt.gca().set_xlim(left=-1.0)
plt.legend(bbox_to_anchor=(0.55, 0.17), loc=2, borderaxespad=0., prop={'size':10})

首先,我将所有小于0的值按比例缩放0.2,这两个列表有可能为负值,同时总体上寻找最负值。然后,我使用此值来查找最负的刻度标签应该是什么(给定我正在使用的缩放)并创建新的刻度标签列表,以便负值显示为我们缩小它们之前的值。

我知道这有点草率,但它确实起作用了!