<p>最明显的方法就是手动设置你想要的限制。(例如<code>ax.axis([xmin, xmax, ymin, ymax])</code>)</p>
<p>如果你不想费心手动找出极限,你有两个选择。。。</p>
<p>正如一些人(tillsten,Yann和Vorticity)所提到的,如果你能在最后一次绘制出你想忽略的函数,那么你可以在绘制之前禁用自动缩放,或者将<code>scaley=False</code>kwarg传递给<code>plot</code></p>
<pre><code>import numpy as np
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
x1 = np.linspace(-1,1,100)
ax.plot(x1, np.sin(x1))
ax.plot(x1, np.sin(x1 / 2.0))
ax.autoscale(False) #You could skip this line and use scalex=False on
ax.plot(x1, 3 * np.sin(x1)) #the "theoretical" plot. It has to be last either way
fig.savefig('test.pdf')
</code></pre>
<p>注意,你可以调整最后一个图的<code>zorder</code>,这样它就画在“中间”,如果你想控制它的话。</p>
<p>如果不想依赖于顺序,而只想指定要根据其自动缩放的行的列表,则可以执行以下操作:(注意:如果处理的是<code>Line2D</code>对象,而不是一般的matplotlib艺术家,则这是一个简化版本。)</p>
<pre><code>import numpy as np
import matplotlib.pyplot as plt
import matplotlib.transforms as mtransforms
def main():
fig, ax = plt.subplots()
x1 = np.linspace(-1,1,100)
line1, = ax.plot(x1, np.sin(x1))
line2, = ax.plot(x1, 3 * np.sin(x1))
line3, = ax.plot(x1, np.sin(x1 / 2.0))
autoscale_based_on(ax, [line1, line3])
plt.show()
def autoscale_based_on(ax, lines):
ax.dataLim = mtransforms.Bbox.unit()
for line in lines:
xy = np.vstack(line.get_data()).T
ax.dataLim.update_from_data_xy(xy, ignore=False)
ax.autoscale_view()
if __name__ == '__main__':
main()
</code></pre>
<p><img src="https://i.stack.imgur.com/SERRH.png" alt="enter image description here"/></p>