turning grid on with AxisItem in pyqtgraph causes axis scaling to break - python

I am having trouble with AxisItem. As soon as I turn on both the x and y grid, the x-axis is no longer able to scale in and out with the zoom/pan function. Any ideas?
from PyQt4 import QtCore, QtGui
from pyqtgraph import Point
import pyqtgraph as pg
pg.setConfigOption('background', 'w')
pg.setConfigOption('foreground', 'k')
class plotClass(QtGui.QMainWindow):
def setupUi(self, MainWindow):
self.centralwidget = QtGui.QWidget(MainWindow)
MainWindow.resize(1900, 1000)
self.viewbox = pg.GraphicsView(MainWindow, useOpenGL=None, background='default')
self.viewbox.setGeometry(QtCore.QRect(0, 0, 1600, 1000))
self.layout = pg.GraphicsLayout()
self.viewbox.setCentralWidget(self.layout)
self.viewbox.show()
self.view = self.layout.addViewBox()
self.axis1 = pg.AxisItem('bottom', linkView=self.view, parent=self.layout)
self.axis2 = pg.AxisItem('right', linkView=self.view, parent=self.layout)
self.axis1.setGrid(255)
self.axis2.setGrid(255)
self.layout.addItem(self.axis1, row=1, col=0)
self.layout.addItem(self.axis2, row=0, col=1)
if __name__== "__main__":
import sys
app = QtGui.QApplication(sys.argv)
MainWindow = QtGui.QMainWindow()
ui = plotClass()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())

Looking at your last comment, consider this option:
The pyqtgraph examples folder contains a "GraphItem.py" example which adds and displays a GraphItem object to a window via a ViewBox only. They don't use a grid however, so if you want to use a grid with a GraphItem, just add a PlotItem first (which has an associated ViewBox already... and you guessed it,...AxisItems for a grid!),... then get the ViewBox to add your GraphItems. The modified GraphItem.py would look like this (with the accompanying showGrid):
import pyqtgraph as pg
from pyqtgraph.Qt import QtCore, QtGui
import numpy as np
# Enable antialiasing for prettier plots
pg.setConfigOptions(antialias=True)
w = pg.GraphicsWindow()
w.setWindowTitle('pyqtgraph example: GraphItem')
### comment out their add of the viewbox
### since the PlotItem we're adding will have it's
### own ViewBox
#v = w.addViewBox()
pItem1 = w.addPlot() # this is our new PlotItem
v = pItem1.getViewBox() # get the PlotItem's ViewBox
v.setAspectLocked() # same as before
g = pg.GraphItem() # same as before
v.addItem(g) # same as before
pItem1.showGrid(x=True,y=True) # now we can turn on the grid
### remaining code is the same as their example
## Define positions of nodes
pos = np.array([
[0,0],
[10,0],
[0,10],
[10,10],
[5,5],
[15,5]
])
## Define the set of connections in the graph
adj = np.array([
[0,1],
[1,3],
[3,2],
[2,0],
[1,5],
[3,5],
])
## Define the symbol to use for each node (this is optional)
symbols = ['o','o','o','o','t','+']
## Define the line style for each connection (this is optional)
lines = np.array([
(255,0,0,255,1),
(255,0,255,255,2),
(255,0,255,255,3),
(255,255,0,255,2),
(255,0,0,255,1),
(255,255,255,255,4),
], dtype=[('red',np.ubyte),('green',np.ubyte),('blue',np.ubyte),('alpha',np.ubyte),('width',float)])
## Update the graph
g.setData(pos=pos, adj=adj, pen=lines, size=1, symbol=symbols, pxMode=False)
## Start Qt event loop unless running in interactive mode or using pyside.
if __name__ == '__main__':
import sys
if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
QtGui.QApplication.instance().exec_()
I tested this and the scroll/zooming still worked after enabling the grid, so still not sure why doing it the other way DOESN'T work, but sometimes finding another way is the best answer :)

Related

How to reset/ start a new polygon with embeded matplotlib in pyqt

I am trying to create a polygon selector in my PySide2 application. Selector is working fine, but then I want to add functionality to reset/start new polygon when escape button is pressed. Something similar to this PolygonSelector example, when escape is pressed.
https://matplotlib.org/stable/gallery/widgets/polygon_selector_demo.html
I tried method .clear() but it does not seems to work for me.
import sys
import numpy as np
from PySide2 import QtWidgets, QtGui
from matplotlib.backends.backend_qtagg import (
FigureCanvas, NavigationToolbar2QT as NavigationToolbar)
from matplotlib.figure import Figure
from matplotlib.widgets import PolygonSelector
class ApplicationWindow(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
self._main = QtWidgets.QWidget()
self.setCentralWidget(self._main)
shortcut_clear_selection = QtWidgets.QShortcut(QtGui.QKeySequence("Escape"), self._main)
shortcut_clear_selection.activated.connect(self.callback_clear_selection)
layout = QtWidgets.QVBoxLayout(self._main)
static_canvas = FigureCanvas(Figure(figsize=(5, 3)))
layout.addWidget(NavigationToolbar(static_canvas, self))
layout.addWidget(static_canvas)
ax = static_canvas.figure.subplots()
t = np.linspace(0, 10, 501)
ax.plot(t, np.tan(t), ".")
self.poly = PolygonSelector(ax, self.onselect)
def onselect(self, verts):
pass
def callback_clear_selection(self):
# HERE should be the reset part
self.poly.clear()
if __name__ == "__main__":
qapp = QtWidgets.QApplication(sys.argv)
app = ApplicationWindow()
app.show()
app.activateWindow()
app.raise_()
qapp.exec_()
Problem is, that ESC key release event is not handled by PolygonSelector but Your callback. Therefore, to clear polygon and restart creation of polygon You have to clear polygon data and show selector again. clear method was just hiding selector, but polygon data stayed unchanged.
Change Your callback code into this:
def callback_clear_selection(self):
# HERE should be the reset
self.poly._xs, self.poly._ys = [0], [0]
self.poly._selection_completed = False
self.poly.set_visible(True)
Now, when You press ESC, polygon should be removed and You can start selection of new one.

Put a Matplotlib plot as a QGraphicsItem/into a QGraphicsView

So I have a very basic plot layout described below (with x and y values changed for brevity):
import matplotlib.pyplot as plt
from matplotlib.figure import Figure
import numpy as np
figure = Figure()
axes = figure.gca()
axes.set_title(‘My Plot’)
x=np.linspace(1,10)
y=np.linspace(1,10)
y1=np.linspace(11,20)
axes.plot(x,y,’-k’,label=‘first one’)
axes.plot(x,y1,’-b’,label=‘second one’)
axes.legend()
axes.grid(True)
And I have designed a GUI in QT designer that has a GraphicsView (named graphicsView_Plot) that I would like to put this graph into and I would like to know how I would go about putting this graph into the GraphicsView. Barring starting over and using the QT based graphing ability I don’t really know how (if possible) to put a matplotlib plot into this graphics view. I know it would be a super simple thing if I can convert it into a QGraphicsItem as well, so either directly putting it into the GraphicsView or converting it to a QGraphicsItem would work for me.
You have to use a canvas that is a QWidget that renders the matplotlib instructions, and then add it to the scene using addWidget() method (or through a QGraphicsProxyWidget):
import sys
from PyQt5 import QtWidgets
from matplotlib.figure import Figure
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
import numpy as np
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
scene = QtWidgets.QGraphicsScene()
view = QtWidgets.QGraphicsView(scene)
figure = Figure()
axes = figure.gca()
axes.set_title("My Plot")
x = np.linspace(1, 10)
y = np.linspace(1, 10)
y1 = np.linspace(11, 20)
axes.plot(x, y, "-k", label="first one")
axes.plot(x, y1, "-b", label="second one")
axes.legend()
axes.grid(True)
canvas = FigureCanvas(figure)
proxy_widget = scene.addWidget(canvas)
# or
# proxy_widget = QtWidgets.QGraphicsProxyWidget()
# proxy_widget.setWidget(canvas)
# scene.addItem(proxy_widget)
view.resize(640, 480)
view.show()
sys.exit(app.exec_())

Add axes labels and title to PyQtGraph ImageView

I use a PyQtGraph ImageView to display multi-dimensional data.
What is the easiest way to add axes labels and a title to such an ImageView?
I tried adding a LabelItem to the underlying ViewBox. I assume positioning it correctly requires hacking the underlying layout. Is this the way to go, or is there an easier way?
import numpy as np
from pyqtgraph.Qt import QtCore, QtGui
import pyqtgraph as pg
app = QtGui.QApplication([])
# make ImageView with test data
imv = pg.ImageView()
data = np.fromfunction(lambda i, j: np.sin(i/16)*j/128, (512, 512), dtype=float) \
+ np.random.normal(scale=0.2, size=(512, 512))
imv.setImage(data)
# add label
vbox = imv.getView()
vbox.addItem(pg.LabelItem("this is a nice label"))
imv.show()
app.exec_()
import numpy as np
from pyqtgraph.Qt import QtCore, QtGui
import pyqtgraph as pg
app = QtGui.QApplication([])
# Add Plot item to show axis labels
plot = pg.PlotItem()
plot.setLabel(axis='left', text='Y-axis')
plot.setLabel(axis='bottom', text='X-axis')
# make ImageView with test data
imv = pg.ImageView(view=plot) # set the plot to ImageView's view
data = np.fromfunction(lambda i, j: np.sin(i/16)*j/128, (512, 512), dtype=float) + np.random.normal(scale=0.2, size=(512, 512))
imv.setImage(data)
# add label
vbox = imv.getView()
vbox.addItem(pg.LabelItem("this is a nice label"))
imv.show()
app.exec_()

How to show matplotlib.pyplot in qt widget?

I want to show the pyplot image in widget (QWidget) that I put in a gui designed in QtDesigner:
When I push the Çiz button I want to show the image that I can create in python with that code:
points = np.array([(1, 1), (2, 4), (3, 1), (9, 3)])
x = points[:,0]
y = points[:,1]
# calculate polynomial
z = np.polyfit(x, y, 2)
f = np.poly1d(z)
x_fit = np.linspace(min(x), max(x), 10000)
y_fit = [f(_x) for _x in x_fit]
plt.plot(x, y)
plt.plot(x_fit, y_fit)
plt.show()
EDIT
I made some changes according to the answer but I have new problems.
After I promote it:
I rearrange my code below:
# calculate polynomial and r
self.x_fit = np.linspace(min(self.itemX), max(self.itemY), 10000)
self.y_fit = [f(_x) for _x in self.x_fit]
self._dlg.plotwidget.plot(self.itemX, self.itemY)
self._dlg.plotwidget.plot(self.x_fit, self.y_fit)
self.itemX is x values in a list.
self.itemY is y values in a list.
self._dlg is the MainWindow that you see.
When I try to open that window I get this error message:
I just tested the code below works for me:
import sys
import numpy as np
import pyqtgraph as pg
from pyqtgraph.Qt import QtCore, QtGui, uic
uifilename = 'test.ui'
form_class = uic.loadUiType(uifilename)[0] #dirty reading of the ui file. better to convert it to a '.py'
class MyWindowClass(QtGui.QMainWindow, form_class):
def __init__(self):
QtGui.QMainWindow.__init__(self)
self.setupUi(self)
self.onInit()
def onInit(self):
#usually a lot of connections here
self.x_fit = np.linspace(1,10000, 10000)
self.y_fit = [f(_x) for _x in self.x_fit]
self.plotwidget.plot(self.x_fit,self.y_fit,symbol='o',pen=None)
self.plotwidget.setLabel('left',text='toto',units='')
self.plotwidget.setLabel('top',text='tata',units='')
def f(x):
return x**2+1
if __name__ == '__main__':
if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
app = QtGui.QApplication([])
pg.setConfigOption('background', 'w')
win = MyWindowClass()
win.show()
app.exec_()
with this test.ui : https://github.com/steph2016/profiles/blob/master/test.ui.
Note the plotwidget is encapsulated inside a layout. I thought it works with just the Main Window, but I'm not sure anymore...
Sorry I don't exactly answer the question but I don't use matplotlib.pyplot with pyqt. I recommend using pyqtgraph (http://pyqtgraph.org), which is quite convenient and powerful. With Qt Designer:
you just insert a 'graphics view' in your GUI
you right-click on it and promote it to "plotwidget" using pyqtgraph. I guess the english tab is something like this: base class 'QGraphicsView'; promoted class 'PlotWidget'; header file 'pyqtgraph', then 'add', then 'promote'
then you can make plotwidget.plot(x,y,...), plotwidget.clear(), and co. It will plot everything inside the plotwidget with a bunch of interacting possibilities

pyqtgraph: synchronize scaling of axes in different plots

I want to synchronize the X-Axis of several pyqtgraph plots. When the user rescales the X-Axis with mouse interactions (e.g. scroll-wheel while mouse on x-Axis) I want to assign the same changes to all the other plots. So how do I do this?
I derived a minimized code from a basic example below.
Do I have to overwrite the viewRangeChanged() functions of w1 and w2?
import pyqtgraph as pg
from pyqtgraph.Qt import QtCore, QtGui
import pyqtgraph.console
import numpy as np
from pyqtgraph.dockarea import *
win = QtGui.QMainWindow()
area = DockArea()
win.setCentralWidget(area)
win.resize(1000,500)
win.setWindowTitle('pyqtgraph example: dockarea')
d1 = Dock("Dock1")
d2 = Dock("Dock2")
area.addDock(d1, 'bottom')
area.addDock(d2, 'bottom', d1)
w1 = pg.PlotWidget(title="Dock 1 plot")
w1.plot(np.random.normal(size=100))
d1.addWidget(w1)
w2 = pg.PlotWidget(title="Dock 2 plot")
w2.plot(np.random.normal(size=100))
d2.addWidget(w2)
win.show()
## Start Qt event loop unless running in interactive mode or using pyside.
if __name__ == '__main__':
import sys
if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
QtGui.QApplication.instance().exec_()
This question has a follow up here with another answer to this question.
We need to use the sigRangeChanged signal and connect it to a slot, the problem is that the change of the range another item would generate the signal sigRangeChanged and so on generating an infinite loop, to solve this you must disconnect those signals before making the modifications and reconnect them to the final.
w1.sigRangeChanged.connect(onSigRangeChanged)
w2.sigRangeChanged.connect(onSigRangeChanged)
def onSigRangeChanged(r):
w1.sigRangeChanged.disconnect(onSigRangeChanged)
w2.sigRangeChanged.disconnect(onSigRangeChanged)
if w1 == r:
w2.setRange(xRange=r.getAxis('bottom').range)
elif w2 == r:
w1.setRange(xRange=r.getAxis('bottom').range)
w1.sigRangeChanged.connect(onSigRangeChanged)
w2.sigRangeChanged.connect(onSigRangeChanged)
Example:
import pyqtgraph as pg
from pyqtgraph.Qt import QtCore, QtGui
import numpy as np
from pyqtgraph.dockarea import *
import sys
def onSigRangeChanged(r):
w1.sigRangeChanged.disconnect(onSigRangeChanged)
w2.sigRangeChanged.disconnect(onSigRangeChanged)
if w1==r:
w2.setRange(xRange=r.getAxis('bottom').range)
elif w2 == r:
w1.setRange(xRange=r.getAxis('bottom').range)
w1.sigRangeChanged.connect(onSigRangeChanged)
w2.sigRangeChanged.connect(onSigRangeChanged)
app = QtGui.QApplication(sys.argv)
win = QtGui.QMainWindow()
area = DockArea()
win.setCentralWidget(area)
win.resize(1000,500)
win.setWindowTitle('pyqtgraph example: dockarea')
d1 = Dock("Dock1")
d2 = Dock("Dock2")
area.addDock(d1, 'bottom')
area.addDock(d2, 'bottom', d1)
w1 = pg.PlotWidget(title="Dock 1 plot")
it=w1.plot(np.random.normal(size=100))
d1.addWidget(w1)
w2 = pg.PlotWidget(title="Dock 2 plot")
w2.plot(np.random.normal(size=100))
d2.addWidget(w2)
w1.sigRangeChanged.connect(onSigRangeChanged)
w2.sigRangeChanged.connect(onSigRangeChanged)
win.show()
sys.exit(app.exec_())
Better yet,
Instead of disconnecting then reconnecting signals, it is possible to use blockSignals.
here is a generic way to synchronize any number of plots :
syncedPlots = [w1, w2, w3] # put as many plots as you wish
def onSigRangeChanged(r):
for g in syncedPlots:
if g !=r :
g.blockSignals(True)
g.setRange(xRange=r.getAxis('bottom').range)
g.blockSignals(False)
for g in syncedPlots:
g.sigRangeChanged.connect(onSigRangeChanged)
There is a better answer in this question:
Instead of connecting to the sigRangeChanged event we can directly link the axes
scales by w2.setXLink(w1).

Categories