<p>如前所述,根据matplotlib文档,可以使用<code>matplotlib.axes.Axes</code>类的<a href="https://matplotlib.org/api/_as_gen/matplotlib.axes.Axes.set_xlim.html" rel="noreferrer">^{<cd2>}</a>方法设置给定轴<code>ax</code>的x限制。</p>
<p>例如</p>
<pre><code>>>> ax.set_xlim(left_limit, right_limit)
>>> ax.set_xlim((left_limit, right_limit))
>>> ax.set_xlim(left=left_limit, right=right_limit)
</code></pre>
<p>一个极限可以保持不变(例如,左极限):</p>
<pre><code>>>> ax.set_xlim((None, right_limit))
>>> ax.set_xlim(None, right_limit)
>>> ax.set_xlim(left=None, right=right_limit)
>>> ax.set_xlim(right=right_limit)
</code></pre>
<p>要设置当前轴的x限制,<code>matplotlib.pyplot</code>模块包含<code>xlim</code>函数,该函数只包装<code>matplotlib.pyplot.gca</code>和<code>matplotlib.axes.Axes.set_xlim</code>。</p>
<pre><code>def xlim(*args, **kwargs):
ax = gca()
if not args and not kwargs:
return ax.get_xlim()
ret = ax.set_xlim(*args, **kwargs)
return ret
</code></pre>
<p>同样,对于y限制,使用<code>matplotlib.axes.Axes.set_ylim</code>或<code>matplotlib.pyplot.ylim</code>。关键字参数是<code>top</code>和<code>bottom</code>。</p>