def test_PlotCurveItem():
p = pg.GraphicsWindow()
p.ci.layout.setContentsMargins(4, 4, 4, 4) # default margins vary by platform
v = p.addViewBox()
p.resize(200, 150)
data = np.array([1,4,2,3,np.inf,5,7,6,-np.inf,8,10,9,np.nan,-1,-2,0])
c = pg.PlotCurveItem(data)
v.addItem(c)
v.autoRange()
# Check auto-range works. Some platform differences may be expected..
checkRange = np.array([[-1.1457564053237301, 16.145756405323731], [-3.076811473165955, 11.076811473165955]])
assert np.allclose(v.viewRange(), checkRange)
assertImageApproved(p, 'plotcurveitem/connectall', "Plot curve with all points connected.")
c.setData(data, connect='pairs')
assertImageApproved(p, 'plotcurveitem/connectpairs', "Plot curve with pairs connected.")
c.setData(data, connect='finite')
assertImageApproved(p, 'plotcurveitem/connectfinite', "Plot curve with finite points connected.")
c.setData(data, connect=np.array([1,1,1,0,1,1,0,0,1,0,0,0,1,1,0,0]))
assertImageApproved(p, 'plotcurveitem/connectarray', "Plot curve with connection array.")
python类GraphicsWindow()的实例源码
def init_viewbox():
"""Helper function to init the ViewBox
"""
global win, vb
win = pg.GraphicsWindow()
win.ci.layout.setContentsMargins(0,0,0,0)
win.resize(200, 200)
win.show()
vb = win.addViewBox()
# set range before viewbox is shown
vb.setRange(xRange=[0, 10], yRange=[0, 10], padding=0)
# required to make mapFromView work properly.
qtest.qWaitForWindowShown(win)
g = pg.GridItem()
vb.addItem(g)
app.processEvents()
def test_plotscene():
tempfilename = tempfile.NamedTemporaryFile(suffix='.svg').name
print("using %s as a temporary file" % tempfilename)
pg.setConfigOption('foreground', (0,0,0))
w = pg.GraphicsWindow()
w.show()
p1 = w.addPlot()
p2 = w.addPlot()
p1.plot([1,3,2,3,1,6,9,8,4,2,3,5,3], pen={'color':'k'})
p1.setXRange(0,5)
p2.plot([1,5,2,3,4,6,1,2,4,2,3,5,3], pen={'color':'k', 'cosmetic':False, 'width': 0.3})
app.processEvents()
app.processEvents()
ex = pg.exporters.SVGExporter(w.scene())
ex.export(fileName=tempfilename)
# clean up after the test is done
os.unlink(tempfilename)
def test_plotscene():
tempfilename = tempfile.NamedTemporaryFile(suffix='.svg').name
print("using %s as a temporary file" % tempfilename)
pg.setConfigOption('foreground', (0,0,0))
w = pg.GraphicsWindow()
w.show()
p1 = w.addPlot()
p2 = w.addPlot()
p1.plot([1,3,2,3,1,6,9,8,4,2,3,5,3], pen={'color':'k'})
p1.setXRange(0,5)
p2.plot([1,5,2,3,4,6,1,2,4,2,3,5,3], pen={'color':'k', 'cosmetic':False, 'width': 0.3})
app.processEvents()
app.processEvents()
ex = pg.exporters.SVGExporter(w.scene())
ex.export(fileName=tempfilename)
# clean up after the test is done
os.unlink(tempfilename)
def init_gui(self):
#self.main_layout = QtWidgets.QVBoxLayout(self)
self.init_main_layout(orientation="vertical")
self.init_attribute_layout()
self.win = pg.GraphicsWindow(title="Expected signal")
self.plot_item = self.win.addPlot(title='Expected ' + self.module.name)
self.plot_item.showGrid(y=True, x=True, alpha=1.)
self.curve = self.plot_item.plot(pen='y')
self.curve_slope = self.plot_item.plot(pen=pg.mkPen('b', width=5))
self.symbol = self.plot_item.plot(pen='b', symbol='o')
self.main_layout.addWidget(self.win)
self.button_calibrate = QtWidgets.QPushButton('Calibrate')
self.main_layout.addWidget(self.button_calibrate)
self.button_calibrate.clicked.connect(lambda: self.module.calibrate())
self.input_calibrated()
def plot_liss(self):
lissx = self.Liss_x.currentText()
lissy = self.Liss_y.currentText()
self.liss_x = self.Liss_x.currentIndex()
self.liss_y = self.Liss_y.currentIndex()
if not (self.channel_states[self.liss_x] and self.channel_states[self.liss_y]):
QtGui.QMessageBox.about(self, 'Error : Insufficient Data', 'Please enable the selected channels in the oscilloscope')
return
self.liss_win = pg.GraphicsWindow(title="Basic plotting examples")
self.liss_win.setWindowTitle('pyqtgraph example: Plotting')
self.p1 = self.liss_win.addPlot(title="Lissajous: x : %s vs y : %s"%(lissx,lissy),x=self.I.achans[self.liss_x].get_yaxis(),y=self.I.achans[self.liss_y].get_yaxis())
self.p1.setLabel('left',lissy);self.p1.setLabel('bottom',lissx)
self.p1.getAxis('left').setGrid(170)
self.p1.getAxis('bottom').setGrid(170)
self.lissvLine = pg.InfiniteLine(angle=90, movable=False,pen=[100,100,200,200])
self.p1.addItem(self.lissvLine, ignoreBounds=True)
self.lisshLine = pg.InfiniteLine(angle=0, movable=False,pen=[100,100,200,200])
self.p1.addItem(self.lisshLine, ignoreBounds=True)
self.vb = self.p1.vb
self.lissproxy = pg.SignalProxy(self.p1.scene().sigMouseClicked, rateLimit=60, slot=self.lissMouseClicked)
def __init__(self, video, result):
self.video = video
self.result = result
app = pg.mkQApp()
self.win = pg.GraphicsWindow(title="Live webcam")
self.win.resize(640,480)
box = self.win.addViewBox(lockAspect=True)
box.invertY()
self.vis = pg.ImageItem()
box.addItem(self.vis)
box = self.win.addViewBox(lockAspect=True)
box.invertY()
self.res = pg.ImageItem()
box.addItem(self.res)
self.win.show()
self.lastUpdate = pg.ptime.time()
self.avgFps = 0.0
def addCompSpecPlot(self, title, sim, comp_id, spec_id, data_size = 1000, x_range = None, y_range = None, measure = "count", **kwargs):
"""
Add plot to display the amount changes of species in a compartment.
Parameters:
* title Title of the plot
* sim STEPS solver
* comp_id ID of the compartment
* spec_id ID of the species
* data_size Size of the data history
* x_range Range of X axis
* y_range Range of y axis
* measure Measure type
* **kwargs Other keywords that are supported by pygraph.GraphicsWindow class
Return:
pyqtgraph.PlotItem object
"""
if title in self.updater:
raise NameError('A Plot with name ' + title + " exists.")
plot = self.addPlot(title = title)
updater = CompSpecUpdater(plot, sim, comp_id, spec_id, data_size, x_range, y_range, measure, **kwargs)
self.updater[title] = updater
return plot
def addTetsSpecPlot(self, title, sim, tets, spec_id, data_size = 1000, x_range = None, y_range = None, measure = "count", **kwargs):
"""
Add plot to display the amount changes of species in a list of tetrahedrons.
Parameters:
* title Title of the plot
* sim STEPS solver
* tets List of tetrahedron indices
* spec_id ID of the species
* data_size Size of the data history
* x_range Range of X axis
* y_range Range of y axis
* **kwargs Other keywords that are supported by pygraph.GraphicsWindow class
Return:
pyqtgraph.PlotItem object
"""
if title in self.updater:
raise NameError('A Plot with name ' + title + " exists.")
plot = self.addPlot(title = title)
updater = TetsSpecUpdater(plot, sim, tets, spec_id, data_size, x_range, y_range, measure, **kwargs)
self.updater[title] = updater
return plot
def addPatchSpecPlot(self, title, sim, patch_id, spec_id, data_size = 1000, x_range = None, y_range = None, **kwargs):
"""
Add plot to display the amount changes of species in a patch.
Parameters:
* title Title of the plot
* sim STEPS solver
* patch_id ID of the patch
* spec_id ID of the species
* data_size Size of the data history
* x_range Range of X axis
* y_range Range of y axis
* **kwargs Other keywords that are supported by pygraph.GraphicsWindow class
Return:
pyqtgraph.PlotItem object
"""
if title in self.updater:
raise NameError('A Plot with name ' + title + " exists.")
plot = self.addPlot(title = title)
updater = PatchSpecUpdater(plot, sim, patch_id, spec_id, data_size, x_range, y_range, **kwargs)
self.updater[title] = updater
return plot
def addTrisSpecPlot(self, title, sim, tris, spec_id, data_size = 1000, x_range = None, y_range = None, **kwargs):
"""
Add plot to display the amount changes of species in a list of triangles.
Parameters:
* title Title of the plot
* sim STEPS solver
* tris List of triangle indices
* spec_id ID of the species
* data_size Size of the data history
* x_range Range of X axis
* y_range Range of y axis
* **kwargs Other keywords that are supported by pygraph.GraphicsWindow class
Return:
pyqtgraph.PlotItem object
"""
if title in self.updater:
raise NameError('A Plot with name ' + title + " exists.")
plot = self.addPlot(title = title)
updater = TrisSpecUpdater(plot, sim, tris, spec_id, data_size, x_range, y_range, **kwargs)
self.updater[title] = updater
return plot
def addPatchSumSpecsPlot(self, title, sim, patch_id, spec_ids, data_size = 1000, x_range = None, y_range = None, **kwargs):
"""
Add plot to display the sum-up amount changes of species in a patch.
Parameters:
* title Title of the plot
* sim STEPS solver
* patch_id ID of the patch
* spec_id ID of the species
* data_size Size of the data history
* x_range Range of X axis
* y_range Range of y axis
* **kwargs Other keywords that are supported by pygraph.GraphicsWindow class
Return:
pyqtgraph.PlotItem object
"""
if title in self.updater:
raise NameError('A Plot with name ' + title + " exists.")
plot = self.addPlot(title = title)
updater = PatchSumSpecsUpdater(plot, sim, patch_id, spec_ids, data_size, x_range, y_range, **kwargs)
self.updater[title] = updater
return plot
def addCompSpecDist(self, title, mesh, sim, comp_id, spec_id, axis = "x", nbins = 20, y_range = None, **kwargs):
"""
Add plot to display the distribution of species in a compartment.
Parameters:
* title Title of the plot
* sim STEPS solver
* comp_id ID of the compartment
* spec_id ID of the species
* axis Spatial direction of the distribution
* nbins Number of bins for the data
* y_range Range of y axis
* **kwargs Other keywords that are supported by pygraph.GraphicsWindow class
Return:
pyqtgraph.PlotItem object
"""
if title in self.updater:
raise NameError('A Plot with name ' + title + " exists.")
plot = self.addPlot(title = title)
updater = CompSpecDistUpdater(plot, mesh, sim, comp_id, spec_id, axis, nbins, y_range, **kwargs)
self.updater[title] = updater
return plot
def addPatchSpecDist(self, title, mesh, sim, patch_id, spec_id, axis = "x", nbins = 20, y_range = None, **kwargs):
"""
Add plot to display the distribution of species in a patch.
Parameters:
* title Title of the plot
* sim STEPS solver
* mesh STEPS mesh
* patch_id ID of the patch
* spec_id ID of the species
* axis Spatial direction of the distribution
* nbins Number of bins for the data
* y_range Range of y axis
* **kwargs Other keywords that are supported by pygraph.GraphicsWindow class
Return:
pyqtgraph.PlotItem object
"""
if title in self.updater:
raise NameError('A Plot with name ' + title + " exists.")
plot = self.addPlot(title = title)
updater = PatchSpecDistUpdater(plot, mesh, sim, patch_id, spec_id, axis, nbins, y_range, **kwargs)
self.updater[title] = updater
return plot
def addTetsSpecDist(self, title, mesh, sim, tets, spec_id, axis = "x", nbins = 20, y_range = None, **kwargs):
"""
Add plot to display the distribution of species in a list of tetrahedrons.
Parameters:
* title Title of the plot
* sim STEPS solver
* mesh STEPS mesh
* tets List of tetrahedron indices
* spec_id ID of the species
* axis Spatial direction of the distribution
* nbins Number of bins for the data
* y_range Range of y axis
* **kwargs Other keywords that are supported by pygraph.GraphicsWindow class
Return:
pyqtgraph.PlotItem object
"""
if title in self.updater:
raise NameError('A Plot with name ' + title + " exists.")
plot = self.addPlot(title = title)
updater = TetsSpecDistUpdater(plot, mesh, sim, tets, spec_id, axis, nbins, y_range, **kwargs)
self.updater[title] = updater
return plot
def addTrisSpecDist(self, title, mesh, sim, tris, spec_id, axis = "x", nbins = 20, y_range = None, **kwargs):
"""
Add plot to display the distribution of species in a list of triangles.
Parameters:
* title Title of the plot
* sim STEPS solver
* mesh STEPS mesh
* tris List of triangle indices
* spec_id ID of the species
* axis Spatial direction of the distribution
* nbins Number of bins for the data
* y_range Range of y axis
* **kwargs Other keywords that are supported by pygraph.GraphicsWindow class
Return:
pyqtgraph.PlotItem object
"""
if title in self.updater:
raise NameError('A Plot with name ' + title + " exists.")
plot = self.addPlot(title = title)
updater = TrisSpecDistUpdater(plot, mesh, sim, tris, spec_id, axis, nbins, y_range, **kwargs)
self.updater[title] = updater
return plot
def addCompSpecPlot(self, title, sim, comp_id, spec_id, data_size = 1000, x_range = None, y_range = None, measure = "count", **kwargs):
"""
Add plot to display the amount changes of species in a compartment.
Parameters:
* title Title of the plot
* sim STEPS solver
* comp_id ID of the compartment
* spec_id ID of the species
* data_size Size of the data history
* x_range Range of X axis
* y_range Range of y axis
* measure Measure type
* **kwargs Other keywords that are supported by pygraph.GraphicsWindow class
Return:
pyqtgraph.PlotItem object
"""
if title in self.updater:
raise NameError('A Plot with name ' + title + " exists.")
plot = self.addPlot(title = title)
updater = CompSpecUpdater(plot, sim, comp_id, spec_id, data_size, x_range, y_range, measure, **kwargs)
self.updater[title] = updater
return plot
def addTetsSpecPlot(self, title, sim, tets, spec_id, data_size = 1000, x_range = None, y_range = None, measure = "count", **kwargs):
"""
Add plot to display the amount changes of species in a list of tetrahedrons.
Parameters:
* title Title of the plot
* sim STEPS solver
* tets List of tetrahedron indices
* spec_id ID of the species
* data_size Size of the data history
* x_range Range of X axis
* y_range Range of y axis
* **kwargs Other keywords that are supported by pygraph.GraphicsWindow class
Return:
pyqtgraph.PlotItem object
"""
if title in self.updater:
raise NameError('A Plot with name ' + title + " exists.")
plot = self.addPlot(title = title)
updater = TetsSpecUpdater(plot, sim, tets, spec_id, data_size, x_range, y_range, measure, **kwargs)
self.updater[title] = updater
return plot
def addPatchSpecPlot(self, title, sim, patch_id, spec_id, data_size = 1000, x_range = None, y_range = None, **kwargs):
"""
Add plot to display the amount changes of species in a patch.
Parameters:
* title Title of the plot
* sim STEPS solver
* patch_id ID of the patch
* spec_id ID of the species
* data_size Size of the data history
* x_range Range of X axis
* y_range Range of y axis
* **kwargs Other keywords that are supported by pygraph.GraphicsWindow class
Return:
pyqtgraph.PlotItem object
"""
if title in self.updater:
raise NameError('A Plot with name ' + title + " exists.")
plot = self.addPlot(title = title)
updater = PatchSpecUpdater(plot, sim, patch_id, spec_id, data_size, x_range, y_range, **kwargs)
self.updater[title] = updater
return plot
def addTrisSpecPlot(self, title, sim, tris, spec_id, data_size = 1000, x_range = None, y_range = None, **kwargs):
"""
Add plot to display the amount changes of species in a list of triangles.
Parameters:
* title Title of the plot
* sim STEPS solver
* tris List of triangle indices
* spec_id ID of the species
* data_size Size of the data history
* x_range Range of X axis
* y_range Range of y axis
* **kwargs Other keywords that are supported by pygraph.GraphicsWindow class
Return:
pyqtgraph.PlotItem object
"""
if title in self.updater:
raise NameError('A Plot with name ' + title + " exists.")
plot = self.addPlot(title = title)
updater = TrisSpecUpdater(plot, sim, tris, spec_id, data_size, x_range, y_range, **kwargs)
self.updater[title] = updater
return plot
def addPatchSumSpecsPlot(self, title, sim, patch_id, spec_ids, data_size = 1000, x_range = None, y_range = None, **kwargs):
"""
Add plot to display the sum-up amount changes of species in a patch.
Parameters:
* title Title of the plot
* sim STEPS solver
* patch_id ID of the patch
* spec_id ID of the species
* data_size Size of the data history
* x_range Range of X axis
* y_range Range of y axis
* **kwargs Other keywords that are supported by pygraph.GraphicsWindow class
Return:
pyqtgraph.PlotItem object
"""
if title in self.updater:
raise NameError('A Plot with name ' + title + " exists.")
plot = self.addPlot(title = title)
updater = PatchSumSpecsUpdater(plot, sim, patch_id, spec_ids, data_size, x_range, y_range, **kwargs)
self.updater[title] = updater
return plot
def addCompSpecDist(self, title, mesh, sim, comp_id, spec_id, axis = "x", nbins = 20, y_range = None, **kwargs):
"""
Add plot to display the distribution of species in a compartment.
Parameters:
* title Title of the plot
* sim STEPS solver
* comp_id ID of the compartment
* spec_id ID of the species
* axis Spatial direction of the distribution
* nbins Number of bins for the data
* y_range Range of y axis
* **kwargs Other keywords that are supported by pygraph.GraphicsWindow class
Return:
pyqtgraph.PlotItem object
"""
if title in self.updater:
raise NameError('A Plot with name ' + title + " exists.")
plot = self.addPlot(title = title)
updater = CompSpecDistUpdater(plot, mesh, sim, comp_id, spec_id, axis, nbins, y_range, **kwargs)
self.updater[title] = updater
return plot
def addPatchSpecDist(self, title, mesh, sim, patch_id, spec_id, axis = "x", nbins = 20, y_range = None, **kwargs):
"""
Add plot to display the distribution of species in a patch.
Parameters:
* title Title of the plot
* sim STEPS solver
* mesh STEPS mesh
* patch_id ID of the patch
* spec_id ID of the species
* axis Spatial direction of the distribution
* nbins Number of bins for the data
* y_range Range of y axis
* **kwargs Other keywords that are supported by pygraph.GraphicsWindow class
Return:
pyqtgraph.PlotItem object
"""
if title in self.updater:
raise NameError('A Plot with name ' + title + " exists.")
plot = self.addPlot(title = title)
updater = PatchSpecDistUpdater(plot, mesh, sim, patch_id, spec_id, axis, nbins, y_range, **kwargs)
self.updater[title] = updater
return plot
def addTetsSpecDist(self, title, mesh, sim, tets, spec_id, axis = "x", nbins = 20, y_range = None, **kwargs):
"""
Add plot to display the distribution of species in a list of tetrahedrons.
Parameters:
* title Title of the plot
* sim STEPS solver
* mesh STEPS mesh
* tets List of tetrahedron indices
* spec_id ID of the species
* axis Spatial direction of the distribution
* nbins Number of bins for the data
* y_range Range of y axis
* **kwargs Other keywords that are supported by pygraph.GraphicsWindow class
Return:
pyqtgraph.PlotItem object
"""
if title in self.updater:
raise NameError('A Plot with name ' + title + " exists.")
plot = self.addPlot(title = title)
updater = TetsSpecDistUpdater(plot, mesh, sim, tets, spec_id, axis, nbins, y_range, **kwargs)
self.updater[title] = updater
return plot
def addTrisSpecDist(self, title, mesh, sim, tris, spec_id, axis = "x", nbins = 20, y_range = None, **kwargs):
"""
Add plot to display the distribution of species in a list of triangles.
Parameters:
* title Title of the plot
* sim STEPS solver
* mesh STEPS mesh
* tris List of triangle indices
* spec_id ID of the species
* axis Spatial direction of the distribution
* nbins Number of bins for the data
* y_range Range of y axis
* **kwargs Other keywords that are supported by pygraph.GraphicsWindow class
Return:
pyqtgraph.PlotItem object
"""
if title in self.updater:
raise NameError('A Plot with name ' + title + " exists.")
plot = self.addPlot(title = title)
updater = TrisSpecDistUpdater(plot, mesh, sim, tris, spec_id, axis, nbins, y_range, **kwargs)
self.updater[title] = updater
return plot
def run(self):
self.app = QtGui.QApplication([])
win = pg.GraphicsWindow(title="Basic plotting examples")
win.resize(1000,600)
win.setWindowTitle('pyqtgraph example: Plotting')
plot = win.addPlot(title=self.name)
self.curve = plot.plot(pen='y')
timer = QtCore.QTimer()
timer.timeout.connect(self._update)
timer.start(50)
self.app.exec_()
try:
self.in_process_pipe.send("closing")
except:
pass
# Process was done, no need to process exception
def test_GraphicsWindow():
def mkobjs():
w = pg.GraphicsWindow()
p1 = w.addPlot()
v1 = w.addViewBox()
return mkrefs(w, p1, v1)
for i in range(5):
assert_alldead(mkobjs())
def test_PlotCurveItem():
p = pg.GraphicsWindow()
p.ci.layout.setContentsMargins(4, 4, 4, 4) # default margins vary by platform
v = p.addViewBox()
p.resize(200, 150)
data = np.array([1,4,2,3,np.inf,5,7,6,-np.inf,8,10,9,np.nan,-1,-2,0])
c = pg.PlotCurveItem(data)
v.addItem(c)
v.autoRange()
# Check auto-range works. Some platform differences may be expected..
checkRange = np.array([[-1.1457564053237301, 16.145756405323731], [-3.076811473165955, 11.076811473165955]])
assert np.allclose(v.viewRange(), checkRange)
assertImageApproved(p, 'plotcurveitem/connectall', "Plot curve with all points connected.")
c.setData(data, connect='pairs')
assertImageApproved(p, 'plotcurveitem/connectpairs', "Plot curve with pairs connected.")
c.setData(data, connect='finite')
assertImageApproved(p, 'plotcurveitem/connectfinite', "Plot curve with finite points connected.")
c.setData(data, connect=np.array([1,1,1,0,1,1,0,0,1,0,0,0,1,1,0,0]))
assertImageApproved(p, 'plotcurveitem/connectarray', "Plot curve with connection array.")
def test_GraphicsWindow():
def mkobjs():
w = pg.GraphicsWindow()
p1 = w.addPlot()
v1 = w.addViewBox()
return mkrefs(w, p1, v1)
for i in range(5):
assert_alldead(mkobjs())
def __init__(self, title="plotwindow"):
self.win = pg.GraphicsWindow(title=title)
self.pw = self.win.addPlot()
self.curves = {}
self.win.show()
self.plot_start_time = time()