pyqt用QChart生成折线图每一个点都要用原型标注
时间: 2024-04-30 17:22:34 浏览: 89
可以使用QScatterSeries来实现每一个点用原型标注的折线图,示例代码如下:
```python
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.QtChart import QChart, QChartView, QLineSeries, QScatterSeries, QValueAxis
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
# 创建 QLineSeries 和 QScatterSeries 对象
series = QLineSeries()
scatter_series = QScatterSeries()
# 添加数据
data = [(0, 1), (1, 3), (2, 2), (3, 5), (4, 7), (5, 4), (6, 6)]
for x, y in data:
series.append(x, y)
scatter_series.append(x, y)
# 设置 QScatterSeries 的样式
scatter_series.setMarkerSize(10)
scatter_series.setMarkerShape(QScatterSeries.MarkerShapeCircle)
# 创建 QChart 对象,并添加 QLineSeries 和 QScatterSeries
chart = QChart()
chart.addSeries(series)
chart.addSeries(scatter_series)
# 设置 X 轴和 Y 轴范围
axis_x = QValueAxis()
axis_x.setRange(0, 6)
axis_y = QValueAxis()
axis_y.setRange(0, 8)
# 将 X 轴和 Y 轴添加到 QChart 中
chart.addAxis(axis_x, chart.AxisX)
chart.addAxis(axis_y, chart.AxisY)
# 将 QLineSeries 和 QScatterSeries 与 X 轴和 Y 轴关联
series.attachAxis(axis_x)
series.attachAxis(axis_y)
scatter_series.attachAxis(axis_x)
scatter_series.attachAxis(axis_y)
# 创建 QChartView 对象,并将 QChart 设置为其图表
chart_view = QChartView(chart)
chart_view.setRenderHint(QPainter.Antialiasing)
# 将 QChartView 添加到主窗口中
self.setCentralWidget(chart_view)
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
```
运行程序,可以看到生成的折线图每一个点都用原型标注。可以根据需要调整原型大小和形状。
阅读全文