pandas scatterplots:停止自动缩放轴

时间:2016-01-29 13:10:10

标签: python pandas matplotlib plot

我正在尝试创建两个具有相同轴的独立散点图。如下面的代码所示,我明确设置了我的轴限制。但是,由于两个图之间的数据传播不同,因此这些限制内的x轴的缩放正在发生变化。比较图1和图2。下面的图2,使用 相同的 代码绘制(除了不同的x值):

图1

#set up figure
fig, ax1 = plt.subplots(1,1)

#plot first series (left y axis)
df.plot(ax=ax1, kind='line', x=bin1, y='hfall', logx=True,
        color='DarkBlue', style='.', markersize=5, legend=False)

#set up second axis as duplicate of the first
ax2 = ax1.twinx()

#plot second series (right y axis)
df.plot(ax=ax2, kind='line', x=bin1, y='vf', logx=True, 
        color='Red', style='.', markersize=5, legend=False)

#set axes limits
ax1.set_xlim([0.,0.4])
ax1.set_ylim([1,1.15])
ax2.set_xlim([0.,0.4])
ax2.set_ylim([2e-06,6e-06])

#set labels and title        
ax1.set_ylabel('Total horizontal flux ($kg/m^2/s$)', color='DarkBlue')
ax1.set_xlabel('horizontal flux in bin1 ($kg/m^2/s$)')
ax1.set_title('bin1', loc='center', fontsize=14)
ax2.ticklabel_format(axis='y', style='sci', scilimits=(0,0))
ax2.set_ylabel('Total vertical flux (all bins) ($kg/m^2/s$)', color='Red')

plt.show()

Figure1

图2

#set up figure
fig, ax1 = plt.subplots(1,1)

#plot first series (left y axis)
df.plot(ax=ax1, kind='line', x=bin2, y='hfall', logx=True,
        color='DarkBlue', style='.', markersize=5, legend=False)

#set up second axis as duplicate of the first
ax2 = ax1.twinx()

#plot second series (right y axis)
df.plot(ax=ax2, kind='line', x=bin2, y='vf', logx=True, 
        color='Red', style='.', markersize=5, legend=False)

#set axes limits
ax1.set_xlim([0.,0.4])
ax1.set_ylim([1,1.15])
ax2.set_xlim([0.,0.4])
ax2.set_ylim([2e-06,6e-06])

#set labels and title        
ax1.set_ylabel('Total horizontal flux ($kg/m^2/s$)', color='DarkBlue')
ax1.set_xlabel('horizontal flux in bin2 ($kg/m^2/s$)')
ax1.set_title('bin2', loc='center', fontsize=14)
ax2.ticklabel_format(axis='y', style='sci', scilimits=(0,0))
ax2.set_ylabel('Total vertical flux (all bins) ($kg/m^2/s$)', color='Red')

plt.show()

Figure 2

我很欣赏这可能是为了最好地向我展示我的数据中的可变性,但我想直接比较这些图 - 在对数刻度上占用的空间的变化是有用的。那么,有没有人知道如何阻止x轴的自动缩放发生? (即我希望x轴在两个图上相同)

1 个答案:

答案 0 :(得分:2)

您无法在对数轴上设置xlim为0。尝试将其设置为您想要匹配的轴的低端数字。 e.g。

ax1.set_xlim([2e-3,0.4])

ax2.set_xlim([2e-3,0.4])