<p>这个问题与jupyter笔记本及其内联后端的使用有关。因此,如果使用<code>%matplotlib notebook</code>后端,您将获得正确的输出。(为此,需要重新启动内核。)</p>
<pre><code>%matplotlib notebook
from mpl_toolkits.axisartist.axislines import SubplotZero
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure(1)
ax = SubplotZero(fig, 111)
fig.add_subplot(ax)
for direction in ["xzero", "yzero"]:
# adds arrows at the ends of each axis
ax.axis[direction].set_axisline_style("-|>", size=5)
# adds X and Y-axis from the origin
ax.axis[direction].set_visible(True)
for direction in ["left", "right", "bottom", "top"]:
# hides borders
ax.axis[direction].set_visible(False)
x = np.linspace(-0.5, 1., 100)
ax.plot(x, np.sin(x*np.pi))
plt.show()
</code></pre>
<p><a href="https://i.stack.imgur.com/UsFo0.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/UsFo0.png" alt="enter image description here"/></a></p>
<p>如果您想/需要使用<code>%matplotlib inline</code>后端,您可能需要还原一些设置,这样箭头就不会从图形中裁剪出来。你知道吗</p>
<ol>
<li><p>创建png图形的默认设置是使用<code>bbox_inches="tight"</code>选项。这可以通过</p>
<pre><code>%config InlineBackend.print_figure_kwargs = {'bbox_inches':None}
</code></pre></li>
<li><p>默认地物大小、dpi和子地块参数<a href="https://stackoverflow.com/questions/42656668/matplotlibrc-rcparams-modified-for-jupyter-inline-plots">are different</a>。恢复这些可以通过</p>
<pre><code>plt.rcdefaults()
</code></pre></li>
</ol>
<p>由于Iypthon中的<a href="https://github.com/jupyter/notebook/issues/3385" rel="nofollow noreferrer">a bug</a>,rcParameters不应设置在笔记本的第一个单元格中。你知道吗</p>
<p>因此</p>
<pre><code># Cell 1
%matplotlib inline
%config InlineBackend.print_figure_kwargs = {'bbox_inches':None}
# Cell 2
import matplotlib.pyplot as plt
plt.rcdefaults()
from mpl_toolkits.axisartist.axislines import SubplotZero
import numpy as np
fig = plt.figure(1)
ax = SubplotZero(fig, 111)
fig.add_subplot(ax)
for direction in ["xzero", "yzero"]:
# adds arrows at the ends of each axis
ax.axis[direction].set_axisline_style("-|>", size=5)
# adds X and Y-axis from the origin
ax.axis[direction].set_visible(True)
for direction in ["left", "right", "bottom", "top"]:
# hides borders
ax.axis[direction].set_visible(False)
x = np.linspace(-0.5, 1., 100)
ax.plot(x, np.sin(x*np.pi))
plt.show()
</code></pre>
<p><a href="https://i.stack.imgur.com/CUNp1.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/CUNp1.png" alt="enter image description here"/></a></p>