简体   繁体   English

与OO Matplotlib的交互式人物

[英]Interactive figure with OO Matplotlib

Using Matplotlib via the OO API is easy enough for a non-interactive backend: 通过OO API使用Matplotlib对于非交互式后端来说非常简单:

 from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
 from matplotlib.figure import Figure

 fig = Figure()
 canvas = FigureCanvas(fig)
 ax = fig.add_subplot(1,1,1)
 ax.plot([1,2,3])
 canvas.print_figure('test.png')

But if I try and repeat something similar with interactive backends, I fail miserably (I can't even get the interactive figure to appear in the first place). 但是,如果我尝试用交互式后端重复类似的东西,我会悲惨地失败(我甚至无法让交互式人物出现在第一位)。 Does anyone have any examples of using Matplotlib via the OO API to create interactive figures? 有没有人有任何通过OO API使用Matplotlib来创建交互式数据的例子?

Well, you need to be using a backend that supports interaction! 好吧,你需要使用支持交互的后端!

backend_agg is not interactive. backend_agg不是交互式的。 backend_tkagg (or one of the other similar backends) is. backend_tkagg (或其他类似的后端之一)是。

Once you're using an interactive backend, you need to do something more like this: 一旦使用了交互式后端,就需要做更多这样的事情:

import matplotlib.backends.backend_tkagg as backend
from matplotlib.figure import Figure

manager = backend.new_figure_manager(0)
fig = manager.canvas.figure
ax = fig.add_subplot(1,1,1)
ax.plot([1,2,3])
fig.show()
backend.show()

Honestly, though, this is not the way to use the oo interface. 但老实说,这不是使用oo界面的方法。 If you're going to need interactive plots, do something more like this: 如果您需要交互式图,请执行以下操作:

import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.plot([1,2,3])
plt.show()

You're still using the oo interface, you're just letting pyplot handle creating the figure manager and enter the gui mainloop for you. 你还在使用oo界面,你只是让pyplot处理创建图形管理器并为你输入gui mainloop。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM