PlotWidget绘制3条线,并分别设置图例
时间: 2023-05-25 21:03:51 浏览: 64
以下是使用PyQtGraph绘制3条线,并分别设置图例的示例代码:
```python
import sys
from PyQt5 import QtWidgets
import pyqtgraph as pg
app = QtWidgets.QApplication(sys.argv)
# 创建一个PlotWidget对象
plot = pg.PlotWidget()
# 创建三条曲线
curve1 = pg.PlotCurveItem(name='Curve 1')
curve1.setData([1, 2, 3], [1, 3, 2])
curve2 = pg.PlotCurveItem(name='Curve 2')
curve2.setData([1, 2, 3], [2, 1, 3])
curve3 = pg.PlotCurveItem(name='Curve 3')
curve3.setData([1, 2, 3], [3, 2, 1])
# 将曲线添加到PlotWidget中
plot.addItem(curve1)
plot.addItem(curve2)
plot.addItem(curve3)
# 设置图例
legend = plot.addLegend()
legend.addItem(curve1, 'Curve 1')
legend.addItem(curve2, 'Curve 2')
legend.addItem(curve3, 'Curve 3')
# 设置PlotWidget的标题和轴标签
plot.setTitle('Plot')
plot.setLabel('left', 'Y Axis')
plot.setLabel('bottom', 'X Axis')
# 显示PlotWidget
plot.show()
sys.exit(app.exec_())
```
在该示例中,我们首先创建了一个PlotWidget对象,并创建了三条曲线,分别命名为“Curve 1”,“Curve 2”和“Curve 3”。然后,我们将每条曲线添加到PlotWidget中,并设置了相应的图例。最后,我们设置了PlotWidget的标题和轴标签,并显示了它。运行代码后,将会看到一个包含三条线和图例的PlotWidget。