当前位置:网站首页>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_())
边栏推荐
- Comprehensive analysis of C language multimedia open source framework GStreamer
- ClassNotFoundException:com. tongweb. geronimo. osgi. locator. ProviderLocator
- How to quickly calculate the FID, is, sfid, precision, recall and other key evaluation indicators of the generated model?
- The manual is not complete. How to manually dig out the monitoring information of tongweb?
- 版本通告|Apache Doris 1.1 Release 版本正式发布!
- FreeRTOS-空闲任务和阻塞延时的实现
- Optimal biking strategy [DP + two points]
- Use tongweb's hot deployment function with caution
- 智康护物业养老服务方案
- 微信小程序---wxss模板样式
猜你喜欢

The manual is not complete. How to manually dig out the monitoring information of tongweb?

Importerror: DLL load failed while importing win32api: the specified program cannot be found.

Comprehensive analysis of C language multimedia open source framework GStreamer

Silent AI: how does shengteng AI solve the problem of sign language learning with large models?

Redis源码与设计剖析 -- 3.字典

4某公司在6个城市c1,c2,c3…c6中有分公司,已知城市ci到cj(I,j=1,2,3,…6)的联通情况下及费用的大小列于以下带权邻接矩阵中C中

Méthode de compilation de la courbe RPS d'O'Neill (originale par le Dr Tao)

Uniapp Gaode map positioning function

TongWeb生产系统应急处理方案

Brief introduction of Bezier curve
随机推荐
贝塞尔曲线简单介绍
Huawei wireless device configuration dynamic load balancing
Tke (k8s) deploy MySQL using CFS storage
BiShe - online reservation and registration system based on SSM
ClassNotFoundException:com.tongweb.geronimo.osgi.locator.ProviderLocator
华为无线设备配置智能漫游
JVM性能优化
Keil环境下STM32定位hardfault位置方法和遇到的情况
华为无线设备配置静态负载均衡
si446使用记录(三):MATCH功能
A Classical Review of nonconvex optimization problems from Symmetry to Geometry, Rochester University, etc.
AcWing 274. Mobile services [DP]
The NFT market pattern has not changed. Can okaleido set off a new round of waves?
TongWeb生产系统应急处理方案
How to avoid global index in pychart? How to cancel the indexing of a folder?
慎用TongWeb的热部署功能
华为无线设备配置频谱导航
Unity realizes UI backpack equipment dragging function
优秀的jar包启动shell脚本收藏
A review of classical must see for Nonconvex Optimization Problems "from symmetry to geometry", University of Rochester, et al