我设法生成了一个交互式matplotlib图形,该图形使用一些ipywidget滑块更改椭圆的位置。
但是,该图显示在新窗口中,我无法在笔记本中找到该图的有效解决方案。
我可以在Jupyter笔记本中生成其他交互式图形,例如线条图,但是使用椭圆,没办法。
%matplotlib
from matplotlib import patches
import matplotlib.pyplot as plt
from ipywidgets import interact
fig = plt.figure()
ax = fig.add_subplot(111)
ellipse = patches.Ellipse((0,0), width=50, height=25, angle=15)
ax.add_patch(ellipse)
ax.set_xlim(-100, 100)
ax.set_ylim(-100, 100)
@interact
def update(xcenter=(-50,50), ycenter=(-50,50)):
ellipse.set_center((xcenter, ycenter))
#fig.canvas.draw()
plt.plot()
在大多数情况下,使用% matplotlib notebook
解决了这个问题。