pyqtgraph:将项目从单独的 ViewBox 添加到图例
pyqtgraph: Add Items from seperate ViewBox to Legend
我基本上使用了这个例子中的代码:https://github.com/pyqtgraph/pyqtgraph/blob/develop/examples/MultiplePlotAxes.py 但我使用 pw.addLegend()
添加了一个图例。我的问题是图例只显示轴 1 的项目,我如何让它显示所有轴的数据?
这是我得到的:
图例中未显示的每一行都链接到右侧的一个轴。
当您将 viewBox
添加到主图 scene
时,您正在绕过图例添加机制。这就是为什么只有第 p1.plot([1, 2, 4, 8, 16, 32], name='White plot')
行添加了图例。因此,您必须手动添加图例。这没什么大不了的,因为我们手头有所有的对象。
添加图例时,它 returns 图例对象,您可以使用它来添加其他曲线。
这是为所有曲线添加图例的 MultiplePlotAxes.py 脚本的修改代码:
# -*- coding: utf-8 -*-
"""
Demonstrates a way to put multiple axes around a single plot.
(This will eventually become a built-in feature of PlotItem)
"""
import pyqtgraph as pg
from pyqtgraph.Qt import QtCore, QtGui
pg.mkQApp()
pw = pg.PlotWidget()
# Add legend item
legend = pw.addLegend()
pw.show()
pw.setWindowTitle('pyqtgraph example: MultiplePlotAxes')
p1 = pw.plotItem
p1.setLabels(left='axis 1')
## create a new ViewBox, link the right axis to its coordinate system
p2 = pg.ViewBox()
p1.showAxis('right')
a = p1.scene().addItem(p2)
p1.getAxis('right').linkToView(p2)
p2.setXLink(p1)
p1.getAxis('right').setLabel('axis2', color='#0000ff')
## create third ViewBox.
## this time we need to create a new axis as well.
p3 = pg.ViewBox()
ax3 = pg.AxisItem('right')
p1.layout.addItem(ax3, 2, 3)
p1.scene().addItem(p3)
ax3.linkToView(p3)
p3.setXLink(p1)
ax3.setZValue(-10000)
ax3.setLabel('axis 3', color='#ff0000')
## Handle view resizing
def updateViews():
## view has resized; update auxiliary views to match
global p1, p2, p3
p2.setGeometry(p1.vb.sceneBoundingRect())
p3.setGeometry(p1.vb.sceneBoundingRect())
## need to re-update linked axes since this was called
## incorrectly while views had different shapes.
## (probably this should be handled in ViewBox.resizeEvent)
p2.linkedViewChanged(p1.vb, p2.XAxis)
p3.linkedViewChanged(p1.vb, p3.XAxis)
updateViews()
p1.vb.sigResized.connect(updateViews)
p1.plot([1, 2, 4, 8, 16, 32], name='White plot')
# Create second curve
curve2 = pg.PlotCurveItem([10, 20, 40, 80, 40, 20], pen='b', name='Blue plot')
# Add curve2 into plot legend
legend.addItem(curve2, curve2.name())
# Add plot
plot2 = p2.addItem(curve2)
# Same deal for another curves ...
curve3 = pg.PlotCurveItem([3200, 1600, 800, 400, 200, 100], pen='r', name='Red plot')
legend.addItem(curve3, curve3.name())
p3.addItem(curve3)
## 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_()
我基本上使用了这个例子中的代码:https://github.com/pyqtgraph/pyqtgraph/blob/develop/examples/MultiplePlotAxes.py 但我使用 pw.addLegend()
添加了一个图例。我的问题是图例只显示轴 1 的项目,我如何让它显示所有轴的数据?
这是我得到的:
图例中未显示的每一行都链接到右侧的一个轴。
当您将 viewBox
添加到主图 scene
时,您正在绕过图例添加机制。这就是为什么只有第 p1.plot([1, 2, 4, 8, 16, 32], name='White plot')
行添加了图例。因此,您必须手动添加图例。这没什么大不了的,因为我们手头有所有的对象。
添加图例时,它 returns 图例对象,您可以使用它来添加其他曲线。
这是为所有曲线添加图例的 MultiplePlotAxes.py 脚本的修改代码:
# -*- coding: utf-8 -*-
"""
Demonstrates a way to put multiple axes around a single plot.
(This will eventually become a built-in feature of PlotItem)
"""
import pyqtgraph as pg
from pyqtgraph.Qt import QtCore, QtGui
pg.mkQApp()
pw = pg.PlotWidget()
# Add legend item
legend = pw.addLegend()
pw.show()
pw.setWindowTitle('pyqtgraph example: MultiplePlotAxes')
p1 = pw.plotItem
p1.setLabels(left='axis 1')
## create a new ViewBox, link the right axis to its coordinate system
p2 = pg.ViewBox()
p1.showAxis('right')
a = p1.scene().addItem(p2)
p1.getAxis('right').linkToView(p2)
p2.setXLink(p1)
p1.getAxis('right').setLabel('axis2', color='#0000ff')
## create third ViewBox.
## this time we need to create a new axis as well.
p3 = pg.ViewBox()
ax3 = pg.AxisItem('right')
p1.layout.addItem(ax3, 2, 3)
p1.scene().addItem(p3)
ax3.linkToView(p3)
p3.setXLink(p1)
ax3.setZValue(-10000)
ax3.setLabel('axis 3', color='#ff0000')
## Handle view resizing
def updateViews():
## view has resized; update auxiliary views to match
global p1, p2, p3
p2.setGeometry(p1.vb.sceneBoundingRect())
p3.setGeometry(p1.vb.sceneBoundingRect())
## need to re-update linked axes since this was called
## incorrectly while views had different shapes.
## (probably this should be handled in ViewBox.resizeEvent)
p2.linkedViewChanged(p1.vb, p2.XAxis)
p3.linkedViewChanged(p1.vb, p3.XAxis)
updateViews()
p1.vb.sigResized.connect(updateViews)
p1.plot([1, 2, 4, 8, 16, 32], name='White plot')
# Create second curve
curve2 = pg.PlotCurveItem([10, 20, 40, 80, 40, 20], pen='b', name='Blue plot')
# Add curve2 into plot legend
legend.addItem(curve2, curve2.name())
# Add plot
plot2 = p2.addItem(curve2)
# Same deal for another curves ...
curve3 = pg.PlotCurveItem([3200, 1600, 800, 400, 200, 100], pen='r', name='Red plot')
legend.addItem(curve3, curve3.name())
p3.addItem(curve3)
## 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_()