pyqt5加载matplotlib图形

导入相关库

import numpy as np

from PyQt5.Qt import *

from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas

from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar

import matplotlib.pyplot as plt

import numpy

创建几个widgets

self.figure=plt.figure(figsize=(5,4),dpi=100,facecolor='gray') #创建画布

self.canvas=FigureCanvas(self.figure) #把画布添加进渲染器中

self.btn=QPushButton('click')

self.toolbar=NavigationToolbar(self.canvas,self) #创建工具栏,把渲染器加入,父对象这里为窗口,所以用self

self.btn.clicked.connect(self.figure_draw) #绑定事件

布局,使用垂直布局,把上面的控件添加布局器上。

layout=QVBoxLayout()

layout.addWidget(self.canvas)

layout.addWidget(self.btn)

layout.addWidget(self.toolbar)

self.setLayout(layout)

在画布上添加子画布,字画布设置背景颜色为透明可以用两种方法。

# ax2.patch.set_facecolor('none')#背景设置为透明

ax2.patch.set_visible(False) #背景设为透明

import numpy as np
from PyQt5.Qt import *
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as  NavigationToolbar
import matplotlib.pyplot as plt
import numpy

class Window(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("matplot的学习")
        self.resize(700,600)
        self.setup_ui()
        self.count=0


    def setup_ui(self):
        self.figure=plt.figure(figsize=(5,4),dpi=100,facecolor='gray') #创建画布
        self.canvas=FigureCanvas(self.figure) #把画布添加进渲染器中
        self.btn=QPushButton('click')
        self.toolbar=NavigationToolbar(self.canvas,self)
        self.btn.clicked.connect(self.figure_draw)

        #布局
        layout=QVBoxLayout()
        layout.addWidget(self.canvas)
        layout.addWidget(self.btn)
        layout.addWidget(self.toolbar)

        self.setLayout(layout)

    def figure_draw(self):

        ax=self.figure.add_subplot(211)
        ax2=self.figure.add_subplot(212)

        x=np.linspace(0,2*np.pi,50)
        y=np.cos(x)


        ax.plot(x,y)
        ax2.plot(x,np.sin(x))
        # ax2.patch.set_facecolor('none')#背景设置为透明
        ax2.patch.set_visible(False)     #背景设为透明


        self.canvas.draw()

尝试画别的图形

pyqt5加载matplotlib图形_第1张图片

from PyQt5.Qt import *

from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas

from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar

import matplotlib.pyplot as plt

import numpy

class Window(QWidget):

def __init__(self):

super().__init__()

self.setWindowTitle("matplot的学习")

self.resize(700,600)

self.setup_ui()

self.count=0

def setup_ui(self):

self.figure=plt.figure(figsize=(5,4),dpi=100,facecolor='gray') #创建画布

self.canvas=FigureCanvas(self.figure) #把画布添加进渲染器中

self.btn=QPushButton('click')

self.toolbar=NavigationToolbar(self.canvas,self)

self.btn.clicked.connect(self.figure_draw)

#布局

layout=QVBoxLayout()

layout.addWidget(self.canvas)

layout.addWidget(self.btn)

layout.addWidget(self.toolbar)

self.setLayout(layout)

def figure_draw(self):

age_list=['10','21','12','14','25']

name_list=['xuan','shui','Alice','min','guo']

#将age_list数据转化为整型

# age_list=list(map(int,age_list))

age_list=[int(x) for x in age_list]

plt.bar(name_list,age_list,color='g')

# #柱体显示数据

for a,b in zip(name_list,age_list):

plt.text(a,b,f'{b}',ha='center',va='bottom')

plt.title('Demo')

#画图

self.canvas.draw()

#保存图片

plt.savefig('demo.png',transparent=True)#背景为透明

你可能感兴趣的:(matplotlib,python,开发语言)