PyQt5_pyqtgraph散点图

2024-02-26 08:30
文章标签 pyqt5 散点图 pyqtgraph

本文主要是介绍PyQt5_pyqtgraph散点图,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

目录

效果:

代码:

使用:


效果:

代码:

需要的包与字符串横坐标控件

import sys,json
from typing import Dict,Any
from PyQt5 import QtCore,QtGui,QtWidgets
import pyqtgraph as pg
import pyqtgraph.examples
pg.setConfigOption('background','w')
pg.setConfigOption('foreground','k')class RotateAxisItem(pg.AxisItem):def drawPicture(self, p, axisSpec, tickSpecs, textSpecs):p.setRenderHint(p.Antialiasing,False)p.setRenderHint(p.TextAntialiasing,True)## draw long line along axispen,p1,p2 = axisSpecp.setPen(pen)p.drawLine(p1,p2)p.translate(0.5,0)  ## resolves some damn pixel ambiguity## draw ticksfor pen,p1,p2 in tickSpecs:p.setPen(pen)p.drawLine(p1,p2)## draw all text# if self.tickFont is not None:#     p.setFont(self.tickFont)p.setPen(self.pen())for rect,flags,text in textSpecs:# this is the important partp.save()p.translate(rect.x(),rect.y())p.rotate(-30)p.drawText(-rect.width(),rect.height(),rect.width(),rect.height(),flags,text)# restoring the painter is *required*!!!p.restore()

散点图控件

# 散点图
class PyQtGraphScatterWidget(QtWidgets.QWidget):def __init__(self):super().__init__()self.temp_mark = 0self.init_data()self.init_ui()def init_data(self):# https://www.sioe.cn/yingyong/yanse-rgb-16/self.current_point_color = (220,20,60) # 猩红self.point_color = (0,0,255) # 纯蓝passdef init_ui(self):self.title_label = QtWidgets.QLabel('散点图')self.title_label.setAlignment(QtCore.Qt.AlignCenter)xax = RotateAxisItem(orientation='bottom')xax.setHeight(h=50)self.pw = pg.PlotWidget(axisItems={'bottom': xax})self.pw.setMouseEnabled(x=True, y=False)self.pw.setAutoVisible(x=False, y=True)layout = QtWidgets.QVBoxLayout()layout.addWidget(self.title_label)layout.addWidget(self.pw)self.setLayout(layout)passdef set_data(self,data:Dict[str,Any]):scatters = pg.ScatterPlotItem(hoverable=True,hoverPen=pg.mkPen('g'),tip=None)spots3 = []# temp startself.x_ticks = [(0, 'aaa'), (1, 'bbb'), (2, 'ccc'), (3, 'ddd'), (4, 'eee'), (5, 'fff'), (6, 'ggg'), (7, 'hhh'),(8, 'iii'), (9, 'jjj')]xax = self.pw.getAxis('bottom')xax.setTicks([self.x_ticks])x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]self.y = [43.5, 78.9, 23.1, 20.9, 9.6, 33.5, 88.9, 13.1, 10.9, 19.6]for x0,y0 in zip(x,self.y):if x0==5:spots3.append({'pos': (x0, y0),'size': 10,'pen': {'color': self.current_point_color, 'width': 2},'brush': pg.mkBrush(color=self.current_point_color),'symbol':'star'})else:spots3.append({'pos':(x0,y0),'size':5,'pen':{'color':self.point_color,'width':2},'brush':pg.mkBrush(color=self.point_color)})pass# temp endscatters.addPoints(spots3)self.pw.addItem(scatters)self.label = pg.TextItem()self.pw.addItem(self.label)scatters.sigClicked.connect(self.scatter_clicked)scatters.sigHovered.connect(self.scatter_hovered)passdef scatter_clicked(self,plot,points):index_val = points[0].index()print(index_val)passdef scatter_hovered(self,plot,points):if len(points)<=0:returncur_x = points[0].pos()[0]cur_y = points[0].pos()[1]index_val = points[0].index()x_str = self.x_ticks[index_val][1]y_val = self.y[index_val]html_str = '<p style="color:black;font-size:12px;">'+x_str+'&nbsp;'+str(y_val)+'</p>'self.label.setHtml(html_str)self.label.setPos(cur_x,cur_y)print('hovered::',points[0].index(),points[0].pos())

使用:

if __name__ == '__main__':app = QtWidgets.QApplication(sys.argv)t_win = PyQtGraphScatterWidget()t_win.show()t_win.set_data(None)sys.exit(app.exec_())pass

点symbol的样式备忘

这篇关于PyQt5_pyqtgraph散点图的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/748252

相关文章

【Python篇】PyQt5 超详细教程——由入门到精通(终篇)

文章目录 PyQt5超详细教程前言第9部分:菜单栏、工具栏与状态栏9.1 什么是菜单栏、工具栏和状态栏9.2 创建一个简单的菜单栏示例 1:创建带有菜单栏的应用程序代码详解: 9.3 创建工具栏示例 2:创建带有工具栏的应用程序代码详解: 9.4 创建状态栏示例 3:创建带有状态栏的应用程序代码详解: 9.5 菜单栏、工具栏与状态栏的结合示例 4:完整的应用程序界面代码详解: 9.6 总结

【Python篇】PyQt5 超详细教程——由入门到精通(中篇二)

文章目录 PyQt5超详细教程前言第7部分:生成图表与数据可视化7.1 matplotlib 与 PyQt5 的结合7.2 在 PyQt5 中嵌入 matplotlib 图表示例 1:嵌入简单的 matplotlib 图表代码详解: 7.3 动态生成图表示例 2:动态更新图表代码详解: 7.4 在应用程序中展示不同类型的图表示例 3:展示不同类型的图表代码详解: 7.5 总结 第8部分:对话

【Python篇】PyQt5 超详细教程——由入门到精通(序篇)

文章目录 PyQt5 超详细入门级教程前言序篇:1-3部分:PyQt5基础与常用控件第1部分:初识 PyQt5 和安装1.1 什么是 PyQt5?1.2 在 PyCharm 中安装 PyQt51.3 在 PyCharm 中编写第一个 PyQt5 应用程序1.4 代码详细解释1.5 在 PyCharm 中运行程序1.6 常见问题排查1.7 总结 第2部分:创建 PyQt5 应用程序与布局管理2

【YOLO 系列】基于YOLOV8的智能花卉分类检测系统【python源码+Pyqt5界面+数据集+训练代码】

前言: 花朵作为自然界中的重要组成部分,不仅在生态学上具有重要意义,也在园艺、农业以及艺术领域中占有一席之地。随着图像识别技术的发展,自动化的花朵分类对于植物研究、生物多样性保护以及园艺爱好者来说变得越发重要。为了提高花朵分类的效率和准确性,我们启动了基于YOLO V8的花朵分类智能识别系统项目。该项目利用深度学习技术,通过分析花朵图像,自动识别并分类不同种类的花朵,为用户提供一个高效的花朵识别

使用matplotlib绘制散点图、柱状图和饼状图-学习篇

一、散点图 Python代码如下: num_points = 100x = np.random.rand(num_points) #x点位随机y = np.random.rand(num_points) #y点位随机colors = np.random.rand(num_points) #颜色随机sizes = 1000 * np.random.rand(num_points) # 大

【python 散点图】美观画时间序列散点图

经常遇到时间序列的数据,用散点图可以直观的查看数据的分布情况。matplotlib模块的pyplot有画散点图的函数,但是该函数要求x轴是数字类型。pandas的plot函数里,散点图类型’scatter’也要求数字型的,用时间类型的会报错。 最终摸索出画散点图的简单办法。可以使用pyplot的plot_date()画散点图。 # -*- coding: utf-8 -*-"""spee

通过VSCODE搭建PyQt5 来实现GUI界面

1、首先安装好了vscpde并配置了python环境,如果还没有配置,请先配置完成后再来安装。 2、通过pip(配置python环境时会安装pip)的方式来安装pyqt5及pyqt5-tools pip install PyQt5     pip install PyQt5-Tools 3、找到PyQt5工具的designer.exe运行,一般安装在"C:\Users\XXXX\A

pyinstaller 打包 pyqt5 Could not find QtWebEngineProcess.exe

问题 使用 pyinstaller 打包 pyqt5 应用的时候本来运行正常,但在中文路径下不能运行。 然后,升级 pyinstaller 和 pyqt5 到最新版本,再次打包,结果英文路径也不行了,爆出 Could not find QtWebEngineProcess.exe 之后自动退出。 解决方法 很简单,只需要两步: 找到应用程序打包输出的文件夹,复制输出文件夹下的 PyQt5

Pyqt5高级技巧2:Tab顺序、伙伴快捷键、各类常用控件的事件(含基础Demo)

一、编辑Tab顺序         点击下面这个按钮后,按控件调整tab的顺序,设置好后,鼠标聚焦在输入框1中,按一下tab鼠标聚焦会跳到下一个输入框中         编辑tab结束后,按下面这个按钮重新返回页面布局   二、编辑伙伴  (删除伙伴的方法:框选-右键选择全部-删除) 三、设置快捷键(仅MainWindow可用)         例如我菜单(MainWind

【PyQt5 应用程序】PyQt基础组件:树形视图

在探索Python和PyQt制作应用程序的旅程中,树形视图是一个非常强大且常用的组件,尤其是在需要显示层次化数据时。 本节将详细介绍PyQt中树形视图(QTreeView)的使用方法,从基本概念到参数应用举例,希望能帮助你更好地掌握这一组件。 文章目录 树形视图基础自定义项和层次示例应用:任务管理器总结 树形视图基础 树形视图是一种用于展示层次结构数据的图形界面组件,允许用户