import pyqtgraph as pg
from datetime import datetime
date_df = pd.read_csv(self.filepath, encoding='gb2312')
def draw1(self):
self.graphicsView1.clear()
load_column = self.comboBox.currentText() # 读取要画的列
times1 = []
for time in data_df.iloc[:, 0]: # 第一列是时间,格式如2020/9/1 0:02
times1.append(datetime.strptime(time, "%Y/%m/%d %H:%M").timestamp()/60) # 先用striptime格式化,再转化成时间戳除以60,即分数绝对值,这样下一分就+1
# plot([x],[y])画图,这里graphicsView要升级成pyqtgraph
# 升级参考 https://zhuanlan.zhihu.com/p/661440594
self.graphicsView1.plot(times1, data_df[load_column], pen='r')
# 或者如下定义pen更多属性
pen1 = pg.mkPen(color=(255, 0, 0), width=15) # 设置曲线颜色为红色,宽度设置为15个像素点
self.graphicsView1.plot(times1, data_df[load_column], pen=pen1)
self.graphicsView1.setLabel('left', load_column) # 左侧坐标轴
self.graphicsView1.setLabel('bottom', date_df.columns[0]) # 底部坐标轴
self.graphicsView1.showGrid(x=True, y=True)
# 更高级玩法,添加图例和打点。如果设置pen=None则画散点图
self.graphicsView1.addLegend()
self.graphicsView1.plot(times1, date_df[load_column], pen=pen1, name="曲线一", symbol='o', symbolSize=5, symbolBrush=('r'))
[Qt-4] PyQt5画图且横坐标为时间
于 2024-07-26 21:45:27 首次发布