PyQt5 plt.show()非阻塞函数

时间:2019-02-06 13:31:14

标签: python python-3.x matplotlib pyqt5

当我必须使用plt.show()函数将结果显示为直方图时,我遇到了问题,因为我需要主体函数的返回值才能进行新的计算,所以我需要该函数是非阻塞的并显示一个新的直方图。 (PS。我正在使用PyCharm ide和Python 3.6.7rc2 matplotlib 3.0.0)

这是代码中的功能:

def plotHistStd(data_file, attribute, number, method):
original = copy.deepcopy(data_file)
modified = copy.deepcopy(data_file)
tmp_arr = []
tmp_arr2 = []
if method == 1: 
    for i in range(0, original.__len__()):
        if original[i] > 0:
            if number > 0:
                tmp = log1p(original[i]+number) 
                tmp_arr.append(copy.deepcopy(original[i])) 
                tmp_arr2.append(copy.deepcopy(tmp.real)) 
            else:
                tmp = np.log1p(original[i]+number)
                tmp_arr.append(copy.deepcopy(original[i]))
                tmp_arr2.append(copy.deepcopy(tmp.real))
        else:
            tmp = np.log1p(original[i]+number)
            tmp_arr.append(copy.deepcopy(original[i]))
            tmp_arr2.append(copy.deepcopy(tmp.real))
        modified[i] = tmp.real 
elif method == 2:
    for i in range(0, original.__len__()):
        if original[i] > 0:
            tmp2 = pow((original[i]), (1.0/number))
            tmp_arr.append(copy.deepcopy(original[i]))
            tmp_arr2.append(copy.deepcopy(tmp2.real))
        else:
            tmp2 = cm.exp(number*cm.log(original[i]))
            tmp_arr.append(copy.deepcopy(original[i]))
            tmp_arr2.append(copy.deepcopy(tmp2.real))
        modified[i] = tmp2.real 
v_max = max(tmp_arr)
v_min = min(tmp_arr)
v_max2 = max(tmp_arr2)
v_min2 = min(tmp_arr2)
v_max = max(v_max, v_max2)
v_min = min(v_min, v_min2)
fig = plt.figure() 
ax1 = fig.add_subplot(2, 1, 1)
ax2 = fig.add_subplot(2, 1, 2)
n, bins, patches = ax1.hist(original, color="blue", bins=15, range=(v_min, v_max)) 
ax1.set_xlabel(attribute) 
ax1.set_ylabel('Frequency')  
n, bins, patches = ax2.hist(modified, color="red", bins=15, range=(v_min, v_max))
ax2.set_xlabel(attribute) 
ax2.set_ylabel('Frequency') 
plt.subplots_adjust(hspace=0.3) 
plt.xticks(rotation=90) 
plt.show() #need these non-blocking version
return modified 

该函数是从主窗口调用的,该窗口是在另一个Python类文件中开发的。 我试图在plt.show()之后插入plt.ion(),但是它不起作用。

P.s。在调用的主窗口中添加优先指示的函数:

    def apply_stdandardization(self):
    if self.file[self.file.__len__() - 4:self.file.__len__()] != ".csv":
        QtWidgets.QMessageBox.warning(QtWidgets.QWidget(),"Alert Message", "No CSV file was selected!")
    else:
        number = self.lineEdit_2.text() 
        index = self.comboBox_2.currentText() 
        std_method = 0 
        #data_file = pd.read_csv(self.file) 
        number = float(number)
        try:
            number = float(number)
        except Exception:
            QtWidgets.QMessageBox.warning(QtWidgets.QWidget(), "Alert Message",
                                          "Insert only numeric value")
        if self.rad_log.isChecked():
            std_method = 1
        elif self.rad_exp.isChecked():
            std_method = 2
        self.new_df = std_function(str(index), number, std_method)

编辑: 我试图在plt.show()之前插入plt.ion(),实际上该程序没有被阻止,但是直方图窗口无法正确打开,如果单击它,程序将崩溃并出现以下错误:

Fatal Python error: PyEval_RestoreThread: NULL tstate

Thread 0x000016c0 (most recent call first):
 File "C:\Python36\lib\threading.py", line 299 in wait
 File "C:\Python36\lib\threading.py", line 551 in wait
 File "C:\PyCharm\helpers\pydev\pydevd.py", line 128 in _on_run
 File "C:\PyCharm\helpers\pydev\_pydevd_bundle\pydevd_comm.py", line 320 in run
 File "C:\Python36\lib\threading.py", line 916 in _bootstrap_inner
 File "C:\Python36\lib\threading.py", line 884 in _bootstrap

 Thread 0x00000fb8 (most recent call first):
 File "C:\PyCharm\helpers\pydev\_pydevd_bundle\pydevd_comm.py", line 382 in _on_run
 File "C:\PyCharm\helpers\pydev\_pydevd_bundle\pydevd_comm.py", line 320 in run
 File "C:\Python36\lib\threading.py", line 916 in _bootstrap_inner
 File "C:\Python36\lib\threading.py", line 884 in _bootstrap

Thread 0x00000b28 (most recent call first):
 File "C:\Python36\lib\threading.py", line 299 in wait
 File "C:\Python36\lib\queue.py", line 173 in get
 File "C:\PyCharm\helpers\pydev\_pydevd_bundle\pydevd_comm.py", line 459 in _on_run
 File "C:\PyCharm\helpers\pydev\_pydevd_bundle\pydevd_comm.py", line 320 in run
 File "C:\Python36\lib\threading.py", line 916 in _bootstrap_inner
 File "C:\Python36\lib\threading.py", line 884 in _bootstrap

Current thread 0x00000b08 (most recent call first):
File "C:/Users/Enrico/PycharmProjects/PythonDataset/PDA.py", line 681 in <module>
File "C:\PyCharm\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18 in execfile
File "C:\PyCharm\helpers\pydev\pydevd.py", line 1135 in run
File "C:\PyCharm\helpers\pydev\pydevd.py", line 1735 in main
File "C:\PyCharm\helpers\pydev\pydevd.py", line 1741 in <module>

进程结束,退出代码为255

0 个答案:

没有答案