返回顶部
首页 > 资讯 > 后端开发 > Python >PyQt5+QtChart实现绘制区域图
  • 297
分享到

PyQt5+QtChart实现绘制区域图

PyQt5 QtChart绘制区域图PyQt5 QtChart区域图PyQt5 QtChart 2022-12-16 12:12:28 297人浏览 薄情痞子

Python 官方文档:入门教程 => 点击学习

摘要

PyQt5 QtChart-区域图 QAreaSeries用于创建区域图数据,传入两个QLineSeries对象。核心代码: series0 = QLineSeries() seri

PyQt5 QtChart-区域图

QAreaSeries用于创建区域图数据,传入两个QLineSeries对象。核心代码:

series0 = QLineSeries()
series1 = QLineSeries()
area = QAreaSeries(series0, series1)

示例代码

import sys
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import Qt
from PyQt5.QtChart import QAreaSeries, QLineSeries, QSplineSeries, QValueAxis, QChart, QChartView


class MyAreaWindow(QWidget):
    def __init__(self, parent=None):
        super(MyAreaWindow, self).__init__(parent)
        self.setWindowTitle("区域图")
        chart = QChart()
        chartView = QChartView()
        
        # 创建Series
        series0 = QLineSeries()
        series1 = QLineSeries()

        # 添加数据
        series0 << QPointF(1, 5) << QPointF(3, 7) << QPointF(7, 6) << QPointF(9, 7) \
        << QPointF(12, 6) << QPointF(16, 7) << QPointF(18, 5)
        series1 << QPointF(1, 3) << QPointF(3, 4) << QPointF(7, 3) << QPointF(8, 2) \
        << QPointF(12, 3) << QPointF(16, 4) << QPointF(18, 3)

        # 创建区域图
        area = QAreaSeries(series0, series1)
        area.setName("面积")

        # 设置画笔, pen用于画区域的边框
        pen = QPen(Qt.red)
        pen.setWidth(3)
        area.setPen(pen)

        # 设置画刷,brush用于填充区域
        brush = QBrush(Qt.yellow)
        area.setBrush(brush)

        # gradient = QLinearGradient(QPointF(0, 0), QPointF(0, 1))
        # gradient.setColorAt(0.0, QColor(0x3cc63c))
        # gradient.setColorAt(1.0, QColor(0x16f626))
        # gradient.setCoordinateMode(QGradient.ObjectBoundingMode)
        # area.setBrush(gradient)

        chart.addSeries(area)
        chart.createDefaultAxes()
        chart.axisX().setRange(0, 20)
        chart.axisY().setRange(0, 10)

        chartView.setChart(chart)
        chartView.setRenderHint(QPainter.Antialiasing)

        vbox = QVBoxLayout()
        vbox.addWidget(chartView)
        self.setLayout(vbox)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    win = MyAreaWindow()
    win.show()
    sys.exit(app.exec_())

效果图

补充

当然除了上面的方法,小编还为大家准备了另一种可以实现区域图绘制的方法,希望对大家有所帮助

import sys
from PyQt5.QtCore import Qt, QPointF
from PyQt5.QtGui import QPainter, QPen, QLinearGradient, QColor
from PyQt5.QtWidgets import QApplication, QMainWindow
from  PyQt5.QtChart import QChartView, QChart, QLineSeries, QAreaSeries
 
class DemoChartArea(QMainWindow):
    def __init__(self, parent=None):
        super(DemoChartArea, self).__init__(parent)   
        
         # 设置窗口标题
        self.setWindowTitle('实战 Qt for python: QChart面积图演示')      
        # 设置窗口大小
        self.resize(480, 360)
        
        self.createChart()
        
    def createChart(self):
        
        #面积图的上下两条曲线
        #注意line0和line1的生存周期!!!!!
        self.line0 = QLineSeries()
        self.line1 = QLineSeries()
        
        self.line0 << QPointF(1, 5) << QPointF(3, 7) << QPointF(7, 6) << QPointF(9, 7) << QPointF(12, 6) << QPointF(16, 7) << QPointF(18, 5)
        self.line1 << QPointF(1, 3) << QPointF(3, 4) << QPointF(7, 3) << QPointF(8, 2) << QPointF(12, 3) << QPointF(16, 4) << QPointF(18, 3)
        
        #面积图
        areaSeries = QAreaSeries(self.line0, self.line1)
        areaSeries.setName('蝙蝠侠') #Batman
        
        pen = QPen(QColor(0x059605))
        pen.setWidth(3)
        areaSeries.setPen(pen)
        
        #渐变设置
        gradient = QLinearGradient(QPointF(0,0), QPointF(0, 1))
        gradient.setColorAt(0.0, QColor(0x3cc63c))
        gradient.setColorAt(1.0, QColor(0x26f626))
        gradient.setCoordinateMode(QLinearGradient.ObjectBoundingMode)
        areaSeries.setBrush(gradient)
              
        #创建图表
        chart = QChart()
        chart.addSeries(areaSeries)
        chart.setTitle('简单面积图示例')
        chart.createDefaultAxes()
        chart.axes(Qt.Horizontal)[0].setRange(0, 20) #横坐标数值范围
        chart.axes(Qt.Vertical)[0].setRange(0, 10) #纵坐标数值范围
         
        #图表视图
        chartView = QChartView(chart)
        chartView.setRenderHint(QPainter.Antialiasing)
        
        self.setCentralWidget(chartView)
      
        
if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = DemoChartArea()
    window.show()
    sys.exit(app.exec())   

效果图

到此这篇关于PyQt5+QtChart实现绘制区域图的文章就介绍到这了,更多相关PyQt5 QtChart区域图内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: PyQt5+QtChart实现绘制区域图

本文链接: https://lsjlt.com/news/175027.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作