在 pyqt5 中绘制 Matplotlib 图
Posted
技术标签:
【中文标题】在 pyqt5 中绘制 Matplotlib 图【英文标题】:Ploting Matplotlib plots in pyqt5 【发布时间】:2019-11-13 05:09:21 【问题描述】:我正在尝试使用导航工具栏在 pyqt5 窗口中绘制 matplotlib 图,以便使用 Figure Canvas 在单击时显示下一个图像。但是,代码在 axes.plot 上给出“图像无法转换为浮点数”错误。数据来自 csv,然后使用 matplotlib 绘制。
# -*- coding: utf-8 -*-
from PyQt5 import QtCore, QtGui, QtWidgets
import matplotlib.pyplot as plt
import pandas as pd
from wordcloud import WordCloud
from matplotlib.backends.backend_qt5agg import FigureCanvas
from matplotlib.backends.backend_qt4agg import NavigationToolbar2QT as NavigationToolbar
df=pd.read_csv('Trump.csv')
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(640, 480)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.figure = plt.figure()
self.axes = self.figure.add_subplot(111)
# We want the axes cleared every time plot() is called
#self.axes.hold(False)
self.axes.clear()
self.canvas = FigureCanvas(self.figure)
self.canvas.setParent(self)
self.canvas.move(100,20)
self.canvas.hide()
self.toolbar = NavigationToolbar(self.canvas, self)
self.toolbar.hide()
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 640, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.setupUi(self)
self.show()
def show(self):
wordcloud = WordCloud(random_state=21, max_font_size=110).generate(str(df['Tweets']))
plot2= plt.imshow(wordcloud, interpolation="bilinear")
self.canvas.show()
self.axes.imshow(plot2)
self.canvas.draw()
self.setScene(scene)
plot3=sns.countplot(x = 'Vader', data = df)
self.canvas.show()
self.axes.imshow(plot3)
self.canvas.draw()
self.setScene(scene)
#self.graphicsView.addItem((plt.imshow(wordcloud)))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
任何帮助将不胜感激!
【问题讨论】:
分享.csv..... 谢谢兄弟! drive.google.com/file/d/1qSHDaMu2kw8Xzb7oLlhv42hoKwCfAhGg/… 【参考方案1】:当您将 matplotlib 与 Qt 一起使用时,您不必使用 pyplot,而是使用轴。
import os
import sys
from PyQt5 import QtCore, QtWidgets
from matplotlib.backends.backend_qt5agg import FigureCanvas, NavigationToolbar2QT
from matplotlib.figure import Figure
from wordcloud import WordCloud
import pandas as pd
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.canvas = FigureCanvas(Figure(figsize=(5, 3)))
self.toolbar = NavigationToolbar2QT(self.canvas, self)
self.setCentralWidget(self.canvas)
self.addToolBar(self.toolbar)
self.axes = self.canvas.figure.add_subplot()
current_dir = os.path.dirname(os.path.realpath(__file__))
df = pd.read_csv(os.path.join(current_dir, "Trump.csv"))
wordcloud = WordCloud(random_state=21, max_font_size=110).generate(
str(df["Tweets"])
)
self.axes.imshow(wordcloud)
self.resize(640, 480)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
【讨论】:
谢谢!如何绘制两个图表并从导航工具栏中滑动它们?以上是关于在 pyqt5 中绘制 Matplotlib 图的主要内容,如果未能解决你的问题,请参考以下文章
如何在 Pyqt5 环境中使用 Pickle 保存 Matplotlib 图?
在 PyQt5 中使用 Matplotlib 绘制 CSV 文件