计算过程中的交互式 Matplotlib 绘图
使用 Matplotlib 创建可视化效果时,通常需要在计算过程中继续探索结果。但是,默认行为是阻止计算,直到调用 show() 函数为止。
分离绘图
要从主计算线程分离绘图,有两个方法方法:
使用draw():
此方法允许选择性地重绘绘图。绘制数据后使用draw(),而不是调用show()。当绘图保持交互时,计算将继续。然而,多次调用draw()可能会导致绘图闪烁。
from matplotlib.pyplot import plot, draw, show plot([1,2,3]) draw() print('continue computation') # at the end call show to ensure window won't close. show()
启用交互模式:
此方法使用Matplotlib的交互模式。调用 ion() 启用交互模式,该模式会在每个绘图命令后自动重绘绘图。计算将继续,同时绘图可以交互式缩放、平移和研究。
from matplotlib.pyplot import plot, ion, show ion() # enables interactive mode plot([1,2,3]) # result shows immediatelly (implicit draw()) print('continue computation') # at the end call show to ensure window won't close. show()
通过采用这两种方法中的任何一种,都可以分离 Matplotlib 绘图并允许计算在后台进行,同时交互式探索中间结果。
以上是如何在计算过程中使 Matplotlib 绘图具有交互性?的详细内容。更多信息请关注PHP中文网其他相关文章!