网站首页 > 技术文章 正文
温度记录示例
演示如何创建一个带负值的柱状图,来形象地显示温度信息。
首先,创建两个柱状图,并添加数据,其中一组代表最低温度,另一组代表最高温度。
low = QBarSet('最低')
high = QBarSet('最高')
low << -52 << -50 << -45.3 << -37.0 << -25.6 << -8.0 \
<< -6.0 << -11.8 << -19.7 << -32.8 << -43.0 << -48.0
high << 11.9 << 12.8 << 18.5 << 26.5 << 32.0 << 34.8 \
<< 38.2 << 34.8 << 29.8 << 20.4 << 15.1 << 11.8
然后将这两个序列添加到QStackedBarSeries中
barSeries = QStackedBarSeries()
barSeries.append(low)
barSeries.append(high)
创建图表对象,并将上面的序列添加到图表中,使用方法setAnimationOptions(QChart.SeriesAnimations)设置序列动画显示。在横坐标上使用QBarCategoryAxis来标志每个月份,纵坐标表示最低最高温度。然后创建图表视图,添加到窗口中显示出来。
代码
完整代码如下:
import sys
from PyQt5.QtGui import QPainter, QPen
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.QtChart import (QChartView, QChart, QStackedBarSeries, QBarSet,
QLegend, QBarCategoryAxis, QValueAxis)
class DemoStackedBarSeries(QMainWindow):
def __init__(self, parent=None):
super(DemoStackedBarSeries, self).__init__(parent)
# 设置窗口标题
self.setWindowTitle('实战 Qt for Python: 显示温度')
# 设置窗口大小
self.resize(720, 480)
self.createChart()
def createChart(self):
#创建条状单元
low = QBarSet('最低')
high = QBarSet('最高')
low << -52 << -50 << -45.3 << -37.0 << -25.6 << -8.0 \
<< -6.0 << -11.8 << -19.7 << -32.8 << -43.0 << -48.0
high << 11.9 << 12.8 << 18.5 << 26.5 << 32.0 << 34.8 \
<< 38.2 << 34.8 << 29.8 << 20.4 << 15.1 << 11.8
#条状图
barSeries = QStackedBarSeries()
barSeries.append(low)
barSeries.append(high)
#创建图表
chart = QChart()
chart.addSeries(barSeries)
chart.setTitle('摄氏温度记录')
chart.setAnimationOptions(QChart.SeriesAnimations) #设置成动画显示
#设置横向坐标(X轴)
categories = ['一月', '二月', '三月', '四月', '五月', '六月', \
'七月', '八月', '九月', '十月', '十一月', '十二月']
axisX = QBarCategoryAxis()
axisX.append(categories)
axisX.setTitleText('月份')
chart.addAxis(axisX, Qt.AlignBottom)
barSeries.attachAxis(axisX)
#设置纵向坐标(Y轴)
axisY = QValueAxis()
axisY.setRange(-52, 52)
axisY.setTitleText('温度 [°C]')
chart.addAxis(axisY, Qt.AlignLeft)
barSeries.attachAxis(axisY)
#图例属性
chart.legend().setVisible(True)
chart.legend().setAlignment(Qt.AlignBottom)
#图表视图
chartView = QChartView(chart)
chartView.setRenderHint(QPainter.Antialiasing)
self.setCentralWidget(chartView)
if __name__ == '__main__':
app = QApplication(sys.argv)
window = DemoStackedBarSeries()
window.show()
sys.exit(app.exec())
运行结果如下图:
本文知识点
- 使用QStackedBarSeries演示温度可视化显示。
请多多关注,评论,收藏,点赞,和转发。
猜你喜欢
- 2025-07-07 用Python制作数据报告:如何自动生成PDF格式的报告?
- 2025-07-07 20种Python数据可视化绘图 直接复制可用
- 2025-07-07 第十二章:Python与数据处理和可视化
- 2025-07-07 Python数据可视化:比较常用的图表工具库
- 2025-07-07 Python能做出BI软件的联动图表效果?这可能是目前唯一的选择
- 2025-07-07 Python 数据分析必学的 10 个核心库:从基础操作到高阶建模全攻略
- 2025-07-07 每天一个 Python 库:matplotlib 全能绘图神器零基础到进阶!
- 2025-07-07 AI办公自动化-kimi批量在多个Excel工作表中绘制柱状图
- 2025-07-07 我用Python的Seaborn库,绘制了15个超好看图表!
- 2025-07-07 Python数据可视化:从Pandas基础到Seaborn高级应用
- 276℃Python短文,Python中的嵌套条件语句(六)
- 276℃python笔记:for循环嵌套。end=""的作用,图形打印
- 272℃PythonNet:实现Python与.Net代码相互调用!
- 266℃Python操作Sqlserver数据库(多库同时异步执行:增删改查)
- 266℃Python实现字符串小写转大写并写入文件
- 126℃原来2025是完美的平方年,一起探索六种平方的算吧
- 107℃Ollama v0.4.5-v0.4.7 更新集合:Ollama Python 库改进、新模型支持
- 107℃Python 和 JavaScript 终于联姻了!PythonMonkey 要火?
- 最近发表
-
- Python入门体验第二课教学反思:学情把握不准确,意外连连
- Python学不会来打我(95)python工厂函数知识点总结
- Python——使用reduce()方法(python函数reduce)
- Python每日一题(python输出每日一贴)
- Python学不会来打我(92)python代码调试知识总结(五)属性问题
- Python 的 __init__ 一起认识认识
- Python中 __init__的通俗解释是什么?
- Rocky Linux 9 系统初始化与安全加固脚本
- Python构建MCP服务器完整教程:5步打造专属AI工...
- Python的import都在用,可没人告诉我底层是这样的
- 标签列表
-
- python中类 (31)
- python 迭代 (34)
- python 小写 (35)
- python怎么输出 (33)
- python 日志 (35)
- python语音 (31)
- python 工程师 (34)
- python3 安装 (31)
- python音乐 (31)
- 安卓 python (32)
- python 小游戏 (32)
- python 安卓 (31)
- python聚类 (34)
- python向量 (31)
- python大全 (31)
- python次方 (33)
- python桌面 (32)
- python总结 (34)
- python浏览器 (32)
- python 请求 (32)
- python 前端 (32)
- python验证码 (33)
- python 题目 (32)
- python 文件写 (33)
- python中的用法 (32)