我的问题的重点是控制右侧 y 轴限制。编码:
if y2_lim != None:
par2.set_ylim(y2_lim)
在情节调用之前或之后没有影响。我难住了。任何输入帮助将不胜感激,特别是解释为什么它不起作用。这种类型的难题在 Matplotlib 中经常发生。谢谢,查尔斯
from mpl_toolkits.axes_grid1 import host_subplot
import mpl_toolkits.axisartist as AA
import matplotlib.pyplot as plt
def plotTwoAxis(aData1, aData2, anXLabel, aY1Label, aY2Label, aTitle,
aSubtitle, aLegend1Label, aLegend2Label, y1_lim = None, y2_lim = None):
host = host_subplot(111, axes_class=AA.Axes)
par1 = host.twinx()
par2 = host.twinx()
offset = 60
new_fixed_axis = par2.get_grid_helper().new_fixed_axis
par2.axis["right"] = new_fixed_axis(loc="right",
axes=par2,
offset=(offset, 0))
par2.axis["right"].toggle(all=True)
host.set_xlim(0, 24)
host.set_xlabel(anXLabel)
host.set_ylabel(aY1Label)
par1.set_ylabel(aY2Label)
if y1_lim != None:
par1.set_ylim(y1_lim)
if y2_lim != None:
par2.set_ylim(y2_lim)
p1, = host.plot(time, aData1, label=aLegend1Label)
p2, = par1.plot(time, aData2, label=aLegend2Label)
host.legend()
host.axis["left"].label.set_color(p1.get_color())
par1.axis["right"].label.set_color(p2.get_color())
plt.title(aTitle + aSubtitle)
plt.draw()
plt.show()