PyQt5+QtChart绘制散点图
短信预约 -IT技能 免费直播动态提醒
PyQt5 QtChart-散点图
QScatterSeries类将数据以散点图显示
import sys
import random
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import Qt
from PyQt5.QtChart import QScatterSeries, QChart, QChartView, QPolarChart
class MyScatterWindow(QWidget):
def __init__(self, parent=None):
super(MyScatterWindow, self).__init__(parent)
self.resize(800, 600)
chart = QChart()
chartView = QChartView()
print(PYQT_VERSION_STR)
scatter = QScatterSeries()
for value in range(1, 500):
scatter.append(value, random.random()*10)
#scatter.append(QPointF(value, random.random()*10))
scatter.setName("散点图")
scatter.setMarkerSize(8) # 标记大小
# scatter.setMarkerShape(QScatterSeries.MarkerShapeRectangle) #方形标记
scatter.setMarkerShape(QScatterSeries.MarkerShapeCircle) # 圆形标记
pen = QPen()
pen.setColor(Qt.red)
pen.setWidth(2)
scatter.setPen(pen)
chart.addSeries(scatter)
chart.createDefaultAxes()
chartView.setChart(chart)
vbox = QVBoxLayout()
vbox.addWidget(chartView)
self.setLayout(vbox)
if __name__ == "__main__":
app = QApplication(sys.argv)
win = MyScatterWindow()
win.show()
sys.exit(app.exec_())
效果图
补充
除了散点图,PytQt5和QtChart还可以绘制其他的图表。下面是小编为大家整理的其他图表绘制的示例代码,需要的可以参考一下
PyQt5 QtChart-饼状图
QPieSeries类将数据以饼状图显示
import sys
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import Qt
from PyQt5.QtChart import QPieSeries, QPieLegendMarker, QChart, QChartView
class MyPieWindow(QWidget):
def __init__(self, parent=None):
super(MyPieWindow, self).__init__(parent)
chart = QChart()
chartView = QChartView()
pieSeries = QPieSeries()
slice0 = pieSeries.append("铁:%10", 1)
pieSeries.append("铝:%20", 2)
pieSeries.append("铜:%70", 7)
pieSeries.setLabelsVisible()
pieSeries.setPieSize(0.6)
slice0.setExploded() # 外伸
slice0.setColor(QColor(255, 0, 150))
chart.setTitle("饼状图")
chart.addSeries(pieSeries)
chartView.setChart(chart)
chartView.setRenderHint(QPainter.Antialiasing)
vbox = QVBoxLayout()
vbox.addWidget(chartView)
self.setLayout(vbox)
if __name__ == "__main__":
app = QApplication(sys.argv)
win = MyPieWindow()
win.show()
sys.exit(app.exec_())
PyQt5 QtChart-QLineSeries 折线图
QLineSeries
QLineSeries类将数据序列显示为折线图,其核心代码:
lineSeries = QLineSeries()
lineSeries.append(1, 3)
lineSeries.append(5, 8)
…
chart.addSeries(lineSeries)
常用方法:
- setPointsVisible(True) :设置数据点显示状态
- setPointLabelsVisible(True):设置数据点标签显示状态
- setPointLabelsFormat(“(@xPoint, @yPoint)”):设置数据点标签格式
- setPointLabelsFont(QFont(None, 8)) :设置数据点标签字体
- setPointLabelsColor(QColor(255, 0,0)) :设置数据点标签颜色
import sys
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtChart import *
import random
class MyWaveWindow(QWidget):
def __init__(self, parent=None):
super(MyWaveWindow, self).__init__(parent)
self.setWindowTitle("折线图")
# 创建图表 并设置相关参数
chart = QChart()
chart.setTitle("随机折线图")
chart.setAnimationOptions(QChart.SeriesAnimations)
chart.setAnimationDuration(1000)
chart.setAnimationEasingCurve(QEasingCurve.InOutCirc)
# chart.setTheme(QChart.ChartThemeBlueCerulean)
chart.legend().show()
# 创建折线数据序列
lineSeries = QLineSeries()
for value in range(0, 100):
lineSeries.append(value, round(random.random()*10, 2))
lineSeries.setPointsVisible(True)
lineSeries.setPointLabelsVisible(True)
lineSeries.setPointLabelsFormat("(@xPoint, @yPoint)")
lineSeries.setPointLabelsFont(QFont(None, 8))
lineSeries.setPointLabelsColor(QColor(255, 0, 0))
chart.addSeries(lineSeries)
# 创建轴坐标
# chart.createDefaultAxes() # 创建默认轴
axis_x = QValueAxis()
axis_x.setLabelFormat("%d")
axis_x.setMinorTickCount(3)
axis_x.setRange(0, 100)
chart.addAxis(axis_x, Qt.AlignBottom)
lineSeries.attachAxis(axis_x)
axis_y = QValueAxis()
axis_y.setLabelFormat("%d")
# axis_y.setTickType(QValueAxis.TicksDynamic)
axis_y.setTickCount(20)
axis_y.setMinorTickCount(3)
axis_y.setRange(0, 10)
chart.addAxis(axis_y, Qt.AlignLeft)
lineSeries.attachAxis(axis_y)
chartView = QChartView(chart)
chartView.setRenderHint(QPainter.Antialiasing)
layout = QVBoxLayout()
layout.addWidget(chartView)
self.setLayout(layout)
self.resize(800, 600)
if __name__ == "__main__":
app = QApplication(sys.argv)
win = MyWaveWindow()
win.show()
app.exit(app.exec_())
到此这篇关于PyQt5+QtChart绘制散点图的文章就介绍到这了,更多相关PyQt5 QtChart散点图内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341