当前位置:网站首页>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_())
边栏推荐
- Use of Google browser developer tools (Master!)
- Redis源码与设计剖析 -- 4.跳跃表
- 【ACWing】2492. HH Necklace
- Overview report of Chinese AI medical imaging industry in 2022
- 华为无线设备配置用户CAC
- TongWeb生产系统应急处理方案
- Logu: p4516 [jsoi2018] stealth action (tree DP, tree grouping knapsack statistical scheme number)
- Can ping command still play like this?
- Uniapp Gaode map positioning function
- matplotlib绘制多折线图(解决matplotlib中文无法显示问题)
猜你喜欢

Huawei wireless device configuration user CAC

谷歌浏览器开发者工具的使用(掌握!)

面试记录

Installation of Topy Library (topology optimization software)

Brief introduction of Bezier curve

Robotics at Google:Laura Graesser | i-Sim2Real:在紧密的人机交互循环中强化学习机器人策略

00 后博士获聘南大特任副研究员,曾 4 岁上小学,14 岁考入南大!

matplotlib绘制多折线图(解决matplotlib中文无法显示问题)

Huawei wireless devices are configured with static load balancing

Microservice calling component feign practice
随机推荐
FreeRTOS personal notes - multi priority support
Huawei Technologies:Jonatan Krolikowski | 从设计到部署零接触深度强化学习WLANs
Rotation formula of coordinate simulation matrix
版本通告|Apache Doris 1.1 Release 版本正式发布!
009 面试题 SQL语句各部分的执行顺序
Okaleido或杀出NFT重围,你看好它吗?
Brief introduction of Bezier curve
数据库的增删改查
Class 3 practice
Unity subtitle scrolling
How to prepare for the autumn recruitment for the graduate students of the second non science class of Graduate School
Redis源码与设计剖析 -- 1.简单动态字符串
Homework for the third day of summer rhcsa training
Si446 usage record (III): match function
Ranking of top ten ERP software systems at home and abroad!
Go exceed API source code reading (III) -- openreader ()
Unity realizes UI backpack equipment dragging function
跑通Caffe ResNet-50网络实现图片分类——基于华为云Ai1s
活动预告|Apache Doris x Apache SeaTunnel 联合 Meetup 开启报名!
96. Different binary search trees