开发者

PyQt5+QtChart实现绘制曲线图

目录
  • QSplineSeries
  • 实现代码
  • 效果图

QSplineSeries

QSplineSeries类将数据序列显示为曲线图。核心代码:

spline = QSplineSeries()

spline.append(0, 23)

spline.append(1, 56)

chart.addSeries(lineSeries)

常用方法

  • setPointsVisible(True) :设置数据点显示状态
  • setPointLabelsVisible(True):设置数据点标签显示状态
  • setPointLabelsFormat(“(@xPoint, @yPoint)”):设置数据点标签格式
  • setPointLabelsFont(QFont(None, 8)) :设置数据点标签字体
  • setPointLabelsColor(QColor(255, 0,0)) :设置数据点标签颜色

实现代码

import random
import sys

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


class MySpljavascriptineWindow(QWidget):
    def __init__(se开发者_C培训lf, parent=None):
        super(MySplineWindow, self).__init__(parent)
        self.setWindowTitle("曲线图表演示")

        chart = QChart()
        chart.setTitle(php"曲线图表")
        chart.setAnjavascriptimationDuration(1500)
        chart.setAnimationOptions(QChart.SeriesAnimations)
        chart.setAnimationEasingCurve(QEasingCurve.OutCirc)
        chart.legend().show()

        spline = QSplineSeries()
        for value in range(1, 50):
            spline.append(value, round(random.random()*100))


        spline.setName("随机噪声")
        # spline.setColor(Qt.blue)
        spline.setPointsVisible(True)www.devze.com
        spline.setPointLabelsVisible(True)
        spline.setPointLabelsFormat("(@xPoint, @yPoint)")
        spline.setPointLabelsFont(QFont(None, 6))
        spline.setPointLabelsColor(Qt.darkBlue)
                
        chart.addSeries(spline)
        chart.createDefaultAxes()

        axis_x = QValueAxis()
        axis_x.setLabelFormat("%d")
        chart.addAxis(axis_x, Qt.AlignBottom)
        spline.attachAxis(axis_x)

        chartView = QChartView()
        chartView.setChart(chart)
        chartView.setRenderHint(QPainter.Antialiasing)
        
        vbox = QVBoxLayout()
        vbox.addwidget(chartView)
 javascript       self.setLayout(vbox)


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

效果图

PyQt5+QtChart实现绘制曲线图

增加抗锯齿:chartView.setRenderHint(QPainter.Antialiasing)

PyQt5+QtChart实现绘制曲线图

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新开发

开发排行榜