簡體   English   中英

在PyQt中使用matplotlib圖

[英]use a matplotlib Figure in PyQt

在這里編程菜鳥。 我正在嘗試在PyQt4 GUI中使用matplotlib小部件。 該小部件類似於matplotlib的qt示例

在某些時候,用戶需要單擊該圖,我認為類似ginput()的東西可以處理。 但是,這不起作用,因為該人物沒有經理(請參見下文)。 請注意,這與另一個問題非常相似,但從未得到解答。

AttributeError: 'NoneType' object has no attribute 'manager'
Figure.show works only for figures managed by pyplot, normally created by pyplot.figure().

我假設通過“正常”可以解決此問題。

另一個簡單的腳本演示:

from __future__ import print_function

from matplotlib.figure import Figure
import numpy as np
import matplotlib.pyplot as plt

x = np.arange(0, 5, 0.1)
y = np.sin(x)
# figure creation by plt (also given a manager, although not explicitly)
plt.figure()
plt.plot(x,y)
coords = plt.ginput() # click on the axes somewhere; this works
print(coords)

# figure creation w/o plt
manualfig = Figure()
manualaxes = manualfig.add_subplot(111)
manualaxes.plot(x,y)
manualfig.show() # will fail because of no manager, yet shown as a method
manualcoords = manualfig.ginput() # comment out above and this fails too
print(manualcoords)

像pyplot一樣流行(沒有它我幾乎找不到答案),在使用GUI時,它似乎不能很好地發揮作用。 我認為pyplot只是OO框架的包裝,但我想我只是菜鳥。

然后我的問題是:是否可以將pyplot附加到matplotlib.figure.Figure的實例? 有沒有簡單的方法可以將經理附加到人物上? 我在matplotlib.backends.backend_qt4agg中找到了new_figure_manager(),但即使它是正確的解決方案,也無法使它正常工作。

非常感謝,

詹姆士

pyplot只是OO接口的包裝器,但是當您仔細閱讀鏈接到的示例時,它做了很多工作,

FigureCanvas.__init__(self, fig)

線條非常重要,因為這就是告訴圖形要使用哪種畫布的原因。 Figure對象只是Axes對象(和一些Text對象)的集合, canvas對象是知道如何將Artist對象(即matplotlib的線條,文本,點等的內部表示)轉換為漂亮顏色的對象。 另請參閱為另一個嵌入示例編寫的內容 ,該示例未將FigureCanvas子類FigureCanvas

有一個PR使這個過程更容易,但是當我們得到1.4時它就停滯了。

另請參見: 推薦的哪種繪制方式:matplotlib或pylab? 如何將pyplot函數附加到圖形實例?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM