当前位置:网站首页>Pyside2嵌入Matplotlib的绘图
Pyside2嵌入Matplotlib的绘图
2022-07-17 21:07:00 【初学小白Lu】
import matplotlib
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
# from matplotlib.figure import Figure
import matplotlib.pyplot as plt
通过继承FigureCanvas类,使得该类既是一个PyQt5的Qwidget,又是一个matplotlib的FigureCanvas。
示例:
import sys
from PySide2.QtWidgets import QWidget, QGridLayout, QApplication
import numpy as np
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
import matplotlib.pyplot as plt
class MainWidget(QWidget):
def __init__(self):
super(MainWidget, self).__init__()
self.main_layout = QGridLayout()
self.setLayout(self.main_layout)
widegt1 = QWidget()
widegt1.setStyleSheet("background-color: red")
widget2 = QWidget()
widget2.setStyleSheet("background-color: red")
self.main_layout.addWidget(widegt1, 0, 0, 3, 2)
self.main_layout.addWidget(widget2, 0, 2, 2, 1)
chart = self.gv_visual_data_content = MyFigureCanvas(
xlim=(0, 2*np.pi),
ylim=(-1, 1))
x = np.arange(0, 2 * np.pi, np.pi / 100)
y = np.cos(x)
self.gv_visual_data_content.axes.plot(x, y)
self.gv_visual_data_content.axes.set_title('cos()')
self.main_layout.addWidget(chart, 2, 2, 1, 1)
self.main_layout.setColumnStretch(0, 1)
self.main_layout.setColumnStretch(1, 1)
self.main_layout.setColumnStretch(2, 1)
self.main_layout.setRowStretch(0, 1)
self.main_layout.setRowStretch(1, 1)
self.main_layout.setRowStretch(2, 1)
class MyFigureCanvas(FigureCanvas):
''' 通过继承FigureCanvas类,使得该类既是一个PyQt5的Qwidget,又是一个matplotlib的FigureCanvas,这是连接pyqt5与matplotlib的关键 '''
def __init__(self, parent=None, width=10, height=5, xlim=(0, 2500), ylim=(-2, 2), dpi=100):
# 创建一个Figure
fig = plt.Figure(figsize=(width, height), dpi=dpi, tight_layout=True) # tight_layout: 用于去除画图时两边的空白
FigureCanvas.__init__(self, fig) # 初始化父类
self.setParent(parent)
self.axes = fig.add_subplot(111) # 调用figure下面的add_subplot方法,类似于matplotlib.pyplot下面的subplot方法
self.axes.spines['top'].set_visible(False) # 去掉上面的横线
self.axes.spines['right'].set_visible(False)
self.axes.set_xlim(xlim)
self.axes.set_ylim(ylim)
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWidget()
window.show()
sys.exit(app.exec_())
边栏推荐
- 手册不全,如何手工刨出TongWeb的监控信息?
- Unity subtitle scrolling
- 同一宿主机不同容器网络通信流程分析
- STM32 positioning hardfault location method and encountered situation in keil environment
- Tencent cloud object storage operation process
- Huawei wireless device configuration dynamic load balancing
- After 2000, he was hired as a special associate researcher of Nanjing University. He went to primary school at the age of 4 and was admitted to Nanjing University at the age of 14!
- Colliding mice collision engineering analysis
- Several small open source projects of mine over the years
- matplotlib绘制多折线图(解决matplotlib中文无法显示问题)
猜你喜欢

STM32 positioning hardfault location method and encountered situation in keil environment

Keil环境下STM32定位hardfault位置方法和遇到的情况

数据库的增删改查

Colliding mice collision engineering analysis

华为无线设备配置静态负载均衡

Use of Google browser developer tools (Master!)

揭开服务网格~Istio Service Mesh神秘的面纱

坐标模拟矩阵旋转的公式

Unveil the mystery of service grid istio service mesh

What are the ways to realize load balancing?
随机推荐
Compréhension initiale de la fonction - partie 2
Huawei Technologies:Jonatan Krolikowski | 从设计到部署零接触深度强化学习WLANs
The TLS connection failed due to the incorrect configuration of privatekeyentry in the keystore file
Ranking of top ten ERP software systems at home and abroad!
Code Runner for VS Code,下载量突破 4000 万!支持超过50种语言
Interview records
Is it safe for Hongye futures to open an account online? Are there any account opening guidelines?
STM32 positioning hardfault location method and encountered situation in keil environment
【ACWing】2492. HH Necklace
研二非科班研究生如何备战秋招
Luo Gu: p3092 [usaco13nov]no change G
Take a look at try{}catch{}
Colliding Mice碰撞老鼠工程分析
【C语言】扫雷介绍与实现【数组与函数】
陶博士月线反转6.0
活动预告|Apache Doris x Apache SeaTunnel 联合 Meetup 开启报名!
asterisk:No compatible codecs, not accepting this offer!
暑期rhcsa培训第三天作业
Tree and bipartite graph [thinking]
华为无线设备配置智能漫游