How to set pyqtgraph axis label offset? - python

I need to implement quite tiny pyqtgraph plots in a GUI. If doing so, by default the axis' label offset is too large. How can I set the offset of the axis label, not the axis ticks.
The following code example creates a basic pyqtgraph plot. I was able to set the offset of the tick text but not the offset of the label text only. I would like to only get the axis labels closer to the axis.
import numpy as np
from pyqtgraph.Qt import QtGui, QtCore
import pyqtgraph as pg
app = QtGui.QApplication([])
x = np.linspace(0, 1, 10000)
y = np.linspace(350, 2500, 10000)
win = pg.GraphicsWindow()
plot = win.addPlot(x=x, y=y, title="Plot")
label_style = {'color': '#EEE', 'font-size': '14pt'}
plot.setLabel('bottom', "some x axis label", **label_style)
plot.setLabel('left', "some y axis label")
plot.getAxis('left').setLabel(**label_style)
font=QtGui.QFont()
font.setPixelSize(14)
plot.getAxis("bottom").tickFont = font
# Here I increased the tickTextOffset of the x axis
plot.getAxis("bottom").setStyle(tickTextOffset=50)
plot.getAxis("left").tickFont = font
plot.getAxis("left").setStyle(tickTextOffset=14)
if __name__ == '__main__':
import sys
if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
QtGui.QApplication.instance().exec_()
Any help is much appreciated!
Update:
I found a pyqtgraph internal solution in pyqtgraph.AxisItem.resizeEvent() but the function does not accept any passed arguments.
def resizeEvent(self, ev=None):
#s = self.size()
## Set the position of the label
nudge = 5
br = self.label.boundingRect()
p = QtCore.QPointF(0, 0)
if self.orientation == 'left':
p.setY(int(self.size().height()/2 + br.width()/2))
p.setX(-nudge)
elif self.orientation == 'right':
p.setY(int(self.size().height()/2 + br.width()/2))
p.setX(int(self.size().width()-br.height()+nudge))
elif self.orientation == 'top':
p.setY(-nudge)
p.setX(int(self.size().width()/2. - br.width()/2.))
elif self.orientation == 'bottom':
p.setX(int(self.size().width()/2. - br.width()/2.))
p.setY(int(self.size().height()-br.height()+nudge))
self.label.setPos(p)
self.picture = None
the corresponding variable is nudge. Unfortunately it is not accessible or is there a way to bypass resizeEvent() without changing the source code of pyqtgraph?
As a proposal to make nudge passable I created a pyqtgraph issue on
github:
https://github.com/pyqtgraph/pyqtgraph/issues/986

One solution is to create a Custom AxisItem and override that method. To call resizeEvent you can make false resizing:
import numpy as np
from pyqtgraph.Qt import QtGui, QtCore
import pyqtgraph as pg
class CustomAxis(pg.AxisItem):
#property
def nudge(self):
if not hasattr(self, "_nudge"):
self._nudge = 5
return self._nudge
#nudge.setter
def nudge(self, nudge):
self._nudge = nudge
s = self.size()
# call resizeEvent indirectly
self.resize(s + QtCore.QSizeF(1, 1))
self.resize(s)
def resizeEvent(self, ev=None):
# s = self.size()
## Set the position of the label
nudge = self.nudge
br = self.label.boundingRect()
p = QtCore.QPointF(0, 0)
if self.orientation == "left":
p.setY(int(self.size().height() / 2 + br.width() / 2))
p.setX(-nudge)
elif self.orientation == "right":
p.setY(int(self.size().height() / 2 + br.width() / 2))
p.setX(int(self.size().width() - br.height() + nudge))
elif self.orientation == "top":
p.setY(-nudge)
p.setX(int(self.size().width() / 2.0 - br.width() / 2.0))
elif self.orientation == "bottom":
p.setX(int(self.size().width() / 2.0 - br.width() / 2.0))
p.setY(int(self.size().height() - br.height() + nudge))
self.label.setPos(p)
self.picture = None
app = QtGui.QApplication([])
x = np.linspace(0, 1, 10000)
y = np.linspace(350, 2500, 10000)
win = pg.GraphicsWindow()
plot = win.addPlot(
x=x, y=y, title="Plot", axisItems={"bottom": CustomAxis(orientation="bottom")}
)
label_style = {"color": "#EEE", "font-size": "14pt"}
plot.setLabel("bottom", "some x axis label", **label_style)
plot.setLabel("left", "some y axis label")
plot.getAxis("left").setLabel(**label_style)
font = QtGui.QFont()
font.setPixelSize(14)
plot.getAxis("bottom").tickFont = font
plot.getAxis("bottom").setStyle(tickTextOffset=50)
plot.getAxis("left").tickFont = font
plot.getAxis("left").setStyle(tickTextOffset=14)
def on_timeout():
plot.getAxis("bottom").nudge += 1
timer = QtCore.QTimer(timeout=on_timeout, interval=500)
timer.start()
if __name__ == "__main__":
import sys
if (sys.flags.interactive != 1) or not hasattr(QtCore, "PYQT_VERSION"):
QtGui.QApplication.instance().exec_()

Related

pyqtgraph: add crosshair on mouse_x graph_y

I want to add a vertical line following the mouse_x position (working) and a horizontal line following the curve (and not mouse_y). In the pyqtgraph crosshair example, it shows how to add a crosshair following the mouse_x and mouse_y position. But that does not help that much.
The following code sets the vertical line position to mouse_x postion. But i dont know how to set the horizontal line's postion to the curves current y position (depending on where the mouse x position is).
data1 = 10000 + 15000 * pg.gaussianFilter(np.random.random(size=10000), 10) + 3000 * np.random.random(size=10000)
def mouseMoved(evt):
pos = evt[0]
if p1.sceneBoundingRect().contains(pos):
mousePoint = vb.mapSceneToView(pos)
index = int(mousePoint.x())
if index > 0 and index < len(data1):
label.setText("<span style='font-size: 12pt'>x=%0.1f, <span style='color: red'>y1=%0.1f</span>, <span style='color: green'>y2=%0.1f</span>" % (mousePoint.x(), data1[index], data2[index]))
vLine.setPos(mousePoint.x()) # here i set the vertical line's position to mouse_x position
#hLinePos = vb.mapToView( vLine.pos() )
hLine.setPos(data1[mousePoint.x()]) # <-- how do i set this horizontal line so it is kind of snaped to the curve
Maybe this crosshair widget can point you in the right direction
from PyQt4 import QtCore, QtGui
import sys
import numpy as np
import pyqtgraph as pg
import random
"""Crosshair Plot Widget Example"""
class CrosshairPlotWidget(QtGui.QWidget):
"""Scrolling plot with crosshair"""
def __init__(self, parent=None):
super(CrosshairPlotWidget, self).__init__(parent)
# Use for time.sleep (s)
self.FREQUENCY = .025
# Use for timer.timer (ms)
self.TIMER_FREQUENCY = self.FREQUENCY * 1000
self.LEFT_X = -10
self.RIGHT_X = 0
self.x_axis = np.arange(self.LEFT_X, self.RIGHT_X, self.FREQUENCY)
self.buffer = int((abs(self.LEFT_X) + abs(self.RIGHT_X))/self.FREQUENCY)
self.data = []
self.crosshair_plot_widget = pg.PlotWidget()
self.crosshair_plot_widget.setXRange(self.LEFT_X, self.RIGHT_X)
self.crosshair_plot_widget.setLabel('left', 'Value')
self.crosshair_plot_widget.setLabel('bottom', 'Time (s)')
self.crosshair_color = (196,220,255)
self.crosshair_plot = self.crosshair_plot_widget.plot()
self.layout = QtGui.QGridLayout()
self.layout.addWidget(self.crosshair_plot_widget)
self.crosshair_plot_widget.plotItem.setAutoVisible(y=True)
self.vertical_line = pg.InfiniteLine(angle=90)
self.horizontal_line = pg.InfiniteLine(angle=0, movable=False)
self.vertical_line.setPen(self.crosshair_color)
self.horizontal_line.setPen(self.crosshair_color)
self.crosshair_plot_widget.setAutoVisible(y=True)
self.crosshair_plot_widget.addItem(self.vertical_line, ignoreBounds=True)
self.crosshair_plot_widget.addItem(self.horizontal_line, ignoreBounds=True)
self.crosshair_update = pg.SignalProxy(self.crosshair_plot_widget.scene().sigMouseMoved, rateLimit=60, slot=self.update_crosshair)
self.start()
def plot_updater(self):
"""Updates data buffer with data value"""
self.data_point = random.randint(1,101)
if len(self.data) >= self.buffer:
del self.data[:1]
self.data.append(float(self.data_point))
self.crosshair_plot.setData(self.x_axis[len(self.x_axis) - len(self.data):], self.data)
def update_crosshair(self, event):
"""Paint crosshair on mouse"""
coordinates = event[0]
if self.crosshair_plot_widget.sceneBoundingRect().contains(coordinates):
mouse_point = self.crosshair_plot_widget.plotItem.vb.mapSceneToView(coordinates)
index = mouse_point.x()
if index > self.LEFT_X and index <= self.RIGHT_X:
self.crosshair_plot_widget.setTitle("<span style='font-size: 12pt'>x=%0.1f, <span style='color: red'>y=%0.1f</span>" % (mouse_point.x(), mouse_point.y()))
self.vertical_line.setPos(mouse_point.x())
self.horizontal_line.setPos(mouse_point.y())
def start(self):
self.timer = QtCore.QTimer()
self.timer.timeout.connect(self.plot_updater)
self.timer.start(self.get_timer_frequency())
def get_crosshair_plot_layout(self):
return self.layout
def get_timer_frequency(self):
return self.TIMER_FREQUENCY
if __name__ == '__main__':
# Create main application window
app = QtGui.QApplication([])
app.setStyle(QtGui.QStyleFactory.create("Cleanlooks"))
mw = QtGui.QMainWindow()
mw.setWindowTitle('Crosshair Plot Example')
# Create and set widget layout
# Main widget container
cw = QtGui.QWidget()
ml = QtGui.QGridLayout()
cw.setLayout(ml)
mw.setCentralWidget(cw)
# Create crosshair plot
crosshair_plot = CrosshairPlotWidget()
ml.addLayout(crosshair_plot.get_crosshair_plot_layout(),0,0)
mw.show()
## Start Qt event loop unless running in interactive mode or using pyside.
if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
QtGui.QApplication.instance().exec_()

How to aesthetically show a generic number of axes in matplotlib?

I want to do a simple GUI that allows the user to add or remove traces from a plot for any number of traces. It looks like this:
The problems I'm having:
I don't know how to make the axes not to superpose with each other for a generic number of plots.
When I plot more than one trace, and then delete all but one, there are two axes showing for some reason. There should always be one axis per trace being shown.
Is there a way to fix these issues? You can find my code below. The only function that should be changed is update_canvas(), I believe. To try it out, just modify the list name_vars in the main with the number of variables you want. The rest of the example code is self-contained.
import numpy as np
from matplotlib.backends.qt_compat import QtWidgets
from matplotlib.backends.backend_qt5agg import FigureCanvas
from matplotlib.figure import Figure
class ApplicationWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(ApplicationWindow, self).__init__(parent)
global name_vars
self.x = np.array([1,2,3,4,5])
self.y = np.random.random((5, len(name_vars)))
self.num_vars = np.size(self.y,1)
self.name_vars = name_vars
self.tags_on = [0] * self.num_vars
self.colors = ['#1F77B4','#FF7F0E','#2CA02C','#D62728','#9467BD',
'#8C564B','#E377C2','#F7F7F7','#BCBD22','#17BECF']
self._main = QtWidgets.QWidget()
self.setCentralWidget(self._main)
canvas = FigureCanvas(Figure(figsize=(10, 10)))
self.canvas_ax = canvas.figure.subplots()
self.canvas_ax.set_xlabel("Time")
self.canvas_ax_twin = []
self.list_tags = QtWidgets.QComboBox(self)
for name in self.name_vars:
self.list_tags.addItem(name)
button_add = QtWidgets.QPushButton('Add', self)
button_remove = QtWidgets.QPushButton('Remove', self)
button_add.clicked.connect(self.add_plot)
button_remove.clicked.connect(self.remove_plot)
layout = QtWidgets.QGridLayout(self._main)
layout.addWidget(canvas, 0, 0)
dropdown_layout = QtWidgets.QHBoxLayout()
dropdown_layout.addWidget(self.list_tags)
dropdown_layout.addWidget(button_add)
dropdown_layout.addWidget(button_remove)
layout.addLayout(dropdown_layout, 1, 0)
self.show()
def add_plot(self):
selected_tag = self.list_tags.currentIndex()
self.tags_on[selected_tag] = 1
self.update_canvas()
def remove_plot(self):
selected_tag = self.list_tags.currentIndex()
self.tags_on[selected_tag] = 0
self.update_canvas()
def update_canvas(self):
# Delete all traces
self.canvas_ax.clear()
[i.clear() for i in self.canvas_ax_twin]
self.canvas_ax_twin = []
num_plots = 0
for ii in range(self.num_vars):
if self.tags_on[ii] == 1:
# If it's not the first trace, create a twin axis
if num_plots != 0:
self.canvas_ax_twin.append(self.canvas_ax.twinx())
self.canvas_ax_twin[-1].plot(self.x, self.y[:,ii], self.colors[num_plots])
self.canvas_ax_twin[-1].set_ylabel(self.name_vars[ii])
self.canvas_ax_twin[-1].yaxis.label.set_color(self.colors[num_plots])
self.canvas_ax_twin[-1].tick_params(axis='y', colors=self.colors[num_plots])
num_plots += 1
# If it's the first trace, use the original axis
else:
self.canvas_ax.plot(self.x, self.y[:,ii], self.colors[num_plots])
self.canvas_ax.set_ylabel(self.name_vars[ii])
self.canvas_ax.yaxis.label.set_color(self.colors[num_plots])
self.canvas_ax.tick_params(axis='y', colors=self.colors[num_plots])
num_plots += 1
# Show the final plot
self.canvas_ax.figure.canvas.draw()
if __name__ == '__main__':
# Edit the number of elements in name_vars to try the code
name_vars = ['V1','V2','V3','V4']
app = QtWidgets.QApplication([])
ex = ApplicationWindow()
ex.show()
app.exec_()
I would suggest to separate the logic from the actual plotting. This makes it easier to follow through. This solves the second question about not removing all axes.
The question about not letting the axes superimpose may be solved by setting the position of additional twin axes to some distance from the axes, depending on how many axes you have.
ax.spines["right"].set_position(("axes", 1+(n-1)*0.1))
where n is the axes number starting from 0. The main axes (n=0) should be excluded, and the first axes will stay at position 1. Further axes are positionned in steps of 0.1.
Then it makes sense to also adjust the right margin of the main axes to give enough space for the extra spines.
import numpy as np
from matplotlib.backends.qt_compat import QtWidgets
from matplotlib.backends.backend_qt5agg import FigureCanvas
from matplotlib.figure import Figure
class ApplicationWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None, name_vars=[]):
super(ApplicationWindow, self).__init__(parent)
self.x = np.array([1,2,3,4,5])
self.y = np.random.random((5, len(name_vars)))
self.num_vars = np.size(self.y,1)
self.name_vars = name_vars
self.tags_on = [0] * self.num_vars
self._main = QtWidgets.QWidget()
self.setCentralWidget(self._main)
self.figure = Figure(figsize=(10, 10))
canvas = FigureCanvas(self.figure)
self.left = self.figure.subplotpars.left
self.right = self.figure.subplotpars.right
self.canvas_ax = canvas.figure.subplots()
self.canvas_ax.set_xlabel("Time")
self.axes = [self.canvas_ax]
self.list_tags = QtWidgets.QComboBox(self)
for name in self.name_vars:
self.list_tags.addItem(name)
button_add = QtWidgets.QPushButton('Add', self)
button_remove = QtWidgets.QPushButton('Remove', self)
button_add.clicked.connect(self.add_plot)
button_remove.clicked.connect(self.remove_plot)
layout = QtWidgets.QGridLayout(self._main)
layout.addWidget(canvas, 0, 0)
dropdown_layout = QtWidgets.QHBoxLayout()
dropdown_layout.addWidget(self.list_tags)
dropdown_layout.addWidget(button_add)
dropdown_layout.addWidget(button_remove)
layout.addLayout(dropdown_layout, 1, 0)
self.show()
def add_plot(self):
selected_tag = self.list_tags.currentIndex()
self.tags_on[selected_tag] = 1
self.update_canvas()
def remove_plot(self):
selected_tag = self.list_tags.currentIndex()
self.tags_on[selected_tag] = 0
self.update_canvas()
def create_nth_axes(self, n, dataset):
if n == 0:
ax = self.canvas_ax
else:
ax = self.canvas_ax.twinx()
ax.spines["right"].set_position(("axes", 1+(n-1)*0.1))
for direction in ["left", "bottom", "top"]:
ax.spines[direction].set_visible(False)
# adjust subplotparams to make space for new axes spine
new_right = (self.right-self.left)/(1+(n-1)*0.1)+self.left
self.figure.subplots_adjust(right=new_right)
color = next(self.canvas_ax._get_lines.prop_cycler)['color']
ax.set_ylabel(self.name_vars[dataset], color=color)
ax.plot(self.x, self.y[:,dataset], color=color)
return ax
def clear_canvas(self):
# Clear main axes
self.canvas_ax.clear()
# clear and remove other axes
for ax in self.axes[1:]:
ax.clear()
ax.remove()
self.axes = [self.canvas_ax]
self.figure.subplots_adjust(right=0.9)
def update_canvas(self):
self.clear_canvas()
k = 0
for i, tag in enumerate(self.tags_on):
if tag:
ax = self.create_nth_axes(k, i)
if k > 0:
self.axes.append(ax)
k += 1
self.canvas_ax.figure.canvas.draw()
if __name__ == '__main__':
# Edit the number of elements in name_vars to try the code
name_vars = ['V1','V2','V3','V4']
app = QtWidgets.QApplication([])
ex = ApplicationWindow(name_vars=name_vars)
ex.show()
app.exec_()

Pyqtgraph & Changing color base on height for surfaceplot

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 :

Plotting with pyqtgraph using external data

I'm trying to plot data of diferentes sensors en realtime,so I decided to plot the data using PyQtGraph in PyQt, in order to make it work with several sensor's data from differnte sources.
Searching example on the internet, i found one and i tried to adapt it ,
Because of QtGui.QApplication.instance().exec_(), which carries the inconvenient side effect of blocking the execution of the rest of the code after it. I tried to manage to used threads using Multiproccessing. I could make the rest of the code works, but how coud I update the plot using external data (Plo2D.update2),I tried to used multiprocessing.Queue , but I didn't work, instead appears massage of the window must be closed.
from pyqtgraph.Qt import QtGui, QtCore
import numpy as np
from numpy import arange
import pyqtgraph as pg
import sys
import multiprocessing
class Plot2D():
def __init__(self,):
self.traces = dict()
self.app = QtGui.QApplication([])
self.win = pg.GraphicsWindow(title="Dibujar")
self.win.resize(1000, 600)
self.win.setWindowTitle('Ejemplo')
pg.setConfigOptions(antialias=True)
#self.canvas = self.win.addPlot(title="Pytelemetry")
self.waveform1 = self.win.addPlot(title='WAVEFORM1', row=1, col=1)
self.waveform2 = self.win.addPlot(title='WAVEFORM2', row=2, col=1)
def start(self):
if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
QtGui.QApplication.instance().exec_()
def set_plotdata(self, name, datax, datay):
if name in self.traces:
self.traces[name].setData(datax, datay)
else:
if name == '910D':
self.traces[name] = self.waveform1.plot(pen='c', width=3)
if name == 'MPU':
self.traces[name] = self.waveform2.plot(pen='c', width=3)
def update2(self):
# Trying to get external data
ptm1 = globals()['DatExt1']
ptm2 = globals()['DatExt2']
while ptm1.empty() is False:
self.data1 = ptm1.get()
self.set_plotdata('MPU', self.data1[0], self.data1[1])
# csvWriterG910D.writerows(Informa)
# file1.flush()
while ptm2.empty() is False:
self.data2 = ptm2.get()
self.set_plotdata('910D', self.data1[0], self.data1[1])
def animation(self):
timer = QtCore.QTimer()
timer.timeout.connect(self.update2)
timer.start(60)
self.start()
# It is thread started from main.py
def ShowData(Data1, Data2): # Data1,Data2 : multiprocessing.Queue
DatExt1 = Data1
DatExt2 = Data2
p = Plot2D()
p.animation()
the main.py:
if __name__ == '__main__':
Data1 = multiprocessing.Queue()
Data2 = multiprocessing.Queue()
Plottingdata = Process(target=PlotData.ShowData, args=(Data1, Data2, ))
Plottingdata.start()
t = np.arange(-3.0, 2.0, 0.01)
i = 0.0
while True:
s = np.sin(2 * 2 * 3.1416 * t) / (2 * 3.1416 * t + i)
time.sleep(1)
Data1.put([t, s])
i = i + 0.1
thanks ind advanced for help
Instead of using MultiProcessing you should use MultiThreading, that is, create threads that are responsible for collecting data (in your example emulate data) and then send the data to the GUI by signals.
PlotData.py
from pyqtgraph.Qt import QtGui, QtCore
import numpy as np
import pyqtgraph as pg
import sys
class Plot2D(pg.GraphicsWindow):
def __init__(self):
pg.GraphicsWindow.__init__(self, title="Dibujar")
self.traces = dict()
self.resize(1000, 600)
pg.setConfigOptions(antialias=True)
#self.canvas = self.win.addPlot(title="Pytelemetry")
self.waveform1 = self.addPlot(title='WAVEFORM1', row=1, col=1)
self.waveform2 = self.addPlot(title='WAVEFORM2', row=2, col=1)
def set_plotdata(self, name, x, y):
if name in self.traces:
self.traces[name].setData(x, y)
else:
if name == "910D":
self.traces[name] = self.waveform1.plot(x, y, pen='y', width=3)
elif name == "MPU":
self.traces[name] = self.waveform2.plot(x, y, pen='y', width=3)
#QtCore.pyqtSlot(str, tuple)
def updateData(self, name, ptm):
x, y = ptm
self.set_plotdata(name, x, y)
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
plot = Plot2D()
if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
QtGui.QApplication.instance().exec_()
main.py
import sys
from pyqtgraph.Qt import QtCore, QtGui
import threading
import numpy as np
import time
from PlotData import Plot2D
class Helper(QtCore.QObject):
changedSignal = QtCore.pyqtSignal(str, tuple)
def create_data1(helper, name):
t = np.arange(-3.0, 2.0, 0.01)
i = 0.0
while True:
s = np.sin(2 * 2 * 3.1416 * t) / (2 * 3.1416 * t + i)
time.sleep(.1)
helper.changedSignal.emit(name, (t, s))
i = i + 0.1
def create_data2(helper, name):
t = np.arange(-3.0, 2.0, 0.01)
i = 0.0
while True:
s = np.cos(2 * 2 * 3.1416 * t) / (2 * 3.1416 * t - i)
time.sleep(.1)
helper.changedSignal.emit(name, (t, s))
i = i + 0.1
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
helper = Helper()
plot = Plot2D()
helper.changedSignal.connect(plot.updateData, QtCore.Qt.QueuedConnection)
threading.Thread(target=create_data1, args=(helper, "910D"), daemon=True).start()
threading.Thread(target=create_data2, args=(helper, "MPU"), daemon=True).start()
if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
QtGui.QApplication.instance().exec_()

Two Y-scales in pyqtgraph (twinx-like)

How can I generate a plot with two Y-scales in pyqtgraph?
I also need the two in different colors (corresponding to lines' colors).
In matplotlib it can be done using twinx, as in this example.
If there's no way to do it with a single plot object, perhaps there's a way to overlay a plot (with y-axis on right side) on another one (with the y-axis on left)?
See pyqtgraph/examples/MultiplePlotAxes.py.
The solution is just what you described--overlay two PlotItems.
Here is some code that I think shows a practical example of what it is you are after. This is an expansion of two pyqtgraph examples: PlotSpeedTest.py and MultiplePlotAxes.py.
from pyqtgraph.Qt import QtGui, QtCore
import numpy as np
import pyqtgraph as pg
pg.setConfigOptions(antialias=True)
pg.setConfigOption('background', '#c7c7c7')
pg.setConfigOption('foreground', '#000000')
from pyqtgraph.ptime import time
app = QtGui.QApplication([])
p = pg.plot()
p.setXRange(0,10)
p.setYRange(-10,10)
p.setWindowTitle('Current-Voltage')
p.setLabel('bottom', 'Bias', units='V', **{'font-size':'20pt'})
p.getAxis('bottom').setPen(pg.mkPen(color='#000000', width=3))
p.setLabel('left', 'Current', units='A',
color='#c4380d', **{'font-size':'20pt'})
p.getAxis('left').setPen(pg.mkPen(color='#c4380d', width=3))
curve = p.plot(x=[], y=[], pen=pg.mkPen(color='#c4380d'))
p.showAxis('right')
p.setLabel('right', 'Dynamic Resistance', units="<font>Ω</font>",
color='#025b94', **{'font-size':'20pt'})
p.getAxis('right').setPen(pg.mkPen(color='#025b94', width=3))
p2 = pg.ViewBox()
p.scene().addItem(p2)
p.getAxis('right').linkToView(p2)
p2.setXLink(p)
p2.setYRange(-10,10)
curve2 = pg.PlotCurveItem(pen=pg.mkPen(color='#025b94', width=1))
p2.addItem(curve2)
def updateViews():
global p2
p2.setGeometry(p.getViewBox().sceneBoundingRect())
p2.linkedViewChanged(p.getViewBox(), p2.XAxis)
updateViews()
p.getViewBox().sigResized.connect(updateViews)
x = np.arange(0, 10.01,0.01)
data = 5+np.sin(30*x)
data2 = -5+np.cos(30*x)
ptr = 0
lastTime = time()
fps = None
def update():
global p, x, curve, data, curve2, data2, ptr, lastTime, fps
if ptr < len(x):
curve.setData(x=x[:ptr], y=data[:ptr])
curve2.setData(x=x[:ptr], y=data2[:ptr])
ptr += 1
now = time()
dt = now - lastTime
lastTime = now
if fps is None:
fps = 1.0/dt
else:
s = np.clip(dt*3., 0, 1)
fps = fps * (1-s) + (1.0/dt) * s
p.setTitle('%0.2f fps' % fps)
else:
ptr = 0
app.processEvents() ## force complete redraw for every plot. Try commenting out to see if a different in speed occurs.
timer = QtCore.QTimer()
timer.timeout.connect(update)
timer.start(0)
if __name__ == '__main__':
import sys
if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
QtGui.QApplication.instance().exec_()

Categories

Resources