求助为什么绘制不了折线图
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'ttt.ui'
#
# Created by: PyQt5 UI code generator 5.15.4
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(761, 440)
self.widget = QtWidgets.QWidget(Form)
self.widget.setGeometry(QtCore.QRect(10, 10, 441, 291))
self.widget.setObjectName("widget")
self.verticalLayout_2 = QtWidgets.QVBoxLayout(self.widget)
self.verticalLayout_2.setContentsMargins(0, 0, 0, 0)
self.verticalLayout_2.setObjectName("verticalLayout_2")
self.verticalLayout = QtWidgets.QVBoxLayout()
self.verticalLayout.setObjectName("verticalLayout")
self.verticalLayout_2.addLayout(self.verticalLayout)
self.pushButton = QtWidgets.QPushButton(Form)
self.pushButton.setGeometry(QtCore.QRect(40, 370, 201, 31))
self.pushButton.setObjectName("pushButton")
self.lineEdit = QtWidgets.QLineEdit(Form)
self.lineEdit.setGeometry(QtCore.QRect(100, 330, 121, 21))
self.lineEdit.setObjectName("lineEdit")
self.label = QtWidgets.QLabel(Form)
self.label.setGeometry(QtCore.QRect(50, 330, 51, 16))
self.label.setObjectName("label")
self.label_2 = QtWidgets.QLabel(Form)
self.label_2.setGeometry(QtCore.QRect(230, 330, 54, 12))
self.label_2.setObjectName("label_2")
self.label_3 = QtWidgets.QLabel(Form)
self.label_3.setGeometry(QtCore.QRect(480, 180, 201, 201))
self.label_3.setText("")
self.label_3.setPixmap(QtGui.QPixmap("../图像材料/black_light.jpg"))
self.label_3.setObjectName("label_3")
self.label_4 = QtWidgets.QLabel(Form)
self.label_4.setGeometry(QtCore.QRect(480, 180, 201, 201))
self.label_4.setText("")
self.label_4.setPixmap(QtGui.QPixmap("../图像材料/green_light.jpg"))
self.label_4.setObjectName("label_4")
self.label_4.hide()
self.label_5 = QtWidgets.QLabel(Form)
self.label_5.setGeometry(QtCore.QRect(480, 180, 201, 201))
self.label_5.setText("")
self.label_5.setPixmap(QtGui.QPixmap("../图像材料/red_light.jpg"))
self.label_5.setObjectName("label_5")
self.label_5.hide()
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.pushButton.setText(_translate("Form", "导出Excle"))
self.label.setText(_translate("Form", "距离:"))
self.label_2.setText(_translate("Form", "cm"))
# from PyQt5.QtGui import QPixmap
# from threading import Thread
import sys
import time
from collections import deque
import requests
import matplotlib.pyplot as plt
import xlwt
from PyQt5.QtCore import QTimer
from matplotlib import animation
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FC
from matplotlib.figure import Figure
import tui
from PyQt5.QtWidgets import *
class windos(QWidget):
def __init__(self):
super(windos, self).__init__()
self.ui = tui.Ui_Form()
self.ui.setupUi(self)
# self.ui.pushButton.clicked.connect(self.exice)
self.AccessToken()
self.x = []
self.y = []
self.fig, self.axs = plt.subplots(nrows=1, ncols=1, figsize=(10, 5))
self.canvas = FC(self.fig)
self.ui.verticalLayout.addWidget(self.canvas)
self.timer = QTimer(self)
self.timer.timeout.connect(self.init_ui)
self.timer.start(1000) # 每秒更新一次
def AccessToken(self):
url = "http://api.nlecloud.com/users/login"
user = {
'Account': '1xxxxxxx7',
'Password': '1xxxx6'
}
token = {'AccessToken': requests.post(url=url, data=user).json()['ResultObj']['AccessToken']}
return token
def csb_cgq(self):
while 1:
self.csb_shuju = requests.get(url=f"http://api.nlecloud.com/devices/1049542/sensors/m_hum",
params=self.AccessToken()).json()['ResultObj']['Value']
self.csb_shijian = requests.get(url=f"http://api.nlecloud.com/devices/1049542/sensors/m_hum",
params=self.AccessToken()).json()['ResultObj']['RecordTime']
return self.csb_shuju, self.csb_shijian
def init_ui(self):
self.shuju, self.shijian = self.csb_cgq()
print(self.shuju)
print(self.shijian)
self.x.append(self.shijian)
self.y.append(int(self.shuju))
if len(self.x) > 5: # 例如,只保留最近5个数据点
self.x.pop(0)
self.y.pop(0)
print(self.x)
print(self.y)
self.axs.clear()
self.axs.plot(self.x, self.y, label='温度', color='blue', marker='o')
self.axs.set_title('温度')
self.axs.set_xlabel('时间')
self.axs.set_ylabel('温度')
self.axs.legend()
# plt.tight_layout()
# ani = animation.FuncAnimation(self.fig, self.init_ui, interval=1000) # 每秒更新一次
# plt.rcParams['font.sans-serif'] = ['SimHei']
# plt.show()
def exice(self):
self.wb = xlwt.Workbook('utf-8')
self.ws = self.wb.add_sheet('book')
self.ws.write(0, 0, '数据')
self.ws.write(0, 1, '时间')
self.row = 0
for i in range(20):
self.row += 1
self.ws.write(self.row, 0, self.csb_shuju)
self.ws.write(self.row, 1, self.csb_shijian)
if __name__ == '__main__':
app = QApplication(sys.argv)
ui = windos()
ui.show()
sys.exit(app.exec_())
...全文
相关推荐
java折线图绘制程序
自己使用java编写的折线图绘制程序,方便图形绘制
QtChart绘制折线图
QtChart绘制折线图
C#绘制折线图实时展示数据
完全独立出来的一个绘制折线图的例子,直接放到程序中就可以用。可以用来实时展示像温度,脉搏这类的数据。
python绘制多个曲线的折线图
主要为大家详细介绍了python绘制多个曲线的折线图,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
python绘制简单折线图代码示例
1.画最简单的直线图 代码如下: import numpy as np import matplotlib.pyplot as plt x=[0,1] y=[0,1] plt.figure() plt.plot(x,y) plt.savefig("easyplot.jpg") 结果如下: 代码解释: #x轴,y轴 x=[0,1] y=[0,1] #创建绘图对象 plt.figure() #在当前绘图对象进行绘图(两个参数是x,y轴的数据) plt.plot(x,y) #保存图象 plt.savefig("easyplot.jpg") 2.给图加上标签与标题 上面的图没有相应的X,Y


