Has anyone created Matplotlib widgets? #104
-
I'd like to re-use or modify them for something I'm trying out. I'm a foreigner when it comes to QT. Looking for a way to jump into making a bespoke plotting widget without all the experimentaiton. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
I actually did some tests a long time ago, so I just recycled some of that old code. Here is a tiny example that should include most code needed to get it running with Qt:
from ryven.NWENV import *
from qtpy.QtWidgets import QWidget, QVBoxLayout
import matplotlib
import matplotlib.pyplot as plt
matplotlib.use('Qt5Agg')
from matplotlib.backends.backend_qt5agg import FigureCanvas, NavigationToolbar2QT as NavigationToolbar
from matplotlib.figure import Figure
class PlotWidget(MWB, QWidget):
def __init__(self, params):
MWB.__init__(self, params)
QWidget.__init__(self)
# Matplotlib
self.canvas = FigureCanvas(Figure(figsize=(5, 3)))
self.ax = self.canvas.figure.subplots()
# Qt
self.setLayout(QVBoxLayout())
self.layout().addWidget(self.canvas)
self.setFixedHeight(300)
def redraw(self, x, functions):
self.ax.clear()
for f in functions.keys():
self.ax.plot(x, functions[f], label=f)
self.ax.figure.canvas.draw()
# ------------------------------------------------------------------------------
from ryven.NENV import *
class MatplotlibNode(Node):
title = 'Matplotlib'
main_widget_class = PlotWidget
main_widget_pos = 'below ports'
def update_event(self, inp=-1):
self.main_widget().redraw(range(10), {'x^2': [x**2 for x in range(10)]})
def view_place_event(self):
self.update()
export_nodes(
MatplotlibNode,
) To test it put it in a new nodes package, import it and place the node, it should show the graph of x^2. |
Beta Was this translation helpful? Give feedback.
-
Got it working. It's coming together nicely! Apr-04-2022.21-49-22.mp4 |
Beta Was this translation helpful? Give feedback.
I actually did some tests a long time ago, so I just recycled some of that old code. Here is a tiny example that should include most code needed to get it running with Qt:
nodes.py