-
Notifications
You must be signed in to change notification settings - Fork 54
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Qt widget with matplotlib API #30
Comments
GR fully supports Qt4 and Qt5 - there are several examples in the GR source distribution. I don't see any problem doing similar thing with Matplotlib when using GR's backend. We will try to provide an example next week ... |
This is a very simple PyQt4 + Matplotlib + GR backend example: #!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
GR / PyQt4 interoperability example
"""
import sys
import os
from PyQt4 import QtCore, QtGui
import sip
os.environ["GKS_WSTYPE"] = "381"
os.environ["MPLBACKEND"] = "module://gr.matplotlib.backend_gr"
import gr
from gr.pygr import *
gr.setregenflags(gr.MPL_POSTPONE_UPDATE)
import numpy as np
import matplotlib.pyplot as plt
class GrWidget(QtGui.QWidget) :
def __init__(self, *args) :
QtGui.QWidget.__init__(self)
self.setupUi(self)
self.connect(self.DrawButton, QtCore.SIGNAL("clicked()"), self.draw)
self.connect(self.QuitButton, QtCore.SIGNAL("clicked()"), self.quit)
self.w = 500
self.h = 500
self.sizex = 1
self.sizey = 1
def setupUi(self, Form) :
Form.setWindowTitle("GrWidget")
Form.resize(QtCore.QSize(500, 500).expandedTo(Form.minimumSizeHint()))
self.DrawButton = QtGui.QPushButton(Form)
self.DrawButton.setText("Draw")
self.DrawButton.setGeometry(QtCore.QRect(290, 5, 100, 25))
self.DrawButton.setObjectName("draw")
self.QuitButton = QtGui.QPushButton(Form)
self.QuitButton.setText("Quit")
self.QuitButton.setGeometry(QtCore.QRect(395, 5, 100, 25))
self.QuitButton.setObjectName("quit")
QtCore.QMetaObject.connectSlotsByName(Form)
def quit(self) :
gr.emergencyclosegks()
self.close()
def draw(self) :
self.setStyleSheet("background-color:white;");
plt.cla()
mu, sigma = 100, 15
x = mu + sigma * np.random.randn(10000)
# the histogram of the data
n, bins, patches = plt.hist(x, 50, normed=1, facecolor='g', alpha=0.75)
plt.xlabel('Smarts')
plt.ylabel('Probability')
plt.title('Histogram of IQ')
plt.axis([40, 160, 0, 0.03])
plt.show()
self.update()
def resizeEvent(self, event):
self.w = event.size().width()
self.h = event.size().height()
if self.w > self.h:
self.sizex = 1
self.sizey = float(self.h)/self.w
else:
self.sizex = float(self.w)/self.h
self.sizey = 1
self.draw()
def paintEvent(self, ev) :
self.painter = QtGui.QPainter()
self.painter.begin(self)
self.painter.drawText(15, 15, "Contour Example using PyQt4 ...")
os.environ['GKSconid'] = "%x!%x" % (sip.unwrapinstance(self), sip.unwrapinstance(self.painter))
gr.updatews()
self.painter.end()
app = QtGui.QApplication(sys.argv)
if not sys.platform == "linux2" :
app.setStyle('Windows')
w = GrWidget()
w.show()
sys.exit(app.exec_()) |
Thank you for your quick reply. However, the example currently segfaults for me:
(gr 0.23.0 and matplotlib 2.0 installed with pip on Python 3.6, Arch Linux.) Moreover, it is not clear from the example whether this would work for multiple figures simultaneously, as you don't specify an explicit Figure/Axes object, relying on pyplot's notion of "current figure/axes" (I would have tried if I could...). However, trying |
.. I only tested on macOS X. Will do further test tomorrow ... (when I'm back in office) |
This code can further be reduced using the #!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
GR / PyQt4 interoperability example
"""
import sys
import os
from PyQt4 import QtCore, QtGui
os.environ["GKS_WSTYPE"] = "381"
os.environ["MPLBACKEND"] = "module://gr.matplotlib.backend_gr"
import numpy as np
import matplotlib.pyplot as plt
from gr.pygr import *
gr.setregenflags(gr.MPL_POSTPONE_UPDATE)
from qtgr import GRWidget
class GrWidget(GRWidget) :
def __init__(self, *args, **kwargs) :
GRWidget.__init__(self, *args, **kwargs)
self.setupUi(self)
self.connect(self.DrawButton, QtCore.SIGNAL("clicked()"), self.update)
self.connect(self.QuitButton, QtCore.SIGNAL("clicked()"), self.close)
def setupUi(self, Form) :
Form.setWindowTitle("GrWidget")
Form.resize(QtCore.QSize(500, 500).expandedTo(Form.minimumSizeHint()))
self.DrawButton = QtGui.QPushButton(Form)
self.DrawButton.setText("Draw")
self.DrawButton.setGeometry(QtCore.QRect(290, 5, 100, 25))
self.DrawButton.setObjectName("draw")
self.QuitButton = QtGui.QPushButton(Form)
self.QuitButton.setText("Quit")
self.QuitButton.setGeometry(QtCore.QRect(395, 5, 100, 25))
self.QuitButton.setObjectName("quit")
QtCore.QMetaObject.connectSlotsByName(Form)
def draw(self) :
plt.cla()
mu, sigma = 100, 15
x = mu + sigma * np.random.randn(10000)
# the histogram of the data
plt.hist(x, 50, normed=1, facecolor='g', alpha=0.75)
plt.xlabel('Smarts')
plt.ylabel('Probability')
plt.title('Histogram of IQ')
plt.axis([40, 160, 0, 0.03])
plt.show()
app = QtGui.QApplication(sys.argv)
if not sys.platform == "linux2" :
app.setStyle('Windows')
w = GrWidget()
w.show()
sys.exit(app.exec_()) |
@cfelder : Thanks - my script was just a Sunday morning hack ... |
We are closing this issue due to inactivity. Please reopen the issue and let us know if its cause still persists with the current version of GR. |
Apologies for resurrecting a dead thread (and if this has already been addressed somewhere), but hopefully this issue fits within the original scope. Like the original question, I'm looking to use GR as a matplotlib backend, but also as a Qt widget (to speed up the plotting I'm doing in my program). Currently, I am adding a FigureCanvas instance from the matplotlib qt5agg backend as a widget to my Qt layout. I see there is a FigureCanvas class in the GR backend, but this FigureCanvas is of type FigureCanvasGR and not recognized by Qt. Should this, perhaps, be wrapped in a QtGR class so it can be displayed in a Qt layout? |
That sounds like it could be done by combining the Ideally open a new issue in the python-gr repository for this. |
Thanks - will open a new issue there. |
gr can currently be used as a matplotlib backend and as a Qt widget, but could it be possible to use both simultaneously? Namely, have a Qt widget (say, of MatplotlibGRWidget class), which would have a
.figure
attribute, that would be amatplotlib.figure.Figure
.The text was updated successfully, but these errors were encountered: