Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 让PySide使用matplotlib_Python_Matplotlib_Pyside - Fatal编程技术网

Python 让PySide使用matplotlib

Python 让PySide使用matplotlib,python,matplotlib,pyside,Python,Matplotlib,Pyside,我已尝试运行,但出现以下错误: Traceback (most recent call last): File ".\matplotlibPySide.py", line 24, in <module> win.setCentralWidget(canvas) TypeError: 'PySide.QtGui.QMainWindow.setCentralWidget' called with wrong argument types: PySide.QtGui.QMa

我已尝试运行,但出现以下错误:

Traceback (most recent call last):
  File ".\matplotlibPySide.py", line 24, in <module>
    win.setCentralWidget(canvas)
TypeError: 'PySide.QtGui.QMainWindow.setCentralWidget' called with wrong argument types:
  PySide.QtGui.QMainWindow.setCentralWidget(FigureCanvasQTAgg)
Supported signatures:
  PySide.QtGui.QMainWindow.setCentralWidget(PySide.QtGui.QWidget)
回溯(最近一次呼叫最后一次):
文件“\matplotlibPySide.py”,第24行,在
win.setCentralWidget(画布)
TypeError:“PySide.QtGui.QMainWindow.setCentralWidget”调用时参数类型错误:
PySide.QtGui.QMainWindow.setCentralWidget(图CANVASQTAGG)
支持的签名:
PySide.QtGui.QMainWindow.setCentralWidget(PySide.QtGui.QWidget)
我正在构建一个简单的科学数据记录器,最终将用于商业应用,因此我确实需要PySide的LGPL和绘图功能。有没有人有过如何让这项工作或其他绘图包或想法的经验


提前谢谢。

我想你可能已经在matplotlib邮件列表上发布了这篇文章。但以防万一有人在寻找答案。最好的选择是使用Github上的主分支,但如果您不能或不知道如何使用Github版本,则可以使用以下代码在PySide中呈现绘图

import numpy as np
from matplotlib import use
use('AGG')
from matplotlib.transforms import Bbox
from matplotlib.path import Path
from matplotlib.patches import Rectangle
from matplotlib.pylab import *
from PySide import QtCore,QtGui

rect = Rectangle((-1, -1), 2, 2, facecolor="#aaaaaa")
gca().add_patch(rect)
bbox = Bbox.from_bounds(-1, -1, 2, 2)

for i in range(12):
    vertices = (np.random.random((4, 2)) - 0.5) * 6.0
    vertices = np.ma.masked_array(vertices, [[False, False], [True, True], [False, False], [False, False]])
    path = Path(vertices)
    if path.intersects_bbox(bbox):
        color = 'r'
    else:
        color = 'b'
    plot(vertices[:,0], vertices[:,1], color=color)

app = QtGui.QApplication(sys.argv)
gcf().canvas.draw()

stringBuffer = gcf().canvas.buffer_rgba(0,0)
l, b, w, h = gcf().bbox.bounds

qImage = QtGui.QImage(stringBuffer, 
                      w,
                      h,
                      QtGui.QImage.Format_ARGB32)

scene = QtGui.QGraphicsScene()
view = QtGui.QGraphicsView(scene)
pixmap = QtGui.QPixmap.fromImage(qImage)
pixmapItem = QtGui.QGraphicsPixmapItem(pixmap)
scene.addItem(pixmapItem)
view.show()

app.exec_()

你提到的例子是:

有效,但您可能需要建议使用PySide:

...
matplotlib.use('Qt4Agg')
matplotlib.rcParams['backend.qt4']='PySide'
import pylab
...
我也有类似的目标(LGPL,潜在的商业用途),下面是我如何让它发挥作用的

创建matplotlib小部件(有关PyQt的更详细信息,请参阅):

在Qt Designer中,我创建了一个空白小部件来保存我的绘图,然后当我
\uuuu init\uuuu
主窗口时,我调用setupPlot:

def  setupPlot(self):
    # create a matplotlib widget
    self.DataPlot = MatplotlibWidget()
    # create a layout inside the blank widget and add the matplotlib widget        
    layout = QtGui.QVBoxLayout(self.ui.widget_PlotArea)        
    layout.addWidget(self.DataPlot,1)
然后根据需要调用plotDataPoints:

def plotDataPoints(self,x,y):        
    self.DataPlot.axes.clear()
    self.DataPlot.axes.plot(x,y,'bo-')
    self.DataPlot.draw()

注意:每次都会清除并重新绘制整个绘图(因为我的数据形状不断变化),因此速度不快。

请尝试跳过此处的示例代码,并使用最新的matplotlib git提示进行尝试。(请注意,您需要具有各种构建依赖项。它不是纯python。)一个月前,qt后端添加了完整的PySide支持,但尚未发布。您喜欢的代码只是一种变通方法。matplotlib的较新版本将完全支持pyside。在本例中,为什么要导入pylab?如果没有这一行,就可以正常工作。@JuanPablo不需要导入pylab,但我认为在实际导入pylab或pyplot之前,您需要尽可能多地配置Matplotlib。。。苏尔特!
def plotDataPoints(self,x,y):        
    self.DataPlot.axes.clear()
    self.DataPlot.axes.plot(x,y,'bo-')
    self.DataPlot.draw()