擅长:python、mysql、java
<p>如果你想改变这个数字,而不是创建一个新的数字,我可以建议如下方法:</p>
<ol>
<li>使用交互式后端;<code>%matplotlib notebook</code></li>
<li>更新绘图中的线,而不是绘制新的线。</li>
</ol>
<p>所以代码可能是这样的:</p>
<pre><code>%matplotlib notebook
from ipywidgets import *
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 2 * np.pi)
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
line, = ax.plot(x, np.sin(x))
def update(w = 1.0):
line.set_ydata(np.sin(w * x))
fig.canvas.draw_idle()
interact(update);
</code></pre>
<p><a href="https://i.stack.imgur.com/PPkbK.png" rel="noreferrer"><img src="https://i.stack.imgur.com/PPkbK.png" alt="enter image description here"/></a></p>
<p>或者,您可以使用<code>plt.show()</code>作为<a href="https://stackoverflow.com/a/45694414/4124317">this answer</a>。</p>