我的问题的重点是控制右侧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()
答案 0 :(得分:0)
您只为set_ylim
方法提供了bottom
参数。此外,如果尚未禁用轴的自动缩放,则应在绘图后调用set_ylim
方法。如果您希望y轴的下限默认为0,那么uo可以像这样修改您的代码:
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_bottom = 0, y1_lim = None, # bottom and top limits respectively
y2_bottom = 0, y2_lim = None): # bottom and top limits, respectively
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)
p1, = host.plot(time, aData1, label=aLegend1Label)
p2, = par1.plot(time, aData2, label=aLegend2Label)
if y1_lim != None:
par1.set_ylim(y1_bottom, y1_lim)
if y2_lim != None:
par2.set_ylim(y2_bottom, y2_lim)
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()
这使得输出具有所需的y限制:
import numpy as np
time = np.arange(25)
plotTwoAxis(np.random.rand(25),
np.random.rand(25),
'x label 1', 'y label 1', 'y label 2','Title', ' Subtitle',
'Plot 1','Plot 2', y1_lim = 0.5, y2_lim = 0.75 )
旁注;这是你想要的关于标题和副标题的行为吗?