嵌入在PyQt中的Matplotlib交互式图形 [英] Matplotlib interactive graph embedded in PyQt

查看:1618
本文介绍了嵌入在PyQt中的Matplotlib交互式图形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个简单的python脚本,它在运行时应该在PyQT窗口中显示一个嵌入式matplotlib图形。我已经使用这个教程来嵌入和运行图形。除了命名约定和总体UI中的一些差异之外,我的图形与教程中提到的图形完全一样。

I've created a simple python script that when run should display an embedded matplotlib graph inside a PyQT window. I've used this tutorial for embedding and running the graph. Aside from some differences in the naming conventions and in the overall UI my graph is generated exactly as the one in the tutorial mentioned.

我的问题是我想让这是一个允许缩放和拖拽的交互式图形,但我想只用鼠标(点击和拖动,滚轮等)和没有工具栏(因为我觉得它很丑)这样做。

My problem is that I would like to make this an interactive graph that allows for zooming and dragging, but I would like to do this with only the mouse (clicking and dragging, scroll wheel, etc) and without the toolbar (as I find it ugly).

Widget类:

class MplCanvas(FigureCanvas):
    """
    Creates a canvas on which to draw our widgets
    """
    def __init__(self):
        self.fig = Figure()
        self.ax = self.fig.add_subplot(111)

        FigureCanvas.__init__(self, self.fig)
        FigureCanvas.setSizePolicy(self, QtGui.QSizePolicy.Expanding,QtGui.QSizePolicy.Expanding)
        FigureCanvas.updateGeometry(self)

class marketdephWidget(QtGui.QWidget):
    """
    The market deph graph
    """
    def __init__(self, parent = None):
        QtGui.QWidget.__init__(self, parent)
        self.canvas = MplCanvas()
        self.vbl = QtGui.QVBoxLayout()
        self.vbl.addWidget(self.canvas)
        self.setLayout(self.vbl)


$ b

Plotting function (run on button press):

# initialize the mplwidgets
def PlotFunc(self):
    randomNumbers = random.sample(range(0, 10), 10)
    self.ui.widget.canvas.ax.clear()
    self.ui.widget.canvas.ax.plot(randomNumbers)
    self.ui.widget.canvas.draw()


推荐答案

已经有一段时间了,但我正在研究类似的问题。我在这里有示例。也许这会有所帮助。

It's been a while but I was working on a similar problem. I have an example here. Maybe this will help.

该程序使用 PySide ,不应该成为问题。

The program is using PySide, shouldn't be a problem though.

运行程序

To run the program

python pyStocker.py

您会看到一个小部件弹出窗口,在左上角您会看到 TextEdit 框。只需输入GOOG或其他有效的股票代码,然后按回车。图表将填充股票的数据。底部的滑动条实时更改图形。

You would see a widget popup and in the top left you'll see a TextEdit box. Just enter GOOG or some other valid stock symbol and press enter. The graph will populate with the stock's data. The slider bar at the bottom changes the graph real-time.

这篇关于嵌入在PyQt中的Matplotlib交互式图形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆