PyQt6+pyqtgraph折線圖繪制顯示功能
1、實現(xiàn)效果

2、環(huán)境:
確認已經安裝pyqtgraph的模塊,如果沒有安裝,使用命令安裝:
pip install pyqtgraph
3、代碼實現(xiàn):
繪制折線函數(shù):
import sys
import random
from PySide6.QtWidgets import QApplication, QGraphicsView, QGraphicsScene, QGraphicsItem
from PySide6.QtCore import Qt, QPointF, QRectF, QTimer
from PySide6.QtGui import QPainter, QPen, QColor, QFont
class ScrollableLineGraph(QGraphicsItem):
def __init__(self, data):
"""
初始化線圖項,設置初始顯示范圍和數(shù)據(jù)
:param data: 要顯示的數(shù)據(jù)列表
"""
super().__init__()
self.data = data
self.current_start = 0 # 當前顯示的起始索引
self.bounding_rect = QRectF(0, 0, 900, 200) # 線圖的邊界矩形
def boundingRect(self):
"""
返回線圖的邊界矩形,用于確定繪制區(qū)域
:return: 矩形邊界
"""
return self.bounding_rect
def paint(self, painter, option, widget):
"""
繪制線圖,包括坐標軸、刻度和數(shù)據(jù)線
:param painter: 繪制工具
:param option: 繪制選項
:param widget: 關聯(lián)的窗口部件
"""
painter.setRenderHint(QPainter.Antialiasing) # 開啟抗鋸齒
axis_pen = QPen(QColor(255, 255, 255)) # 坐標軸線顏色
line_pen = QPen(QColor(255, 0, 0)) # 數(shù)據(jù)線顏色
text_font = QFont() # 字體設置
# 繪制x軸
painter.setPen(axis_pen)
painter.drawLine(0, 200, 900, 200)
# 繪制x軸刻度和標簽
for i in range(0, 20, 1):
x = (i - self.current_start) * 45
if 0 <= x < 900:
painter.drawLine(x, 200, x, 210)
painter.drawText(x - 5, 220, str(i + self.current_start))
# 繪制y軸
painter.drawLine(0, 0, 0, 200)
# 繪制y軸刻度和標簽
for i in range(0, 5):
y = 200 - 45 * i
painter.drawLine(-5, y, 0, y)
painter.drawText(-20, y - 5, str(i * 20)) #10:表示Y坐標值間隔大小
# 繪制y軸刻度和標簽,給每個標簽添加上橫向網(wǎng)格,網(wǎng)格為點狀虛線
for i in range(0, 5):
y = 200 - 45 * i
# 網(wǎng)格為點狀虛線
painter.setPen(QPen(QColor(128, 128, 128), 0.5, Qt.DashLine))
# painter.drawLine(-5, y, 0, y)
# painter.drawText(-20, y - 5, str(i * 10))
painter.drawLine(0, y, 900, y)
# 假設已經初始化了painter對象等必要的準備工作
# 定義一些常量,提高代碼的可讀性
CANVAS_HEIGHT = 200
SCALE_FACTOR = 45
LABEL_OFFSET = -20
GRID_LINE_WIDTH = 0.5
GRID_LINE_COLOR = QColor(128, 128, 128)
GRID_LINE_STYLE = Qt.DashLine
MAX_LABELS = 10
# 新增:封裝繪制網(wǎng)格線和刻度標簽的函數(shù)
def draw_scale_and_grid(painter, y_position, label):
"""
繪制y軸刻度和標簽以及對應的橫向網(wǎng)格線
:param painter: QPainter對象,用于繪制
:param y_position: y軸位置
:param label: 刻度標簽的文本
"""
try:
# 繪制網(wǎng)格線
painter.setPen(QPen(GRID_LINE_COLOR, GRID_LINE_WIDTH, GRID_LINE_STYLE))
painter.drawLine(-5, y_position, 0, y_position)
# 繪制刻度標簽
painter.drawText(-LABEL_OFFSET, y_position - 5, label)
# 繪制右側網(wǎng)格線
painter.drawLine(0, y_position, 900, y_position)
except Exception as e:
print(f"Error during drawing scale and grid: {e}")
# 主繪制邏輯
try:
for i in range(MAX_LABELS + 1): # 由于范圍是從0到10,因此循環(huán)次數(shù)應為MAX_LABELS + 1
y = CANVAS_HEIGHT - SCALE_FACTOR * i
# 檢查y位置是否在畫布內,若不在則跳過
if y < 0:
break
label = str(i * 10)
# draw_scale_and_grid(painter, y, label)
except Exception as e:
print(f"Unexpected error occurred: {e}")
# 假設在這段代碼的末尾,有適當?shù)馁Y源釋放邏輯,例如painter對象的銷毀等
# 設置字體
painter.setFont(text_font)
# 繪制x軸和y軸標簽
painter.drawText(900, 205, "Time")
painter.rotate(-90)
painter.drawText(-30,10, "Value")
painter.rotate(90)
# 繪制數(shù)據(jù)線
painter.setPen(line_pen)
painter.setRenderHint(QPainter.SmoothPixmapTransform)
last_point = QPointF(0, 200 - self.data[self.current_start])
for i in range(self.current_start, min(self.current_start + 50, len(self.data))):
x = (i - self.current_start) * 45
y = 200 - self.data[i]
painter.drawLine(last_point, QPointF(x, y))
last_point = QPointF(x, y)在QT Designer的Widget頁面添加Graphics View,命名為graphicsView
在頁面類中定義圖表初始化和數(shù)據(jù)更新方法:
def GraphWidget_uiInit(self):
data = []
scene = QGraphicsScene(self.graphicsView)
self.graphicsView.setScene(scene)
self.data_graph = ScrollableLineGraph(data)
scene.addItem(self.data_graph)
scene.setBackgroundBrush
self.graphicsView.setRenderHint(QPainter.Antialiasing)
self.graphicsView.setFixedSize(960, 240)
self.graphicsView.setSceneRect(0, 0, 900, 220)
self.graphicsView.setHorizontalScrollBarPolicy(Qt.ScrollBarAsNeeded)
# # 設置定時器,用于動態(tài)更新數(shù)據(jù)
# self.timer = QTimer(self)
# self.timer.timeout.connect(self.update_data)
# self.timer.start(60) # 每500毫秒觸發(fā)一次
def update_data(self):
#隨機數(shù)據(jù)
# new_data = random.randint(0, 100)
# self.data_graph.data.append(new_data)
#將字符串數(shù)據(jù)轉換成int類型的列表數(shù)據(jù)
dataStr8 = "100 30 178 100 69 60 98 98 79 50 30 20 29 58 69 39 98 29 32"
list_from_string8 = dataStr8.split()
number_list8 = list(map(int, list_from_string8))
self.data_graph.data = number_list8
self.data_graph.current_start = max(0, len(self.data_graph.data) - 50)
self.data_graph.update()參考資料:
https://blog.csdn.net/hyd_csdn/article/details/140644014
到此這篇關于PyQt6+pyqtgraph折線圖繪制顯示的文章就介紹到這了,更多相關PyQt6 pyqtgraph折線圖繪制內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Python實現(xiàn)購物評論文本情感分析操作【基于中文文本挖掘庫snownlp】
這篇文章主要介紹了Python實現(xiàn)購物評論文本情感分析操作,結合實例形式分析了Python使用中文文本挖掘庫snownlp操作中文文本進行感情分析的相關實現(xiàn)技巧與注意事項,需要的朋友可以參考下2018-08-08
Python中urlencode()函數(shù)構建URL查詢字符串的利器學習
這篇文章主要為大家介紹了Python中urlencode()函數(shù)構建URL查詢字符串的利器學習,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-10-10
解決pycharm下載庫時出現(xiàn)Failed to install package的問題
很多小伙伴遇到pycharm下載庫時出現(xiàn)Failed to install package不知道怎么解決,下面小編給大家?guī)砹私鉀Q方法,需要的朋友參考下吧2021-09-09
flask post獲取前端請求參數(shù)的三種方式總結
這篇文章主要介紹了flask post獲取前端請求參數(shù)的三種方式總結,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-12-12
Python使用pickle模塊實現(xiàn)序列化功能示例
這篇文章主要介紹了Python使用pickle模塊實現(xiàn)序列化功能,結合實例形式分析了基于pickle模塊的序列化操作相關操作技巧,需要的朋友可以參考下2018-07-07

