pyqtgraph not compatible with matplotlib

865 views
Skip to first unread message

Jianbao Tao

unread,
Oct 9, 2012, 1:27:35 AM10/9/12
to pyqt...@googlegroups.com
Hi,

I am playing with pyqtgraph recently. I am impressed by its speed. :-) However, there is one thing that really bothers me: It doesn't appear  to be compatible with matplotlib, which is very strange, considering the fact that pyqtgraph and matplotlib have their own name space. Here is a snippet that demonstrates that problem.

#--------------------- code ----------------------------------
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(100)
plt.plot(x)  # Working
plt.show()

import pyqtgraph
plt.plot(x)  # Not working
#----------------------- end of code -------------------------

Cheers,
Jianbao

Jianbao Tao

unread,
Oct 9, 2012, 1:33:42 AM10/9/12
to pyqt...@googlegroups.com
I forgot to mention my platform.

pyqtgraph version: pyqtgraph-dev-r201.tar.gz
OS: Mountain lion (OS X 10.8.2)
Python: 2.7.2 (apple version)
matplotlib: 1.2.0rc2

Let me know if you need more information to diagnose the problem.

Jianbao

Luke Campagnola

unread,
Oct 9, 2012, 3:25:40 AM10/9/12
to pyqt...@googlegroups.com
I bet the problem is that you can't use two different GUI libraries in the same application. In this case, pyplot probably creates a window with TkInter, which prevents Qt from loading properly. Instead, use matplotlib's Qt backend to ensure that the two libraries play nicely together.  
(see pyqtgraph/widgets/MatplotlibWidget.py )


Luke

Jianbao Tao

unread,
Oct 9, 2012, 11:43:50 AM10/9/12
to pyqt...@googlegroups.com
Luke,

With all due respect, your bet is wrong. I used the Qt4Agg for matplotlib backend, and the same problem is still there. Besides, in the example code, it is not matplotlib that makes pyqtgraph not working. It is pyqtgraph that makes matplotlib not working. So, your argument doesn't really make too much sense to me.

Jianbao

-- [ You are subscribed to pyqt...@googlegroups.com. To unsubscribe, send email to pyqtgraph+...@googlegroups.com ]

Luke Campagnola

unread,
Oct 9, 2012, 8:49:11 PM10/9/12
to pyqt...@googlegroups.com
On Tue, Oct 9, 2012 at 11:43 AM, Jianbao Tao <jianb...@gmail.com> wrote:
With all due respect, your bet is wrong. I used the Qt4Agg for matplotlib backend, and the same problem is still there. Besides, in the example code, it is not matplotlib that makes pyqtgraph not working. It is pyqtgraph that makes matplotlib not working. So, your argument doesn't really make too much sense to me.

Sorry, I did not read your question carefully enough. You are absolutely correct--pyqtgraph has a wrapper function that it installs around numpy.concatenate as a workaround for a segmentation fault bug in numpy.  My implementation of that wrapper is not quite correct, and causes matplotlib to stop working. 

This is fixed in the latest release.

Thanks!
Luke

 

Jianbao Tao

unread,
Oct 10, 2012, 2:17:06 PM10/10/12
to pyqt...@googlegroups.com
Thanks for following up on this, Luke. I will try the release later.

Jianbao


-- [ You are subscribed to pyqt...@googlegroups.com. To unsubscribe, send email to pyqtgraph+...@googlegroups.com ]

ML___

unread,
May 17, 2022, 4:20:45 PM5/17/22
to pyqtgraph
Hello,

Thanks to all those who've worked on PyQtGraph.  I switched to it for a real time plotting app as I was bogging down Matplotlib. 
It's easy to use, highly flexible, and fast!

I'm seeing a similar issue as was reported in 2012, where importing pyqtgraph stops matplotlib from showing plots:

--------------------my code------------------------------
import matplotlib.pyplot as plt
plt.ion()
plt.show()
plt.plot([1,2,3,4])    # 1) call succeeds, interactive plot window is shown
import pyqtgraph  # 2) after this call the existing plot window from (1) is unresponsive
plt.plot([4,3,2,1])    # 3) function succeeds as in (1), but plot window is not updated, and remains unresponsive
-----------------------------------------------------------------

I'm running Debian Testing (bookworm) recently updated
Python 3.10.4
pyqtgraph.__version__  = 0.12.4
matplotlib.__version__  = 3.5.1

Regards,
Michael

Ognyan Moore

unread,
May 17, 2022, 5:16:23 PM5/17/22
to pyqt...@googlegroups.com
The only support that pyqtgraph has with matplotlib is that you can export windows to a matplotlib window, there is a little more in the documentation here: https://pyqtgraph.readthedocs.io/en/latest/exporting.html#export-formats

Trying to use both matplotlib and pyqtgraph at the same time will likely lead to unintentionally bad behavior from some issues w/ QApplication I would imagine.  If you specify a non Qt back-end of matplotlib, it might work, but this is fairly untested, and I would generally not recommend.

--
You received this message because you are subscribed to the Google Groups "pyqtgraph" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pyqtgraph+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pyqtgraph/496afc6c-3c4c-44e0-bb28-2f1ef7c67860n%40googlegroups.com.

Patrick

unread,
May 17, 2022, 10:58:44 PM5/17/22
to pyqtgraph
Hi,

I have successfully used both pyqtgraph and matplotlib together in a data processing/analysis project. I used pyqtgraph for the most part, all the interactive plots etc, and then matplotlib for the final step of generating publication-quality plots. It's something I always wanted to open-source but never really sorted out the details for that, but I'll try to summarise here. Perhaps email me directly if you have problems and I may be able to sort out and supply some better code.

Imports look something like:

# Do usual Qt imports, pyqtgraph and matplotlib
from PyQt5 import QtWidgets, uic
import pyqtgraph as pg
import matplotlib as mpl
mpl.use('Qt5Agg')
# For matplotlib plots in the GUI
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
# For matplotlib plot exports to pdf
from matplotlib.backends.backend_pdf import FigureCanvasPdf, PdfPages

In relevant UI class, set up a matplotlib FigureCanvas widget to render onto:

self.resultplot_figureCanvas = FigureCanvas(mpl.figure.Figure(constrained_layout=True))
self.resultplots_groupBox.layout().addWidget(self.resultplot_figureCanvas)

In appropriate place, render matplotlib plot to the FigureCanvas instead of letting mpl.pyplot create one for you:

fig = self.resultplot_figureCanvas.figure
fig.clear()
ax = fig.subplots(1, 1)
ax.plot([1, 2, 3], [4, 5, 6])
#...
self.resultplot_figureCanvas.draw()

If you want to render to a pdf, then render to a FigureCanvasPdf something like:

canvas = FigureCanvasPdf(mpl.figure.Figure(constrained_layout=True, figsize=(figwidth, figheight)))
fig = canvas.figure
ax = fig.subplots(1, 1)
ax.plot([1, 2, 3], [4, 5, 6])
# ...
fig.savefig("filename.pdf", bbox_inches="tight", dpi=150, metadata=metadata_pdf)

The application is a little old now (Qt5 etc), but hopefully that might give you something to work with.

Patrick

ML___

unread,
May 20, 2022, 3:10:04 PM5/20/22
to pyqtgraph
Hi Patrick,

Thanks for all the info, will have a look shortly, and report back.

Michael

ML___

unread,
May 22, 2022, 12:28:51 AM5/22/22
to pyqtgraph
Hi,

So I have matplotlib and pyqtgraph coexisting.   Not sure exactly what changed, but had something to do with pyqtgraph auto selecting
pyqt5.  Got my imports in order and now my app works as desired with pyside2, and separate matplotlibs scripts work as well. 

Michael

Ognyan Moore

unread,
May 22, 2022, 11:14:46 AM5/22/22
to pyqt...@googlegroups.com
If you have multiple Qt versions in your environment, you can force pyqtgraph to use a specific binding by setting the PYQTGRAPH_QT_LIB environment variable:


Unfortunately, this functionality isn't documented, but we have no plans in removing it.  Having multiple Qt bindings is an issue for people who install matplotlib using the conda defaults channel (which installs PyQt5 5.9, which pyqtgraph does not support).  A couple of other workarounds, in that case, involve installing matplotlib via pip, or installing the conda-forge variant of matplotlib.

--
You received this message because you are subscribed to the Google Groups "pyqtgraph" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pyqtgraph+...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages