【问题标题】:How to integrate matplotlib fumction with QGraphicView?如何将 matplotlib 函数与 QGraphicsView 集成?
【发布时间】:2017-03-31 20:36:57
【问题描述】:

我正在使用 Qt Designer 和 PyQt4 开发一个桌面应用程序。我需要显示一个带有轮廓的图形,在 python 中可以使用 matplotlib.pyplot.contourf 完成。我想在 QGraphicView 对象中显示结果。

我正在尝试通过将 QGraphicView 提升为 Qt-Designer 中的 pygtgraph 来做到这一点。如果 QGraphicView 的对象名称是 CNOPlot 我已经写了

import matplotlib.pyplot as plt
self.CNOPlot.plot(plt.contourf(xx, yy, Z,cmap=plt.cm.autumn, alpha=0.8))

它在单独的窗口中给出输出。 我希望这是在 CNOPlot 内绘制的。

【问题讨论】:

  • 在您的视图中添加QGraphicsScene 并在场景中渲染您的情节。
  • 非常感谢@C.Dip。你能否举一些例子或任何链接来展示如何做到这一点。我对它完全陌生。
  • 好的,我写一个简短的答案
  • 我也不应该把它推广到pyqtgraph吗?

标签: python qt matplotlib pyqt4 pyqtgraph


【解决方案1】:

这是一个简短的例子:

import matplotlib.pyplot as plt
from PyQt4 import QtGui
from PyQt4.Qt import Qt
from matplotlib.figure import Figure
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas

class MyView(QtGui.QGraphicsView):
    def __init__(self):
        QtGui.QGraphicsView.__init__(self)

        scene = QtGui.QGraphicsScene(self)
        self.scene = scene

        figure = Figure()
        axes = figure.gca()
        axes.set_title("title")
        axes.plot(plt.contourf(xx, yy, Z,cmap=plt.cm.autumn, alpha=0.8))
        canvas = FigureCanvas(figure)
        canvas.setGeometry(0, 0, 500, 500)
        scene.addWidget(canvas)

        self.setScene(scene)

【讨论】:

  • 非常感谢,但它仍然是一个单独的窗口,并带有以下错误跟踪:AttributeError: QuadContourSet instance has no attribute 'float'
  • xxyy 必须是整数数组。
  • 但是按照要求他们不能。
猜你喜欢
  • 1970-01-01
  • 2021-04-04
  • 2017-10-28
  • 2020-08-15
  • 2020-05-18
  • 2016-08-22
  • 1970-01-01
  • 1970-01-01
  • 2013-01-14
相关资源
最近更新 更多