首页 > 解决方案 > Plot.show 函数创建不需要的多个数字?

问题描述

我有一个通过按钮调用绘图功能的 UI。令人讨厌的是,它创建了多个图形,而不仅仅是一个图形。以下是我的绘图功能。

def PlotFunction(self, xdata, xlabel, y1data, y1specs, y1label, y2data, y2label, Y2Axis):

    matplotlib.rcParams.update({'font.size': 18})
    plt.clf()   # Clear the Entire Current Figure
    fig = plt.figure()
    fig.set_size_inches(9, 6.75, forward=True) # 4:3

    # Sub-Plot 1
    ax1 = fig.add_subplot(111)

    # Axes 1 - Y1Data
    Lines = []
    for i in range(len(y1data)):
        temp = ax1.plot(xdata, y1data[i], y1specs[i], linewidth=3, label=y1label[i])
        Lines += temp
    ax1.set_xlabel(xlabel)
    # ax1.set_ylabel(y1label)
    ax1.set_xlim(0,None)

    # Axes 2 - Flight Mode
    if Y2Axis == True:

        # Axes 2
        ax2 = ax1.twinx()
        # ax2.set_ylabel(y2label)
        line4 = ax2.plot(xdata, y2data, label=y2label, 
                        color='#fb7d07',
                        linestyle='dotted',
                        marker='o',
                        markeredgecolor='#fb7d07',
                        markerfacecolor='#fb7d07',
                        markersize=4) # Color: Pumpkin Orange

        if y2data == self.FlightPhases: # self.CSV['ucus_modu']:
            FlightMode = ['YERDE', 'KALKIS', 'AYRILMA', 'SEYIR', 'KONUM', 'DONUS', 'INIS','GNSS_YOK']
            ax2.set_yticklabels(FlightMode)
            ax2.set_yticks([2,3,4,5,6,7,8,9])
            ax2.set_ylim(2,9)
            ax2.set_xlim(0,None)

        Lines += line4

        # Show Plot
        ax2.grid(b=True, which='major', color='k', linestyle='-')
        ax2.grid(b=True, which='minor', color='k', linestyle=':', alpha=0.5) # alpha [0-1]
        ax2.minorticks_on()

    else:

        # Show Plot
        ax1.grid(b=True, which='major', color='k', linestyle='-')
        ax1.grid(b=True, which='minor', color='k', linestyle=':', alpha=0.5) # alpha [0-1]
        ax1.minorticks_on()

    # Set Legends
    labels = [l.get_label() for l in Lines]
    ax1.legend(Lines, labels, ncol = len(labels), loc='upper center', bbox_to_anchor=(0.5, 1.15))

    print("A", plt.get_fignums())
    plt.show()
    print("B", plt.get_fignums())

我只需单击一次,但打印返回:

Python 3.6.6 |Anaconda, Inc.| (default, Jun 28 2018, 11:27:44) on Windows (64 bits).
This is the Pyzo interpreter with integrated event loop for PYQT5.

Using IPython 7.2.0 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: (executing file "A00_AHIT_Main_LOGData.py")
Note on using QApplication.exec_(): 
The GUI event loop is already running in the pyzo kernel, and exec_()
does not block. 

A [1, 2]
Note on using QApplication.exec_(): 
The GUI event loop is already running in the pyzo kernel, and exec_()
does not block. 

B [1, 2]
A [1, 2, 3]
Note on using QApplication.exec_(): 
The GUI event loop is already running in the pyzo kernel, and exec_()
does not block. 

B [1, 2, 3]

这怎么可能?似乎这个函数被调用了两次或更多,但没有。IPyhton 不知何故复制了这些图。我在https://github.com/statsmodels/statsmodels/issues/1265中发现了类似的问题。我不明白确切的原因。

标签: python-3.xmatplotlibpyqt5

解决方案


推荐阅读