python - In Jupyter Notebook and matplotlib, how to avoid replot everything in an interactive plot
问题描述
In jupyter notebook, have a plot side by side with another animation. For each frame of the animation, the plot should update with a vertical line and a red dot showing the value of that property. A minimal working example (without the animation) is below:
import ipywidgets as wg
import matplotlib.pyplot as plt
class interactive_plot:
def __init__(self,r):
self.X = [x for x in range(r)]
self.Y = [y for y in range(r)]
self.r = r
self.fig = None
self.ax = None
def pathPlot(self, xframe = None):
if self.fig is None:
self.fig = plt.figure()
self.ax =self.fig.add_subplot(1,1,1)
self.ax.plot(self.X, self.Y, 'bo-')
else:
self.fig = plt.gcf()
self.ax = plt.gca()
self.ax.plot(self.X, self.Y, 'bo-')
if xframe is not None:
self.ax.axvline(x = self.X[xframe],color = 'black',linestyle = '--')
self.ax.plot(self.X[xframe],self.Y[xframe], 'ro', markersize=10)
def pathPlay(self):
frame = wg.IntSlider(min = 0, max = self.r - 1)
wg.interact(self.pathPlot, xframe = frame)
test = interactive_plot(10)
test.pathPlay()
This minimal example works "fine". However, in my jupyter notebook there is another animation side by side with this, and as the number of animations increase, perfomance is an issue (One may see the plot being cleared and reploted).
I thought that maybe I could avoid the line self.ax.plot(self.X, self.Y, 'bo-')
under the else
statement (substituting with a self.fig.show()) and just update the vertical line with the red dot. However, I could not make this. Everytime, the blue line goes away and I get only the vertical line and the red dot in the plot.
So I was wondering:
1- What is the correct way to just update a plot?
2- Will I really get a performance enhancement? At least I won't see the plot being cleared and reploted, right?
解决方案
推荐阅读
- reactjs - 更新到 React 17 时枚举出错,
- javascript - 格式化 mailto 链接,以便 PHP 可以清晰地解释它
- sql-server - 如何将证书和签名程序集部署到其他数据库?
- javascript - 反应JS | 无法将 undefined 或 null 转换为对象(带 Formik)
- javascript - 由于错误而无法加载模型并且 AppRegistry.registerComponent 未被调用 REACT-NATIVE
- flutter - 如何在颤动中保存范围滑块值
- java - 在 ORMlite 中获取磁盘上的 PostgreSql 表大小(字节)
- google-cloud-platform - 谷歌监视器 - 带有 GCE 过滤器的 CPU、内存和磁盘使用情况的仪表板
- c# - 升级到 .NET 4.0 后 C# 动态 JObject 不起作用
- autohotkey - 无法在 AutoHotkey 中进行切换