PyQt5+QtChart绘制散点图
PyQt5 QtChart-散点图
QScatterSeries类将数据以散点图显示
import sys import random from PyQt5.QtGui import * from PyQt5.QtCore import * from PyQt5.QtWidgets import * from PyQt5.QtCore import Qt from PyQt5.QtChart import QScatterSeries, QChart, QChartView, QPolarChart class MyScatterWindow(QWidget): def __init__(self, parent=None): super(MyScatterWindow, self).__init__(parent) self.resize(800, 600) chart = QChart() chartView = QChartView() print(PYQT_VERSION_STR) scatter = QScatterSeries() for value in range(1, 500): scatter.append(value, random.random()*10) #scatter.append(QPointF(value, random.random()*10)) scatter.setName("散点图") scatter.setMarkerSize(8) # 标记大小 # scatter.setMarkerShape(QScatterSeries.MarkerShapeRectangle) #方形标记 scatter.setMarkerShape(QScatterSeries.MarkerShapeCircle) # 圆形标记 pen = QPen() pen.setColor(Qt.red) pen.setWidth(2) scatter.setPen(pen) chart.addSeries(scatter) chart.createDefaultAxes() chartView.setChart(chart) vbox = QVBoxLayout() vbox.addWidget(chartView) self.setLayout(vbox) if __name__ == "__main__": app = QApplication(sys.argv) win = MyScatterWindow() win.show() sys.exit(app.exec_())
效果图
补充
除了散点图,PytQt5和QtChart还可以绘制其他的图表。下面是小编为大家整理的其他图表绘制的示例代码,需要的可以参考一下
PyQt5 QtChart-饼状图
QPieSeries类将数据以饼状图显示
import sys from PyQt5.QtGui import * from PyQt5.QtCore import * from PyQt5.QtWidgets import * from PyQt5.QtCore import Qt from PyQt5.QtChart import QPieSeries, QPieLegendMarker, QChart, QChartView class MyPieWindow(QWidget): def __init__(self, parent=None): super(MyPieWindow, self).__init__(parent) chart = QChart() chartView = QChartView() pieSeries = QPieSeries() slice0 = pieSeries.append("铁:%10", 1) pieSeries.append("铝:%20", 2) pieSeries.append("铜:%70", 7) pieSeries.setLabelsVisible() pieSeries.setPieSize(0.6) slice0.setExploded() # 外伸 slice0.setColor(QColor(255, 0, 150)) chart.setTitle("饼状图") chart.addSeries(pieSeries) chartView.setChart(chart) chartView.setRenderHint(QPainter.Antialiasing) vbox = QVBoxLayout() vbox.addWidget(chartView) self.setLayout(vbox) if __name__ == "__main__": app = QApplication(sys.argv) win = MyPieWindow() win.show() sys.exit(app.exec_())
PyQt5 QtChart-QLineSeries 折线图
QLineSeries
QLineSeries类将数据序列显示为折线图,其核心代码:
lineSeries = QLineSeries()
lineSeries.append(1, 3)
lineSeries.append(5, 8)
…
chart.addSeries(lineSeries)
常用方法:
- setPointsVisible(True) :设置数据点显示状态
- setPointLabelsVisible(True):设置数据点标签显示状态
- setPointLabelsFormat(“(@xPoint, @yPoint)”):设置数据点标签格式
- setPointLabelsFont(QFont(None, 8)) :设置数据点标签字体
- setPointLabelsColor(QColor(255, 0,0)) :设置数据点标签颜色
import sys from PyQt5.QtGui import * from PyQt5.QtCore import * from PyQt5.QtWidgets import * from PyQt5.QtChart import * import random class MyWaveWindow(QWidget): def __init__(self, parent=None): super(MyWaveWindow, self).__init__(parent) self.setWindowTitle("折线图") # 创建图表 并设置相关参数 chart = QChart() chart.setTitle("随机折线图") chart.setAnimationOptions(QChart.SeriesAnimations) chart.setAnimationDuration(1000) chart.setAnimationEasingCurve(QEasingCurve.InOutCirc) # chart.setTheme(QChart.ChartThemeBlueCerulean) chart.legend().show() # 创建折线数据序列 lineSeries = QLineSeries() for value in range(0, 100): lineSeries.append(value, round(random.random()*10, 2)) lineSeries.setPointsVisible(True) lineSeries.setPointLabelsVisible(True) lineSeries.setPointLabelsFormat("(@xPoint, @yPoint)") lineSeries.setPointLabelsFont(QFont(None, 8)) lineSeries.setPointLabelsColor(QColor(255, 0, 0)) chart.addSeries(lineSeries) # 创建轴坐标 # chart.createDefaultAxes() # 创建默认轴 axis_x = QValueAxis() axis_x.setLabelFormat("%d") axis_x.setMinorTickCount(3) axis_x.setRange(0, 100) chart.addAxis(axis_x, Qt.AlignBottom) lineSeries.attachAxis(axis_x) axis_y = QValueAxis() axis_y.setLabelFormat("%d") # axis_y.setTickType(QValueAxis.TicksDynamic) axis_y.setTickCount(20) axis_y.setMinorTickCount(3) axis_y.setRange(0, 10) chart.addAxis(axis_y, Qt.AlignLeft) lineSeries.attachAxis(axis_y) chartView = QChartView(chart) chartView.setRenderHint(QPainter.Antialiasing) layout = QVBoxLayout() layout.addWidget(chartView) self.setLayout(layout) self.resize(800, 600) if __name__ == "__main__": app = QApplication(sys.argv) win = MyWaveWindow() win.show() app.exit(app.exec_())
到此这篇关于PyQt5+QtChart绘制散点图的文章就介绍到这了,更多相关PyQt5 QtChart散点图内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们
赞 (0)