python

关注公众号 jb51net

关闭
首页 > 脚本专栏 > python > PyQt5 QtChart区域图

PyQt5+QtChart实现绘制区域图

作者:SongYuLong的博客

QChart是一个QGraphicScene中可以显示的QGraphicsWidget。本文将利用QtChart实现区域图的绘制,文中的示例代码讲解详细,感兴趣的小伙伴可以了解一下

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区域图内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文