如何使用matplotlib和pyplot

时间:2016-10-18 13:53:39

标签: pandas matplotlib pyqt4 seaborn qthread

据我所知,已经发布了一两个相关的问题,但并不完全符合我的需要。我正在构建这个gui,通过单击按钮激活模块。这个通过按下按钮激活的python模块从多个pandas数据帧生成热图并保存这些图像,然后使用pandas ExcelWriter将其保存到xlsx中。

我尝试实现QThread,因为其他stackoverflow示例试图解释类似的问题,但我继续收到此错误:"It is not safe to use pixmaps outside the GUI thread"。据我所知,技术上我并没有在MAIN gui线程中创建热图,但我想QThread我仍然在“一个”gui线程内。这些热图基于的数据帧有时可能很大,我有点理解在创建热图时将信号发送到主gui线程并在主gui类中具有热图功能的概念...但是我担心以后传递如此多的数据会很麻烦......这更像是流水线而不是线程。我只是希望这个工作线程创建这些图像并保存它们然后将这些保存的文件保存到xlsx而不会中断主要的gui ..

(注意:这是一个简化版本,在真实程序中,几乎同时创建了几个这样的线程,每个线程内部将创建几个热图)

--- --- main.py

import sys
from MAIN_GUI import *
from PyQt4 import QtGui, QtCore
from excel_dummy import *

if __name__=="__main__":
    app = QtGui.QApplication(sys.argv)


class MAIN_GUI(QtGui.QMainWindow):
    def __init__(self):
        super(MAIN_GUI, self).__init__()
        self.uiM = Ui_MainWindow()
        self.uiM.setupUi(self)
        self.connect(self.uiM.updateALL_Button,QtCore.SIGNAL('clicked()'),self.newThread)

    def newThread(self):
        Excelify = excelify()
        Excelify.start()
        self.connect(Excelify,QtCore.SIGNAL('donethread(QString)'),(self.done))

    def done(self):
        print('done')


main_gui = MAIN_GUI()
main_gui.show()
main_gui.raise_()
sys.exit(app.exec_())

--- --- excel_dummy.py

import os, pandas as pd
from pandas import ExcelWriter
import numpy as np
import seaborn.matrix as sm

from PyQt4 import QtCore
from PyQt4.QtCore import QThread
from matplotlib.backends.backend_agg import FigureCanvas
from matplotlib.figure import Figure
import time

class excelify(QThread):
    def __init__(self):
        QThread.__init__(self)

    def run(self):
        path = 'home/desktop/produced_files'
        with ExcelWriter(path + '/final.xlsx', engine='xlsxwriter') as writer:
            workbook = writer.book
            worksheet = workbook.add_worksheet()
            heatit = self.heatmap()
            worksheet.insert_image('C3',path + '/' + 'heat.jpg')
            worksheet.write(2, 2, 'just write something')
            writer.save()
        print('file size: %s "%s"' % (os.stat(path).st_size, path))
        time.slee(0.3)
        self.emit(QtCore.SIGNAL('donethread(QString)'),'')

    def heatmap(self):
        df = pd.DataFrame(np.array([[1,22222,33333],[2,44444,55555],[3,44444,22222],[4,55555,33333]]),columns=['hour','in','out'])
        dfu = pd.DataFrame(df.groupby([df.in,df.hour]).size())
        dfu.reset_index(inplace=True)
        dfu.rename(columns={'0':'Count'})
        dfu.columns=['in','hour','Count']
        dfu_2 = dfu.copy()

        mask=0
        fig = Figure()
        ax = fig.add_subplot(1,1,1)
        canvas = FigureCanvas(fig)
        df_heatmap = dfu_2.pivot('in','hour','Count').fillna(0)

        sm.heatmap(df_heatmap,ax=ax,square=True,annot=False,mask=mask)
        fig.savefig(path + '/' + heat.jpg')

--- --- MAIN_GUI.py

from PyQt4 import QtCore,QtGui
try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.unicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName(_fromUtf8("MainWindow"))
        MainWindow.resize(320,201)
        self.centralwidget = QtGui.QWidget(MainWindow)
        self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
        self.updateALL_Button = QtGui.QPushButton(self.centralwidget)
        self.updateALL_Button.setGeometry(QtCore.QRect(40,110,161,27))
        self.updateALL_Button.setFocusPolicy(QtCore.Qt.NoFocus)
        self.updateALL_Button.setObjectName(_fromUtf8("Options_updateALL_Button"))
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtGui.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 320, 24))
        self.menubar.setObjectName(_fromUtf8("menubar"))
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtGui.QStatusBar(MainWindow)
        self.statusbar.setObjectName(_fromUtf8("statusbar"))
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self,MainWindow):
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
        self.updateALL_Button.setText(_translate("MainWindow", "updateALL", None))

1 个答案:

答案 0 :(得分:1)

即使你明确地使用Agg后端来生成你的数字,看起来Seaborn仍然使用系统上的默认后端,这很可能是Qt4Agg,交互式后端。我们希望Seaborn使用non-interactive backend来避免任何错误(有关后端的详细信息,请参阅matplotlib documentation)。为此,请告知导入中的Matplotlib使用Agg后端,然后在 Matplotlib之后导入Seaborn

您还需要将您的数字保存为png,因为Agg后端不支持jpg。除非你有一些使用jpg的具体原因,否则png通常是更好的图形格式。

最后,您可以使用内存缓冲区,而不是将图像保存到临时文件,然后再将其保存在Excel工作簿中。我还没有对它进行过测试,但如果您使用的是大型文件,它可能会更快。

以下是我编写的MWE,其中包含前面提到的要点,并且在Python3.4中我的系统没有出现任何错误:

import pandas as pd
import time
from pandas import ExcelWriter
import numpy as np
from PyQt4 import QtCore, QtGui

import matplotlib as mpl
mpl.use('Agg')
from matplotlib.backends.backend_agg import FigureCanvas
import seaborn.matrix as sm

try:  # Python 2 (not tested)
    from cStringIO import StringIO as BytesIO
except ImportError:  # Python 3
    from io import BytesIO


class MAIN_GUI(QtGui.QWidget):
    def __init__(self):
        super(MAIN_GUI, self).__init__()

        self.worker = Excelify()

        btn = QtGui.QPushButton('Run')
        disp = QtGui.QLabel()

        self.setLayout(QtGui.QGridLayout())
        self.layout().addWidget(btn, 0, 0)
        self.layout().addWidget(disp, 2, 0)
        self.layout().setRowStretch(1, 100)

        btn.clicked.connect(self.worker.start)
        self.worker.figSaved.connect(disp.setText)


class Excelify(QtCore.QThread):

    figSaved = QtCore.pyqtSignal(str)

    def run(self):
        self.figSaved.emit('Saving figure to Workbook.')    
        t1 = time.clock()

        image_data = self.heatmap()
        with ExcelWriter('final.xlsx', engine='xlsxwriter') as writer:
            wb = writer.book
            ws = wb.add_worksheet()
            ws.insert_image('C3', 'heat.png', {'image_data': image_data})
            writer.save()

        t2 = time.clock()    
        self.figSaved.emit('Done in %f sec.' % (t2-t1))

    def heatmap(self):    
        df = pd.DataFrame(np.array([[1, 22222, 33333], [2, 44444, 55555],
                                    [3, 44444, 22222], [4, 55555, 33333]]),
                          columns=['hour', 'in', 'out'])
        dfu = pd.DataFrame(df.groupby([df.out, df.hour]).size())
        dfu.reset_index(inplace=True)
        dfu.rename(columns={'0': 'Count'})
        dfu.columns = ['in', 'hour', 'Count']

        fig = mpl.figure.Figure()
        fig.set_canvas(FigureCanvas(fig))
        ax = fig.add_subplot(111)

        df_heatmap = dfu.pivot('in', 'hour', 'Count').fillna(0)
        sm.heatmap(df_heatmap, ax=ax, square=True, annot=False, mask=0)

        buf= BytesIO()
        fig.savefig(buf, format='png')

        return(buf)


if __name__ == '__main__':
    import sys

    app = QtGui.QApplication(sys.argv)
    w = MAIN_GUI()
    w.show()
    w.setFixedSize(200, 100)
    sys.exit(app.exec_())