def showPie(self):
"""
绘制饼状图
"""
# (1)创建图表和视图
chart = QChart()
chart.setTitle('中国高等教育普及率')
chart.legend().setAlignment(Qt.AlignmentFlag.AlignLeft)
chartView = QChartView(self) # 必须有self,才能在界面上显示图表
chartView.setGeometry(10, 10, 800, 600)
chartView.setChart(chart)
# (2)创建序列并添加数据
pieSet = {'儿童和老人': 35, '劳动人口': 49, '参加高考者': 7, '大学生': 8}
pSeries = QPieSeries()
for item in pieSet.items():
pSeries.append(item[0] + '(%d' % item[1] + '%)', item[1])
pSeries.setLabelsVisible(True)
pSeries.setHoleSize(0.2)
pSeries.setPieSize(0.6)
slice = pSeries.slices()[3]
slice.setExploded(True)
slice.setPen(QPen(Qt.GlobalColor.green, 2))
slice.setBrush(Qt.GlobalColor.yellow)
chart.addSeries(pSeries)
def showBar(self):
"""
绘制柱状图
"""
# (1)创建图表和视图
chart = QChart()
chart.setTitle('2017~2021年高考人数和录取率')
chart.legend().setAlignment(Qt.AlignmentFlag.AlignTop)
chartView = QChartView(self) # 必须有self,才能在界面上显示图表
chartView.setGeometry(10, 10, 800, 600)
chartView.setChart(chart)
# (2)创建序列并添加数据
number_signup = [940, 975, 1031, 1071, 1078]
signupSet = QBarSet('报考')
for i in range(0, 5):
signupSet << number_signup[i]
number_enroll = [700, 791, 820, 967.5, 689]
enrollSet = QBarSet('录取')
for i in range(0, 5):
enrollSet << number_enroll[i]
bSeries = QBarSeries()
bSeries.append(signupSet)
bSeries.append(enrollSet)
bSeries.setLabelsVisible(True)
bSeries.setLabelsPosition(QAbstractBarSeries.LabelsPosition.LabelsInsideEnd)
chart.addSeries(bSeries)
lSeries = QLineSeries()
lSeries.setName('趋势')
for i in range(0, 5):
lSeries.append(i, number_enroll[i])
pen = QPen(Qt.GlobalColor.green)
pen.setWidth(2)
lSeries.setPen(pen)
lSeries.setPointLabelsVisible(True)
lSeries.setPointLabelsFormat('@yPoint 万')
chart.addSeries(lSeries)
# (3)建立和设置坐标轴
year = ['2017', '2018', '2019', '2020', '2021']
axisX = QBarCategoryAxis()
axisX.setTitleText('年份')
axisX.append(year)
chart.addAxis(axisX, Qt.AlignmentFlag.AlignBottom)
bSeries.attachAxis(axisX)
lSeries.attachAxis(axisX)
axisY = QValueAxis()
axisY.setTitleText('人数(万)')
chart.addAxis(axisY, Qt.AlignmentFlag.AlignLeft)
bSeries.attachAxis(axisY)
lSeries.attachAxis(axisY)
def showLine(self):
"""
绘制螺旋曲线图
"""
# (1)创建图表和视图
chart = QChart()
chart.setTitle('螺旋曲线')
chartView = QChartView(self) # 必须有self,才能在界面上显示图表
chartView.setGeometry(10, 10, 800, 600)
chartView.setChart(chart)
# (2)创建序列并添加数据
n = 1000
pointList = np.linspace(1, 10 * 2 * np.pi, n)
lSeries1 = QLineSeries()
lSeries1.setName('Archimedes')
for t in pointList:
x = (1 + 0.618 * t) * np.cos(t)
y = (1 + 0.618 * t) * np.sin(t)
lSeries1.append(x, y)
chart.addSeries(lSeries1)
lSeries2 = QLineSeries()
lSeries2.setName('hyperbolic')
for t in pointList:
x = 10 * 2 * np.pi * (np.cos(t) / t)
y = 10 * 2 * np.pi * (np.sin(t) / t)
lSeries2.append(x, y)
chart.addSeries(lSeries2)
# (3)建立坐标轴
chart.createDefaultAxes()
完整代码:
# -*- coding:utf-8 -*-
"""
------------------------------------------------
File Name: 绘制柱状图.py
Description:
Author: lzq
date:2024-08-03 12:18
------------------------------------------------
"""
import sys
import numpy as np
from PyQt6.QtCharts import QChart, QChartView, QBarSet, QBarSeries, QAbstractBarSeries, QLineSeries, QBarCategoryAxis, \
QValueAxis, QPieSeries
from PyQt6.QtCore import Qt
from PyQt6.QtGui import QPen
from PyQt6.QtWidgets import QDialog, QPushButton, QVBoxLayout, QApplication, QWidget
class MyWidget(QWidget):
def __init__(self):
super(MyWidget, self).__init__()
self.setWindowTitle("二维图表")
# self.showBar()
# self.showPie()
self.showLine()
def showBar(self):
"""
绘制柱状图
"""
# (1)创建图表和视图
chart = QChart()
chart.setTitle('2017~2021年高考人数和录取率')
chart.legend().setAlignment(Qt.AlignmentFlag.AlignTop)
chartView = QChartView(self) # 必须有self,才能在界面上显示图表
chartView.setGeometry(10, 10, 800, 600)
chartView.setChart(chart)
# (2)创建序列并添加数据
number_signup = [940, 975, 1031, 1071, 1078]
signupSet = QBarSet('报考')
for i in range(0, 5):
signupSet << number_signup[i]
number_enroll = [700, 791, 820, 967.5, 689]
enrollSet = QBarSet('录取')
for i in range(0, 5):
enrollSet << number_enroll[i]
bSeries = QBarSeries()
bSeries.append(signupSet)
bSeries.append(enrollSet)
bSeries.setLabelsVisible(True)
bSeries.setLabelsPosition(QAbstractBarSeries.LabelsPosition.LabelsInsideEnd)
chart.addSeries(bSeries)
lSeries = QLineSeries()
lSeries.setName('趋势')
for i in range(0, 5):
lSeries.append(i, number_enroll[i])
pen = QPen(Qt.GlobalColor.green)
pen.setWidth(2)
lSeries.setPen(pen)
lSeries.setPointLabelsVisible(True)
lSeries.setPointLabelsFormat('@yPoint 万')
chart.addSeries(lSeries)
# (3)建立和设置坐标轴
year = ['2017', '2018', '2019', '2020', '2021']
axisX = QBarCategoryAxis()
axisX.setTitleText('年份')
axisX.append(year)
chart.addAxis(axisX, Qt.AlignmentFlag.AlignBottom)
bSeries.attachAxis(axisX)
lSeries.attachAxis(axisX)
axisY = QValueAxis()
axisY.setTitleText('人数(万)')
chart.addAxis(axisY, Qt.AlignmentFlag.AlignLeft)
bSeries.attachAxis(axisY)
lSeries.attachAxis(axisY)
def showPie(self):
"""
绘制饼状图
"""
# (1)创建图表和视图
chart = QChart()
chart.setTitle('中国高等教育普及率')
chart.legend().setAlignment(Qt.AlignmentFlag.AlignLeft)
chartView = QChartView(self) # 必须有self,才能在界面上显示图表
chartView.setGeometry(10, 10, 800, 600)
chartView.setChart(chart)
# (2)创建序列并添加数据
pieSet = {'儿童和老人': 35, '劳动人口': 49, '参加高考者': 7, '大学生': 8}
pSeries = QPieSeries()
for item in pieSet.items():
pSeries.append(item[0] + '(%d' % item[1] + '%)', item[1])
pSeries.setLabelsVisible(True)
pSeries.setHoleSize(0.2)
pSeries.setPieSize(0.6)
slice = pSeries.slices()[3]
slice.setExploded(True)
slice.setPen(QPen(Qt.GlobalColor.green, 2))
slice.setBrush(Qt.GlobalColor.yellow)
chart.addSeries(pSeries)
def showLine(self):
"""
绘制螺旋曲线图
"""
# (1)创建图表和视图
chart = QChart()
chart.setTitle('螺旋曲线')
chartView = QChartView(self) # 必须有self,才能在界面上显示图表
chartView.setGeometry(10, 10, 800, 600)
chartView.setChart(chart)
# (2)创建序列并添加数据
n = 1000
pointList = np.linspace(1, 10 * 2 * np.pi, n)
lSeries1 = QLineSeries()
lSeries1.setName('Archimedes')
for t in pointList:
x = (1 + 0.618 * t) * np.cos(t)
y = (1 + 0.618 * t) * np.sin(t)
lSeries1.append(x, y)
chart.addSeries(lSeries1)
lSeries2 = QLineSeries()
lSeries2.setName('hyperbolic')
for t in pointList:
x = 10 * 2 * np.pi * (np.cos(t) / t)
y = 10 * 2 * np.pi * (np.sin(t) / t)
lSeries2.append(x, y)
chart.addSeries(lSeries2)
# (3)建立坐标轴
chart.createDefaultAxes()
if __name__=='__main__':
app = QApplication(sys.argv)
w = MyWidget()
w.show()
sys.exit(app.exec())