首頁 > 軟體

PyQt5+QtChart繪製散點圖

2022-12-17 14:00:40

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散點圖內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com


IT145.com E-mail:sddin#qq.com