[英]Matplotlib: Aligning two y-axis around zero
这是另一种方式,我认为它更通用。
import numpy as np
%matplotlib inline
import matplotlib.pyplot as plt
def align_zeros(axes):
ylims_current = {} # Current ylims
ylims_mod = {} # Modified ylims
deltas = {} # ymax - ymin for ylims_current
ratios = {} # ratio of the zero point within deltas
for ax in axes:
ylims_current[ax] = list(ax.get_ylim())
# Need to convert a tuple to a list to manipulate elements.
deltas[ax] = ylims_current[ax][1] - ylims_current[ax][0]
ratios[ax] = -ylims_current[ax][0]/deltas[ax]
for ax in axes: # Loop through all axes to ensure each ax fits in others.
ylims_mod[ax] = [np.nan,np.nan] # Construct a blank list
ylims_mod[ax][1] = max(deltas[ax] * (1-np.array(list(ratios.values()))))
# Choose the max value among (delta for ax)*(1-ratios),
# and apply it to ymax for ax
ylims_mod[ax][0] = min(-deltas[ax] * np.array(list(ratios.values())))
# Do the same for ymin
ax.set_ylim(tuple(ylims_mod[ax]))
x = np.array(range(1,11))
y1 = 5*x-10
y2 = -10*x+5
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.plot(x,y1,'r',label='y1')
ax1.set_ylabel('y1')
ax2 = ax1.twinx()
ax2.plot(x,y2,'g',label='y2')
ax2.set_ylabel('y2')
align_zeros([ax1,ax2])
ax1.legend(loc='upper left')
ax2.legend(loc='upper right')
plt.show()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.