pyqtgraph: maintain constant position of TextItem while scaling - python

I would like a TextItem that maintains a constant position on the graph while scaling the y-axis, essentially the same functionality as legend only as a TextItem where I can change the text as needed. I cannot figure out how to do this. Any suggestions welcome.
This example shows the problem. On the lefthand graph, scaling the y-axis causes the text to move whereas on the righthand graph the legend stays in a constant position as you scale. I would like the position of the textItem to be defined like the legend position, in a constant position relative to the graph window. Alternatively if someone knows how to change the format of the legend and update the text that would also work, but from my reading of the documentation this is not possible.
import pyqtgraph as pg
from PyQt4 import QtGui
import numpy as np
import sys
def main():
app = QtGui.QApplication(sys.argv)
widg = QtGui.QWidget()
widg.move(100, 100)
pg.setConfigOption('background', 'w')
pg.setConfigOption('foreground', 'k')
pgWidg = pg.GraphicsLayoutWidget()
pgWidg.resize(750, 250)
graph1 = pgWidg.addPlot(row=1, col=1)
graph2 = pgWidg.addPlot(row=1, col=2)
curve1 = graph1.plot(y=np.sin(np.linspace(1, 21, 1000)), pen='k')
curve2 = graph2.plot(y=np.sin(np.linspace(1, 21, 1000)), pen='k')
graph1.addItem(curve1)
graph2.addItem(curve2)
graph1.setMouseEnabled(x=False, y=True)
graph2.setMouseEnabled(x=False, y=True)
graph1Text = pg.TextItem(text = 'A1', color=(0, 0, 0))
graph1.addItem(graph1Text)
graph1Text.setPos(150, 1)
legend = graph2.addLegend()
style = pg.PlotDataItem(pen='w')
legend.addItem(style, 'A2')
grid = QtGui.QGridLayout()
grid.addWidget(pgWidg, 0,0)
widg.setLayout(grid)
widg.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()

7 bilion years later of googling...
label = pg.LabelItem("Error", size="36pt", color="FF0000")
label.setParentItem(self.plotInstance)
label.anchor(itemPos=(1,0), parentPos=(1,0), offset=(-10,10))
where self.plotInstance = pg.PlotWidget.getPlotItem()
works on PyQt5 and pyqtgraph 0.12

This is a somewhat old question now- Hopefully this will help someone. Answering this helped me answer my own question.
Please note that I used PySide2 rather than PyQt4- I don't think this is significantly different to PyQt4. I am also using pyqtgraph 0.11.1.
There is a getLabel() method of the LegendItem that returns the LabelItem inside the legend for a given plotItem. This should allow you to do what you want.
You created your legend with this code:
legend = graph2.addLegend()
style = pg.PlotDataItem(pen='w')
legend.addItem(style, 'A2')
You can then get the labelitem with:
legend_labelitem = legend.getLabel(style)
With that you should be able to change the properties - such as using .setText() to set a new legend text:
legend_labelitem.setText('Something else')
The full code would end up as this:
import pyqtgraph as pg
# from PySide2 import QtGui # <---- tested with this
from PyQt4 import QtGui
import numpy as np
import sys
def main():
app = QtGui.QApplication(sys.argv)
widg = QtGui.QWidget()
widg.move(100, 100)
pg.setConfigOption('background', 'w')
pg.setConfigOption('foreground', 'k')
pgWidg = pg.GraphicsLayoutWidget()
pgWidg.resize(750, 250)
graph1 = pgWidg.addPlot(row=1, col=1)
graph2 = pgWidg.addPlot(row=1, col=2)
curve1 = graph1.plot(y=np.sin(np.linspace(1, 21, 1000)), pen='k')
curve2 = graph2.plot(y=np.sin(np.linspace(1, 21, 1000)), pen='k')
graph1.addItem(curve1)
graph2.addItem(curve2)
graph1.setMouseEnabled(x=False, y=True)
graph2.setMouseEnabled(x=False, y=True)
graph1Text = pg.TextItem(text = 'A1', color=(0, 0, 0))
graph1.addItem(graph1Text)
graph1Text.setPos(150, 1)
legend = graph2.addLegend()
style = pg.PlotDataItem(pen='w')
legend.addItem(style, 'A2')
legend_labelitem = legend.getLabel(style) # <---------
legend_labelitem.setText('Something else') # <---------
grid = QtGui.QGridLayout()
grid.addWidget(pgWidg, 0,0)
widg.setLayout(grid)
widg.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
It produces this:

Related

Why aren't the matplotlib checkboxes working in pyQt5?

I have a little issue in my code since I have a matplotlib graph with checkboxes (to choose what to plot) but when I open it with Pyqt5 (a Pushbutton) it does open but the checkboxes do not work, we can not touch them. My function works well, but not with Pyqt5, I hope I'm enough precise and I'm sorry if I am not. Here is my code if that can help you :
from PyQt5.QtWidgets import QPushButton, QMainWindow, QApplication
import sys
from matplotlib.widgets import CheckButtons
import matplotlib.pyplot as plt
def graph_check () :
x = [1,2,3,4,5,6]
y1 = [1,1,1,1,3,1]
y2 = [0,2,1,2,2,1]
y3 = [4,3,2,0,0,5]
fig,ax = plt.subplots()
p1, = ax.plot(x,y1,color = 'red', label = 'red')
p2, = ax.plot(x,y2,color = 'green', label = 'green')
p3, = ax.plot(x,y3,color = 'blue', label = 'blue')
lines = [p1,p2,p3]
plt.subplots_adjust(left = 0.25, bottom=0.1, right=0.95,top = 0.95)
# checkbuttons widgets
labels = ['red', 'green', 'blue']
activated = [True, True,True]
axCheckbutton = plt.axes([0.03,0.4,0.15,0.15])
chxbox = CheckButtons(axCheckbutton, labels,activated)
def set_visible (label) :
index = labels.index(label)
lines[index].set_visible(not lines[index].get_visible())
plt.draw()
chxbox.on_clicked(set_visible)
plt.show()
# that function does work well, in the end we have a graph with 3 lines and we can make
# them visible or not thanks to the checkbox.
class Window(QMainWindow):
def __init__(self):
super().__init__()
self.setGeometry(100, 100, 600, 400)
self.btn1 = QPushButton('Graph check', self)
self.btn1.setGeometry(130, 215, 125, 55)
self.btn1.clicked.connect(self.btn1_onClicked)
self.show()
def btn1_onClicked(self):
graph_check()
# it works, we can see the graph but it is impossible to use the checkbox...
App = QApplication(sys.argv)
window = Window()
sys.exit(App.exec())
The problem is caused because "chxbox" is a local variable whose reference will be removed when the function finishes executing. A possible solution is to make it an attribute of another object that has a greater scope:
# ...
plt.chxbox = CheckButtons(axCheckbutton, labels, activated)
def set_visible(label):
index = labels.index(label)
lines[index].set_visible(not lines[index].get_visible())
plt.draw()
plt.chxbox.on_clicked(set_visible)
# ...

How to use plotly offline without regenerating figure?

currently, I am using plotly offline like this:
from PyQt5.QtWebEngineWidgets import QWebEngineView
from PyQt5.QtWidgets import *
import plotly.graph_objects as go
import plotly
import numpy as np
class Window(QWidget):
def __init__(self):
super(Window, self).__init__()
grid_layout = QGridLayout()
self.pb = QPushButton('plot')
# some example data
x = np.arange(0, 2*np.pi, 0.001)
y = np.sin(x)
# create the plotly figure
fig = go.Figure(go.Scatter(x=x, y=y))
# we create html code of the figure
html = "".join(['<html><body>',
plotly.offline.plot(fig, output_type='div', include_plotlyjs='cdn'),
'</body></html>']
)
# we create an instance of QWebEngineView and set the html code
self.plot_widget = QWebEngineView()
self.plot_widget.setHtml(html)
grid_layout.addWidget(self.plot_widget, 0, 0)
grid_layout.addWidget(self.pb, 1, 0)
self.setLayout(grid_layout)
self.pb.clicked.connect(self.newplot)
def newplot(self):
# some example data
x = np.arange(0, 2 * np.pi, 0.001)
y = np.sin(x+np.random.uniform(low=0, high=2*np.pi))
# create the plotly figure
fig = go.Figure(go.Scatter(x=x, y=y))
# we create html code of the figure
html = "".join(['<html><body>',
plotly.offline.plot(fig, output_type='div', include_plotlyjs='cdn'),
'</body></html>']
)
self.plot_widget.setHtml(html)
if __name__ == '__main__':
app = QApplication([])
window = Window()
window.show()
app.exec_()
My problem is, this way when I draw a new plot, the whole thing regenerates. Is there a way to do it more smoothly and only update the existing figure? Keep the axes, and just replace the old line with a new one in the plot.
I have been using Plotly for Python for more than a year and I have never heard of the possibility that you described.

ScaleBar in pyqtgraph doesn't update when scale is changed

When I change the scale of the axis of my image, my ScaleBar shows the incorrect scale. How do I update the scale bar when I change the axes?
from PyQt5 import QtWidgets
import pyqtgraph as pg
import numpy as np
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
imvOCTTopLeft = pg.ImageView(view=pg.PlotItem())
imvOCTTopLeft.setImage(np.random.normal(size=(100,100)))
imvOCTTopLeft.view.getAxis('left').setScale(0.6)
imvOCTTopLeft.view.getAxis('bottom').setScale(0.4)
scale = pg.ScaleBar(size=10,suffix = "px")
viewbox = imvOCTTopLeft.view
if not isinstance(viewbox, pg.ViewBox): viewbox = viewbox.getViewBox()
scale.setParentItem(viewbox)
scale.anchor((1, 1), (1, 1), offset=(-20, -20))
imvOCTTopLeft.show()
sys.exit(app.exec_())
This image shows that the scale bar is showing approximately 4 pixels but states that it is showing 10 pixels.
I think this is because I changed the axis scale.
This seems to be a bug: link. The viewbox rescales after sigRangeChanged is emitted.
"Hacky" solution is to delay the ScaleBar update:
(You might need to play around with the time, 100 and 10 worked for me. If it doesnt work, increase it.)
from PyQt5 import QtWidgets, QtCore
import pyqtgraph as pg
import numpy as np
def updateDelay(scale, time):
QtCore.QTimer.singleShot(time, scale.updateBar)
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
plotItem = pg.PlotItem()
imvOCTTopLeft = pg.ImageView(view=plotItem)
imvOCTTopLeft.setImage(np.random.normal(size=(100, 100)))
imvOCTTopLeft.view.getAxis('left').setScale(0.6)
scale = 0.4 #edit
imvOCTTopLeft.view.getAxis('bottom').setScale(scale) #edit
scale = pg.ScaleBar(size=10*(1/scale), suffix="px") #edit
scale.text.setText('10 px') #edit
plotItem.sigRangeChanged.connect(lambda: updateDelay(scale, 10)) # here: time=10ms
viewbox = imvOCTTopLeft.view
if not isinstance(viewbox, pg.ViewBox): viewbox = viewbox.getViewBox()
scale.setParentItem(viewbox)
scale.anchor((1, 1), (1, 1), offset=(-20, -20))
imvOCTTopLeft.show()
updateDelay(scale, 100) # here time=100ms
sys.exit(app.exec_())
Result:

How can I improve scrolling speed in matplotlib when a figure contains many axes?

I have a matplotlib figure with many axes, and the scrolling/zooming becomes unusably slow. Is there anyway to speed it up?
As an example, try scrolling one of the axes produced with this code:
import matplotlib.pyplot as plt
fig,plts = plt.subplots(10,10)
plt.show()
(I am on a Mac, using the macosx backend. The QT4Agg backend seemed similarly sluggish.)
I think the slowdown comes from matplotlib redrawing the entire figure, rather than just the subplot you want to zoom. I have found that you can speed things up by creating multiple figures and embedding them in a PyQt widget.
Here's a quick proof of concept using 'figure_enter_event' and a bit of ugly hackery to allow the use of a single navigation toolbar across all figures. Note that I have only attempted to make the pan and zoom features work properly. By peeking at the source of NavigationToolbar2 in backend_bases.py some more I'm sure you could adapt it to your needs.
import sys
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtCore import pyqtSlot
import matplotlib
matplotlib.use('Qt5Agg')
matplotlib.rcParams['backend.qt5'] = 'PyQt5'
matplotlib.rcParams.update({'figure.autolayout': True})
from matplotlib.figure import Figure
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar
import numpy as np
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, **kwargs):
super(MainWindow, self).__init__(**kwargs)
# Construct the plots
playout = QtWidgets.QGridLayout()
playout.setContentsMargins(0, 0, 0, 0)
for row in range(0, 10):
for col in range(0, 10):
fig = Figure()
ax = fig.add_subplot(111)
canvas = FigureCanvas(fig)
canvas.mpl_connect('figure_enter_event', self.enterFigure)
playout.addWidget(canvas, row, col, 1, 1)
t = np.arange(-2*np.pi, 2*np.pi, step=0.01)
ax.plot(t, np.sin(row*t) + np.cos(col*t))
# Assign toolbar to first plot
self.navbar = NavigationToolbar(playout.itemAtPosition(0, 0).widget(), self)
cwidget = QtWidgets.QWidget()
layout = QtWidgets.QVBoxLayout(cwidget)
layout.setContentsMargins(0, 0, 0, 0)
layout.addWidget(self.navbar)
layout.addLayout(playout)
self.setCentralWidget(cwidget)
def enterFigure(self, event):
self.navbar.canvas = event.canvas
event.canvas.toolbar = self.navbar
self.navbar._idDrag = event.canvas.mpl_connect('motion_notify_event', self.navbar.mouse_move)
# Toggle control off and then on again for the current canvas
if self.navbar._active:
if self.navbar._active == 'PAN':
self.navbar.pan()
self.navbar.pan()
elif self.navbar._active == 'ZOOM':
self.navbar.zoom()
self.navbar.zoom()
app = QtWidgets.QApplication(sys.argv)
win = MainWindow()
win.show()
app.exec_()

How to make Matplotlib redraw faster?

I have to plot a grid of subplots (16x16 ones, for example). I use matplotlib but It is a bit too slow, especially when I have to redraw the plots (for example, when their size is changed). How can I make it faster?
P.S. Here is an example of code:
import sys
import matplotlib
matplotlib.use('Qt4Agg')
import pylab
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
import PyQt4
from PyQt4 import QtCore, QtGui, Qt
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
# generate the plot
fig = Figure(figsize=(800, 800), facecolor=(1, 1, 1), edgecolor=(0, 0, 0))
for i in range(256):
ax = fig.add_subplot(16, 16, i)
ax.plot([0, 1])
ax.set_xticks([])
ax.set_yticks([])
# ax.set_title("Mega %i" % (i,))
# generate the canvas to display the plot
canvas = FigureCanvas(fig)
canvas.setMinimumWidth(640)
canvas.setMinimumHeight(640)
# generate layout
layout = QtGui.QVBoxLayout();
layout.addWidget(canvas)
layout.setGeometry(QtCore.QRect(0, 0, 1000, 1000))
# generate widget
widget = QtGui.QWidget()
widget.setLayout(layout)
# generate scroll area
win = QtGui.QScrollArea()
win.setWidget(widget)
win.setMinimumWidth(100)
win.setWidgetResizable(True)
win.show()
canvas.draw()
sys.exit(app.exec_())
I don't have an environment to test your code but I these steps work for me:
Using cProfile to profile the whole thing (f.e. How can you profile a python script?).
Usually its one or two functions which slow down all.
Search stackoverflow/the internet for these function names. Usually 1-2 people solved the performance issue already.
Greetings Kuishi

Categories

Resources