Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pandas scatterplots: stop automatic scaling of axis

I am trying to create two separate scatterplots with identical axes. As the code below shows, I am explicitly setting my axis limits. However, because the spread of the data is different between the two plots, the scaling of the x axis within these limits is changing. Compare Figures 1 & 2 below, plotted using identical code (except the different x values):

Figure 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

Figure 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

I appreciate that this is probably happening to best show me the variability within my data, but I want to compare the plots directly - the shift in space occupied on the log scale is useful to know. SO, does anybody out there know how I can stop this automatic scaling of the x axis from happening? (i.e. I want the x axis to be identical on the two plots)

like image 683
Ian Ashpole Avatar asked Dec 05 '25 05:12

Ian Ashpole


1 Answers

You can't set an xlim of 0 on a logarithmic axis. Try setting it to the number on the low end of the axes you want to match. e.g.

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

ax2.set_xlim([2e-3,0.4])
like image 69
tmdavison Avatar answered Dec 07 '25 18:12

tmdavison