I'm trying to plot a numpy array using QScatterSeries, however only the axis are updated and the points are not displayed. I'm not sure why it is not working.
projectionwindow.py
from PySide2.QtCore import Qt
from PySide2.QtWidgets import QWidget, QHBoxLayout
from PySide2.QtGui import QColor, QPen
from PySide2.QtCharts import QtCharts
class ProjectionWindow(QWidget):
"""
TODO
"""
def __init__(self, parent=None) -> 'None':
super().__init__()
self.setWindowTitle('Projection')
self.resize(800, 800)
self.chart = QtCharts.QChart()
self.chart_view = QtCharts.QChartView(self.chart)
self.layout = QHBoxLayout(self)
self.layout.addWidget(self.chart_view)
self.setLayout(self.layout)
self.show()
def loadCharts(self, data: 'ndarray') -> 'None':
points = QtCharts.QScatterSeries()
points.setMarkerSize(2.0)
for i in range(data.shape[0]):
points.append(data[i, 0], data[i, 1])
self.chart.addSeries(points)
self.chart.createDefaultAxes()
self.chart.show()
This is my current result when calling
main.py
import sys
import numpy as np
from PySide2.QtWidgets import QApplication
from ui.projectionwindow import ProjectionWindow
if __name__ == "__main__":
app = QApplication(sys.argv)
data = np.array([[1,2],
[3,4]])
window = ProjectionWindow(app)
window.loadCharts(data)
sys.exit(app.exec_())
Resulted obtained:
You have 2 errors:
The markerSize is very small that makes it indistinguishable to the eye.
When establishing a series for the first time, the QChart takes the minimum rectangle so that in your case it is in the corners, so the solution is to change the minimum and maximum value of the axes considering an adequate margin.
def loadCharts(self, data: "ndarray") -> "None":
points = QtCharts.QScatterSeries()
points.setMarkerSize(20)
for i in range(data.shape[0]):
points.append(data[i, 0], data[i, 1])
self.chart.addSeries(points)
self.chart.createDefaultAxes()
m_x, M_x = min(data[:, 0]), max(data[:, 0])
m_y, M_y = min(data[:, 1]), max(data[:, 1])
ax = self.chart.axes(Qt.Horizontal, points)[0]
ax.setMin(m_x - 1)
ax.setMax(M_x + 1)
ay = self.chart.axes(Qt.Vertical, points)[0]
ay.setMin(m_y - 1)
ay.setMax(M_y + 1)
Output:
Related
I am working on GUI where I have a system with graphs.
I want to use the spanselector in the graph i do visualize.
I have searched and i can't understand how to use the span selector while calling the matplotlib widget.
This is an example i'm following to plot. it has 3 parts(main,mplwidget,ui file)
the main code file
# ------------------------------------------------------
# ---------------------- main.py -----------------------
# ------------------------------------------------------
from PyQt5.QtWidgets import*
from PyQt5.uic import loadUi
from matplotlib.backends.backend_qt5agg import (NavigationToolbar2QT as NavigationToolbar)
import numpy as np
import random
#from matplotlib.widgets import SpanSelector
class MatplotlibWidget(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
loadUi("qt_designer.ui",self)
self.setWindowTitle("PyQt5 & Matplotlib Example GUI")
self.pushButton_generate_random_signal.clicked.connect(self.update_graph)
self.addToolBar(NavigationToolbar(self.MplWidget.canvas, self))
def update_graph(self):
fs = 500
f = random.randint(1, 100)
ts = 1/fs
length_of_signal = 100
t = np.linspace(0,1,length_of_signal)
cosinus_signal = np.cos(2*np.pi*f*t)
sinus_signal = np.sin(2*np.pi*f*t)
self.MplWidget.canvas.axes.clear()
self.MplWidget.canvas.axes.plot(t, cosinus_signal)
self.MplWidget.canvas.axes.plot(t, sinus_signal)
self.MplWidget.canvas.axes.legend(('cosinus', 'sinus'),loc='upper right')
self.MplWidget.canvas.axes.set_title('Cosinus - Sinus Signal')
self.MplWidget.canvas.draw()
#span = self.MplWidget.canvas.axes.SpanSelector(ax1, onselect, 'horizontal', useblit=True,rectprops=dict(alpha=0.5, facecolor='red'))
def onselect(min_value, max_value):
print(min_value, max_value)
return min_value, max_value
app = QApplication([])
window = MatplotlibWidget()
window.show()
app.exec_()
the mplwidget file
# ------------------------------------------------------
# -------------------- mplwidget.py --------------------
# ------------------------------------------------------
from PyQt5.QtWidgets import*
from matplotlib.backends.backend_qt5agg import FigureCanvas
from matplotlib.figure import Figure
class MplWidget(QWidget):
def __init__(self, parent = None):
QWidget.__init__(self, parent)
self.canvas = FigureCanvas(Figure())
vertical_layout = QVBoxLayout()
vertical_layout.addWidget(self.canvas)
self.canvas.axes = self.canvas.figure.add_subplot(111)
self.setLayout(vertical_layout)
the ui file is attached to this link with all the codes:
here
in the other way this is the example code to use the span selector:
import matplotlib.pyplot as plt
import matplotlib.widgets as mwidgets
fig, ax = plt.subplots()
ax.plot([1, 2, 3], [10, 50, 100])
def onselect(vmin, vmax):
print(vmin, vmax)
rectprops = dict(facecolor='blue', alpha=0.5)
span = mwidgets.SpanSelector(ax, onselect, 'horizontal',span_stays=True,button=1,,rectprops=rectprops)
fig.show()
//////////////////////////////////////////////////////////////////////////////
i tried a lot of ways to assess the span selector but im a little bit confused in the way it works and how i should connect the the structure of code?
if i run whithin The comented line:
span = self.MplWidget.canvas.axes.SpanSelector(ax1, onselect, 'horizontal', useblit=True,rectprops=dict(alpha=0.5, facecolor='red'))
its shown the following error:
AttributeError:'AxesSubplot' object has no attribute 'SpanSelector'
finally, this is the desire result
You have to pass the self.MplWidget.canvas.axes as ax:
# ...
self.MplWidget.canvas.draw()
self.span = SpanSelector(
self.MplWidget.canvas.axes,
self.onselect,
"horizontal",
useblit=True,
rectprops=dict(alpha=0.5, facecolor="red"),
)
def onselect(self, min_value, max_value):
print(min_value, max_value)
Note: since select is a method of the class, it must have self as the first parameter, and it must be invoked with self.select.
I'm using matplotlib with pyqt5 to draw data into 3 axes, and than user can make selection in one plot that will be shown in other two plots too. Since I'm working with big data (up to 10 millions of points), drawing selection could be slow, especially when I need to draw to scatterplot.
I am trying to use matplotlib blit function, but have some issues with result. Here is minimum simple example.
import matplotlib
matplotlib.use('Qt5Agg')
import numpy as np
import sys
from matplotlib.backends.qt_compat import QtCore, QtWidgets
from matplotlib.backends.backend_qt5agg import (FigureCanvas, NavigationToolbar2QT as NavigationToolbar)
from matplotlib.figure import Figure
class ApplicationWindow(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
self._main = QtWidgets.QWidget()
self.setCentralWidget(self._main)
layout = QtWidgets.QVBoxLayout(self._main)
self.static_canvas = FigureCanvas(Figure(figsize=(10, 10)))
layout.addWidget(self.static_canvas)
layout.addWidget(NavigationToolbar(self.static_canvas, self))
axes = self.static_canvas.figure.subplots(2, 1)
self.ax1 = axes[0]
self.ax2 = axes[1]
self.ax1.cla()
self.ax2.cla()
button = QtWidgets.QPushButton('Click me!')
button.clicked.connect(self.update_canvas_blit)
layout.addWidget(button)
# Fixing random state for reproducibility
np.random.seed(19680801)
# Create random data
N = 50000
x = np.random.rand(N)
y = np.random.rand(N)
self.ax1.scatter(x, y)
self.points = self.ax1.scatter([],[], s=5, color='red')
x = np.linspace(0, 1000, 100000)
self.ax2.plot(x, np.sin(x))
self.lines, = self.ax2.plot([],[], color='red')
self.static_canvas.draw()
self.background1 = self.static_canvas.copy_from_bbox(self.ax1.bbox)
self.background2 = self.static_canvas.copy_from_bbox(self.ax2.bbox)
def update_canvas_blit(self):
N = 50
x = np.random.rand(N)
y = np.random.rand(N)
self.static_canvas.restore_region(self.background1)
self.points.set_offsets(np.c_[x,y])
self.ax1.draw_artist(self.points)
self.ax1.figure.canvas.blit(self.ax1.bbox)
self.static_canvas.restore_region(self.background2)
x = np.linspace(0, np.random.randint(500,1000), 1000)
self.lines.set_data(x, np.sin(x))
self.ax2.draw_artist(self.lines)
self.ax2.figure.canvas.blit(self.ax2.bbox)
if __name__ == "__main__":
qapp = QtWidgets.QApplication(sys.argv)
app = ApplicationWindow()
app.show()
qapp.exec_()
When clicking button, expected output should be still same background with random points/lines redrawing. In a way it is happening but there are some strange artifacts that looks like somehow axes are drawn to each other. But when I try to save it to .png, it will restore to good state.
The problem is that the snapshot of the background is taken at a moment in time where the figure has not yet been shown on screen. At that point the figure is 10 by 10 inches large. Later, it is shown inside the QMainWindow and resized to fit into the widget.
Only once that has happened, it makes sense to take the background snapshot.
One option is to use a timer of 1 second and only then copy the background. This would look as follows.
import numpy as np
import sys
from matplotlib.backends.qt_compat import QtCore, QtWidgets
from matplotlib.backends.backend_qt5agg import (FigureCanvas, NavigationToolbar2QT as NavigationToolbar)
from matplotlib.figure import Figure
class ApplicationWindow(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
self._main = QtWidgets.QWidget()
self.setCentralWidget(self._main)
layout = QtWidgets.QVBoxLayout(self._main)
self.static_canvas = FigureCanvas(Figure(figsize=(10, 10)))
layout.addWidget(self.static_canvas)
layout.addWidget(NavigationToolbar(self.static_canvas, self))
axes = self.static_canvas.figure.subplots(2, 1)
self.ax1 = axes[0]
self.ax2 = axes[1]
self.ax1.cla()
self.ax2.cla()
button = QtWidgets.QPushButton('Click me!')
button.clicked.connect(self.update_canvas_blit)
layout.addWidget(button)
# Fixing random state for reproducibility
np.random.seed(19680801)
# Create random data
N = 50000
x = np.random.rand(N)
y = np.random.rand(N)
self.ax1.scatter(x, y)
self.points = self.ax1.scatter([],[], s=5, color='red')
x = np.linspace(0, 1000, 100000)
self.ax2.plot(x, np.sin(x))
self.lines, = self.ax2.plot([],[], color='red')
self.static_canvas.draw()
self._later()
def _later(self, evt=None):
self.timer = self.static_canvas.new_timer(interval=1000)
self.timer.single_shot = True
self.timer.add_callback(self.update_background)
self.timer.start()
def update_background(self, evt=None):
self.background1 = self.static_canvas.copy_from_bbox(self.ax1.bbox)
self.background2 = self.static_canvas.copy_from_bbox(self.ax2.bbox)
def update_canvas_blit(self):
N = 50
x = np.random.rand(N)
y = np.random.rand(N)
self.static_canvas.restore_region(self.background1)
self.points.set_offsets(np.c_[x,y])
self.ax1.draw_artist(self.points)
self.ax1.figure.canvas.blit(self.ax1.bbox)
self.static_canvas.restore_region(self.background2)
x = np.linspace(0, np.random.randint(500,1000), 1000)
self.lines.set_data(x, np.sin(x))
self.ax2.draw_artist(self.lines)
self.ax2.figure.canvas.blit(self.ax2.bbox)
if __name__ == "__main__":
qapp = QtWidgets.QApplication(sys.argv)
app = ApplicationWindow()
app.show()
qapp.exec_()
I am building application which includes QCharts. Everything was working until I changed Value Axis to the DateTime axis. Now I don't see any series on the chart. I was trying methods which was provided in other topics on stack overflow but without success.
I was trying as it was suggested in other topics to change datetime to msec since epoch when I am setting range of x axe - unfortunately with this method on x axe I see epoch time not current time.
When I am setting range like now I see correct time on x axe but I don't see any series.
I've checked series - there are correct points in the range of x, y axis.
I am using python 3.7 and pyside2.
self.plot = QtCharts.QChart()
self.add_series("Magnitude (Column 1)", [0, 1])
self.chart_view = QtCharts.QChartView(self.plot)
self.series = QtCharts.QLineSeries()
self.series.setName(name)
self.plot.addSeries(self.series)
# Setting X-axis
self.axis_x = QtCharts.QDateTimeAxis()
self.axis_x.setTickCount(10)
self.axis_x.setLabelsAngle(70)
self.axis_x.setFormat("dd.MM.yy h:mm:ss")
self.axis_x.setTitleText("Date")
self.axis_x.setMax(QDateTime.currentDateTime().addSecs(60))
self.axis_x.setMin(QDateTime.currentDateTime())
# Setting Y-axis
self.axis_y = QtCharts.QValueAxis()
self.axis_y.setTickCount(7)
self.axis_y.setLabelFormat("%i")
self.axis_y.setTitleText("Temperature [celcious]")
self.axis_y.setMax(30)
self.axis_y.setMin(20)
self.series.attachAxis(self.axis_x)
self.series.attachAxis(self.axis_y)
self.plot.addAxis(self.axis_x, Qt.AlignBottom)
self.plot.addAxis(self.axis_y, Qt.AlignLeft)
...
# Add points to the chart
def addPoint(self):
x = QDateTime.currentDateTime().toSecsSinceEpoch()
y = float(20+self.i)
self.series.append(x, y)
print(self.series.points())
self.i += 1
print(QDateTime.currentDateTime().toMSecsSinceEpoch(),y)
You must use the toMSecsSinceEpoch() method instead of toSecsSinceEpoch(). On the other side of my experience I have seen that it is necessary to establish the range each time data is added (maybe it is a QtCharts bug).
Considering the above the solution is:
import random
from PySide2 import QtCore, QtGui, QtWidgets
from PySide2.QtCharts import QtCharts
class MainWindow(QtWidgets.QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.plot = QtCharts.QChart()
# self.add_series("Magnitude (Column 1)", [0, 1])
self.chart_view = QtCharts.QChartView(self.plot)
self.setCentralWidget(self.chart_view)
self.series = QtCharts.QLineSeries()
self.series.setName("Magnitude")
self.plot.addSeries(self.series)
# Setting X-axis
self.axis_x = QtCharts.QDateTimeAxis()
self.axis_x.setTickCount(10)
self.axis_x.setLabelsAngle(70)
self.axis_x.setFormat("dd.MM.yy h:mm:ss")
self.axis_x.setTitleText("Date")
self.axis_x.setMax(QtCore.QDateTime.currentDateTime().addSecs(60))
self.axis_x.setMin(QtCore.QDateTime.currentDateTime())
# Setting Y-axis
self.axis_y = QtCharts.QValueAxis()
self.axis_y.setTickCount(7)
self.axis_y.setLabelFormat("%i")
self.axis_y.setTitleText("Temperature [celcious]")
self.axis_y.setMax(30)
self.axis_y.setMin(20)
self.plot.setAxisX(self.axis_x, self.series)
self.plot.setAxisY(self.axis_y, self.series)
# ...
timer = QtCore.QTimer(self)
timer.timeout.connect(self.addPoint)
timer.start(500)
# Add points to the chart
def addPoint(self):
dt = QtCore.QDateTime.currentDateTime()
v = random.uniform(20, 30)
self.series.append(dt.toMSecsSinceEpoch(), v)
t_m, t_M = min(dt, self.axis_x.min()), max(dt, self.axis_x.max())
m, M = min(v, self.axis_y.min()), max(v, self.axis_y.max())
self.axis_x.setRange(t_m, t_M)
self.axis_y.setRange(m, M)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
window = MainWindow()
window.resize(640, 480)
window.show()
sys.exit(app.exec_())
I am looking for a way to make my surface plot item to change color base on height. Below is my current method:
def __init__(self, s):
self.traces = dict()
self.app = QtGui.QApplication(sys.argv)
self.w = gl.GLViewWidget()
self.w.opts['distance'] = 2000
self.w.setWindowTitle('pyqtgraph example: GLLinePlotItem')
self.w.setGeometry(0, 0, 600, 600)
self.w.show()
self.socket = s
self.timer = QtCore.QTimer()
self.timer.setInterval(1) # in milliseconds
self.timer.start()
self.timer.timeout.connect(self.onNewData)
# create the background grids
#gx is the y grid
#gz is the x gid
gx = gl.GLGridItem()
gx.rotate(90, 0, 1, 0)
gx.translate(0, 0, 0)
self.w.addItem(gx)
gz = gl.GLGridItem()
gz.translate(200, 0, -500)
self.w.addItem(gz)
gx.scale(100, 10, 100)
gz.scale(20, 10, 100)
self.y = np.linspace(0, 100, 10)
self.x = np.linspace(60,400, 708)
temp_z = np.zeros((10,708))
self.surf = gl.GLSurfacePlotItem(x=self.y, y=self.x, z=temp_z, shader='heightColor',
computeNormals=False, smooth=False)
self.surf.scale(3,1,1)
self.surf.shader()['colorMap'] = np.array([0.7, 2, 0.5, 0.2, 0.7, 0.7, 0.2, 0, 2])
self.w.addItem(self.surf)
But the method is not working out quiet well. As Z values get very high, the surface become completely white. Btw, I have no idea of what I am doing with colormap, i just took it off the example.
I suggest you to use the colors option of GLSurfacePlotItem.
The idea is to compute colors that are associated with the z values of the surface (the heigths) an make them normalize (between 0 and 1). With this, you can compute a color for each point of the surface with cmap of matlotlib for instance.
# -*- coding: utf-8 -*-
from __future__ import print_function, absolute_import
from pyqtgraph.Qt import QtCore, QtGui
import pyqtgraph as pg
import pyqtgraph.opengl as gl
import matplotlib.pyplot as plt
import numpy as np
import os
from PyQt4.QtGui import QFileDialog
import sys
if not( 'app' in locals()):
app = QtGui.QApplication([])
traces = dict()
# app = QtGui.QApplication(sys.argv)
w = gl.GLViewWidget()
w.opts['distance'] = 2000
w.setWindowTitle('pyqtgraph example: GLLinePlotItem')
w.setGeometry(0, 0, 600, 600)
w.show()
# socket = s
# timer = QtCore.QTimer()
# timer.setInterval(1) # in milliseconds
# timer.start()
# timer.timeout.connect(onNewData)
# create the background grids
#gx is the y grid
#gz is the x gid
gx = gl.GLGridItem()
gx.rotate(90, 0, 1, 0)
gx.translate(0, 0, 0)
w.addItem(gx)
gz = gl.GLGridItem()
gz.translate(200, 0, -500)
w.addItem(gz)
gx.scale(100, 10, 100)
gz.scale(20, 10, 100)
y = np.linspace(0, 100, 10)
print(y)
x = np.linspace(0,100, 10)
print(x)
temp_z = np.random.rand(len(x),len(y))*100.
cmap = plt.get_cmap('jet')
minZ=np.min(temp_z)
maxZ=np.max(temp_z)
rgba_img = cmap((temp_z-minZ)/(maxZ -minZ))
surf = gl.GLSurfacePlotItem(x=y, y=x, z=temp_z, colors = rgba_img )
surf.scale(3,1,1)
# surf.shader()['colorMap'] = np.array(list(np.linspace(-100, 100, 1000)))
w.addItem(surf)
if __name__ == '__main__':
import sys
if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
QtGui.QApplication.instance().exec_()
which give :
I wish to add functionality such that the user can draw a rectangle over a selection of lines and the graph will refresh such that the lines within the rectangle keep their respective colour and any lines outside turn grey?
My code is as follows, and currently zooms on the drawing of a user defined rectangle over the lines, (for 3 lines, my actual code will plot a lot more):
from pyqtgraph.Qt import QtGui, QtCore
import numpy as np
import pyqtgraph as pg
pg.setConfigOption('background', 'w')
pg.setConfigOption('foreground', 'k')
from random import randint
class CustomViewBox(pg.ViewBox):
def __init__(self, *args, **kwds):
pg.ViewBox.__init__(self, *args, **kwds)
self.setMouseMode(self.RectMode)
## reimplement right-click to zoom out
def mouseClickEvent(self, ev):
if ev.button() == QtCore.Qt.RightButton:
#self.autoRange()
self.setXRange(0,5)
self.setYRange(0,10)
def mouseDragEvent(self, ev):
if ev.button() == QtCore.Qt.RightButton:
ev.ignore()
else:
pg.ViewBox.mouseDragEvent(self, ev)
app = pg.mkQApp()
vb = CustomViewBox()
graph = pg.PlotWidget(viewBox=vb, enableMenu=False)
colour = []
for i in range(0,3):
colourvalue = [randint(0,255), randint(0,255), randint(0,255)]
tuple(colourvalue)
colour.append(colourvalue)
y_data = [
[['a',0],['b',1],['c',None],['d',6],['e',7]],
[['a',5],['b',2],['c',1],['d',None],['e',1]],
[['a',3],['b',None],['c',4],['d',9],['e',None]],
]
x_data = [0, 1, 2, 3, 4]
for i in range(3):
xv = []
yv = []
for j, v in enumerate(row[i][1] for row in y_data):
if v is not None:
xv.append(int(j))
yv.append(float(v))
graph.plot(xv, yv, pen = colour[i], name=y_data[0][i][0])
graph.show()
graph.setWindowTitle('Hourly Frequency Graph')
graph.setXRange(0,5)
graph.setYRange(0,10)
graph.setLabel('left', "Frequency", units='%')
graph.setLabel('bottom', "Hour")
graph.showGrid(x=True, y=True)
if __name__ == '__main__':
import sys
if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
QtGui.QApplication.instance().exec_()
Thanks in advance for any help and advice!
As an aside I would also like to know why this code always give a segmentation fault: 11 when I close the window.