python - 如何动态更新 PyQt 条形图?
问题描述
我正在尝试动态创建和更新条形图。我能够创建图表,但是在尝试动态更新它时出现以下错误。
图表是使用QChart创建的。获取来自端口的所有数据的以下错误,并在最后显示图表而不进行任何动态更新。错误说无法添加系列。系列已经在图表上。
下面给出的代码。我是 python 和 PyQt5 的新手,任何线索将不胜感激。
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.resize(800, 600)
self.s = serial.Serial('/dev/pts/1', 9600, timeout=None, parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS)
self.set0 = QBarSet('Count')
# set0.append([random.randint(0, 999) for i in range(3)])
self.series = QBarSeries()
# self.series.append(self.set0)
self.chart = QChart()
# self.chart.addSeries(self.series)
self.chart.setTitle('Bar Chart Demo')
self.chart.setAnimationOptions(QChart.SeriesAnimations)
months = ('Diamond', 'Hexagon', 'Trapezium')
axisX = QBarCategoryAxis()
axisX.append(months)
axisY = QValueAxis()
axisY.setRange(0, 1000)
self.chart.addAxis(axisX, Qt.AlignBottom)
self.chart.addAxis(axisY, Qt.AlignLeft)
self.chart.legend().setVisible(True)
self.chart.legend().setAlignment(Qt.AlignBottom)
self.chartView = QChartView(self.chart)
self.setCentralWidget(self.chartView)
self.drawGraph()
def drawGraph(self):
timeout = 50
timeout_start = time.time()
while time.time() < timeout_start + timeout:
time.sleep(1)
cc = self.s.read(15)
ccread = cc.decode("utf-8")
print(ccread)
diamond = ccread[1:4]
hexa = ccread[6:9]
trep = ccread[11:14]
self.set0.append(int(diamond))
self.set0.append(int(hexa))
self.set0.append(int(trep))
self.series.append(self.set0)
self.chart.addSeries(self.series)
解决方案
推荐阅读
- jquery - 设置数据值属性时的表格排序器 ajax 在初始加载时触发
- c# - Regex replace decimal numbers that do not belong to a hex color value and are not followed by a certain string
- r - 反转转换配方步骤(规范化和记录)的优雅方式?
- javascript - Discord.JS 机器人删除自己的消息
- python - 如何构建表示 XML 文件中所有节点和数据的 Python 字典?
- laravel - 使用 laravel 显示星级
- javascript - Ajax Loadmore + Shuffle.js,结果没有布局
- python - Discord.py hangman game - problem with bot recognizing positions of guessed letters
- .net - WhiteSource 不排除目录
- batch-file - 尝试从 .bat 文件执行 gcc (MinGW64) 时出现“没有这样的文件或目录”错误