PyQt5+QtChart实现绘制区域图
短信预约 -IT技能 免费直播动态提醒
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区域图内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341