Matplotlib interactive graph embedded in PyQt

2024/11/17 18:43:51

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:

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)

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()
Answer

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

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

To run the program

python pyStocker.py

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.

https://en.xdnf.cn/q/71161.html

Related Q&A

How to pass path names to Python script by dropping files/folders over script icon

I am working in Mac OS X and have been writing simple file/folder copy scripts in Python. Is there a way to drag and drop a folder on top of a Python script icon and pass the file or folders path as an…

Why wouldnt I want to add Python.exe to my System Path at install time?

Im reinstalling Python, on Windows 7, and one of the first dialog boxes is the Customize Python screen.The default setting for "Add Python.exe to Path" is "Entire feature will be unavail…

How to install python-gtk2, python-webkit and python-jswebkit on OSX

Ive read through many of the related questions but am still unclear how to do this as there are many software combinations available and many solutions seem outdated.What is the best way to install the…

Forking python, defunct child

I have some troubles with Python child processes so I wrote a very simple script:import os import sys import timepid = os.fork() if pid:#parenttime.sleep(30) else:#child#os._exit(0)sys.exit()While pare…

is there a way to know the length of a bytearray variable in python?

I have this code:variable = "FFFF" message = bytearray( variable.decode("hex") )after this, I want to perform something like this:message.len()but it seems that bytearray does not h…

Matplotlib: Check for empty plot

I have a loop which loads and plots some data, something like this:import os import numpy as np import matplotlib.pyplot as pltfor filename in filenames:plt.figure()if os.path.exists(filename):x, y = n…

Hyperlink in Streamlit dataframe

I am attempting to display a clickable hyperlink inside a dataframe containing filtered results on Streamlit. This is my code so far: import pandas as pd import streamlit as st import openpyxl import n…

Best way to detect if checkbox is ticked

My work:Scan the paper Check horizontal and vertical line Detect checkbox How to know checkbox is ticked or notAt this point, I thought I could find it by using Hierarchical and Contours: Below is my w…

ResultSet object has no attribute find_all

i always met one problem, when I scraping one web page.AttributeError: ResultSet object has no attribute find. Youre probably treating a list of items like a single item. Did you call find_all() when y…

How can I set path to load data from CSV file into PostgreSQL database in Docker container?

I would like to load data from CSV file into PostgreSQL database in Docker. I run:docker exec -ti my project_db_1 psql -U postgresThen I select my database:\c myDatabaseNow I try to load data from myfi…