Matplotlib Qt4 GUI 编程 - 将 plt.figure() 替换为 OO 等价物
Matplotlib Qt4 GUI programming - replace plt.figure() with OO equivalent
我有一个使用 Qt4 Designer 制作的应用程序,它将 matplotlib 图插入到容器小部件中。
生成图形的代码来自另一个模块obspy:
self.st.plot(fig = self.rawDataPlot)
https://docs.obspy.org/packages/autogen/obspy.core.stream.Stream.plot.html
通常,这会为 st
对象的数据创建并显示一个 matplotlib 图,它是时间序列。当指定 fig
参数时,这会告诉 self.st.plot
绘制到现有的 matplotlib 图形实例。
我必须生成图形然后将其放置在我的 GUI 小部件中的代码是:
def addmpl(self, fig, layout, window): # code to add mpl figure to Qt4 widget
self.canvas = FigureCanvas(fig)
layout.addWidget(self.canvas)
self.canvas.draw()
self.toolbar = NavigationToolbar(self.canvas,
window, coordinates=True)
layout.addWidget(self.toolbar)
self.rawDataPlot = plt.figure() # code to create a mpl figure instance
self.st.plot(fig = self.rawDataPlot) # plot time-series data to existing matplotlib figure instance
self.addmpl(self.rawDataPlot, self.mplvl, self.mplwindow) # add mpl figure to Qt4 widget
我想做的是实例化一个 matplot 图(供 self.st.plot
使用),但在某种程度上避免使用 plt.figure()
,正如我所读到的那样,这在使用对象时是不好的做法面向编程。
如果我将 plt.figure()
替换为 Figure()
(来自 matplotlib.figure.Figure()),我会得到一个错误:
AttributeError: 'NoneType' object has no attribute 'draw'
就目前而言,如果我使用 plt.figure(),该应用程序运行良好,但是否有一种干净的方法来避免使用 is 并且它对我的情况是否必要?
PS,这里的代码片段取自一个更大的来源,但我认为它明白了要点..
原则上这两种方法都应该有效。
假设导入是正确的,无论你设置 self.rawDataPlot = plt.figure()
还是 self.rawDataPlot = Figure()
都没有太大的区别。
所以错误很可能是在 self.st.plot()
函数中触发的。 (一般情况下,如果报错,追加traceback。)
查看 obspy.core.stream.Stream.plot
的来源有一个关键字参数
:param draw:
If True, the figure canvas is explicitly re-drawn, which
ensures that existing figures are fresh. It makes no difference
for figures that are not yet visible.
Defaults to True
.
这意味着 plot 函数显然试图绘制 canvas,在提供 Figure()
的情况下尚未设置。
因此,一个好的猜测是调用
self.rawDataPlot = Figure()
self.st.plot(fig = self.rawDataPlot, draw=False)
看看问题是否仍然存在。
我有一个使用 Qt4 Designer 制作的应用程序,它将 matplotlib 图插入到容器小部件中。
生成图形的代码来自另一个模块obspy:
self.st.plot(fig = self.rawDataPlot)
https://docs.obspy.org/packages/autogen/obspy.core.stream.Stream.plot.html
通常,这会为 st
对象的数据创建并显示一个 matplotlib 图,它是时间序列。当指定 fig
参数时,这会告诉 self.st.plot
绘制到现有的 matplotlib 图形实例。
我必须生成图形然后将其放置在我的 GUI 小部件中的代码是:
def addmpl(self, fig, layout, window): # code to add mpl figure to Qt4 widget
self.canvas = FigureCanvas(fig)
layout.addWidget(self.canvas)
self.canvas.draw()
self.toolbar = NavigationToolbar(self.canvas,
window, coordinates=True)
layout.addWidget(self.toolbar)
self.rawDataPlot = plt.figure() # code to create a mpl figure instance
self.st.plot(fig = self.rawDataPlot) # plot time-series data to existing matplotlib figure instance
self.addmpl(self.rawDataPlot, self.mplvl, self.mplwindow) # add mpl figure to Qt4 widget
我想做的是实例化一个 matplot 图(供 self.st.plot
使用),但在某种程度上避免使用 plt.figure()
,正如我所读到的那样,这在使用对象时是不好的做法面向编程。
如果我将 plt.figure()
替换为 Figure()
(来自 matplotlib.figure.Figure()),我会得到一个错误:
AttributeError: 'NoneType' object has no attribute 'draw'
就目前而言,如果我使用 plt.figure(),该应用程序运行良好,但是否有一种干净的方法来避免使用 is 并且它对我的情况是否必要?
PS,这里的代码片段取自一个更大的来源,但我认为它明白了要点..
原则上这两种方法都应该有效。
假设导入是正确的,无论你设置 self.rawDataPlot = plt.figure()
还是 self.rawDataPlot = Figure()
都没有太大的区别。
所以错误很可能是在 self.st.plot()
函数中触发的。 (一般情况下,如果报错,追加traceback。)
查看 obspy.core.stream.Stream.plot
的来源有一个关键字参数
:param draw:
If True, the figure canvas is explicitly re-drawn, which ensures that existing figures are fresh. It makes no difference for figures that are not yet visible. Defaults toTrue
.
这意味着 plot 函数显然试图绘制 canvas,在提供 Figure()
的情况下尚未设置。
因此,一个好的猜测是调用
self.rawDataPlot = Figure()
self.st.plot(fig = self.rawDataPlot, draw=False)
看看问题是否仍然存在。