Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting PySide to work with matplotlib

Tags:

I have tried running the example code on the SciPy website, but I get this error:

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) 

I am building a simple scientific data logger that will eventually be used in commercial applications, so I really need both the LGPL from PySide and plotting functionality. Does anyone have experience on how to get this to work or alternative plotting packages or ideas?

Thanks in advance.

like image 224
jonathanbsyd Avatar asked Jul 17 '11 11:07

jonathanbsyd


People also ask

Can I use Plotly with matplotlib?

No, Plotly does not use Matplotlib. In other words, Matplotlib is not a dependency for plotly. While both packages allow you to visualize data, they do it differently. To get both Matplotlib and Plotly, download the “Data Plotting” Python runtime.

Does matplotlib use Qt?

Simple Qt application embedding Matplotlib canvases. This program will work equally well using any Qt binding (PyQt6, PySide6, PyQt5, PySide2). The binding can be selected by setting the QT_API environment variable to the binding name, or by first importing it.

What is the difference between matplotlib Pyplot and matplotlib?

The matplotlib. pyplot is a collection of functions that make matplotlib work like MATLAB. Each pyplot function makes some change to a figure: e.g., creates a figure, creates a plotting area in a figure, plots some lines in a plotting area, decorates the plot with labels, etc.

Do You Need %Matplotlib inline?

So %matplotlib inline is only necessary to register this function so that it displays in the output. Running import matplotlib. pyplot as plt also registers this same function, so as of now it's not necessary to even use %matplotlib inline if you use pyplot or a library that imports pyplot like pandas or seaborn.


2 Answers

The example that you mention:

http://www.scipy.org/Cookbook/Matplotlib/PySide

works, but you might need to suggest the use of PySide:

... matplotlib.use('Qt4Agg') matplotlib.rcParams['backend.qt4']='PySide' import pylab ... 
like image 166
dsign Avatar answered Oct 17 '22 06:10

dsign


I had similar goals (LGPL, potential commercial use) and here's how I ended up getting it to work.

Create a matplotlib widget (see here for a more detailed one for PyQt):

import matplotlib  matplotlib.use('Qt4Agg') matplotlib.rcParams['backend.qt4']='PySide'  from matplotlib.figure import Figure from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas  class MatplotlibWidget(FigureCanvas):      def __init__(self, parent=None,xlabel='x',ylabel='y',title='Title'):         super(MatplotlibWidget, self).__init__(Figure())          self.setParent(parent)         self.figure = Figure()         self.canvas = FigureCanvas(self.figure)         self.axes = self.figure.add_subplot(111)          self.axes.set_xlabel(xlabel)         self.axes.set_ylabel(ylabel)         self.axes.set_title(title) 

In Qt Designer I created a blank widget to hold my plot and then when I __init__ the main window I call 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) 

Then I call plotDataPoints as needed:

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

Note: this clears and redraws the entire plot every time (since the shape of my data keeps changing) and so isn't fast.

like image 34
akehrer Avatar answered Oct 17 '22 06:10

akehrer