2016-08-19 2 views
0

Python (x, y) 패키지에 QT와 함께 제공되는 MPL 위젯을 사용하고 있습니다. 새 데이터로 플롯을 새로 고치거나 다시 그리려고합니다. fig.canvas.draw() 메소드는 주 플롯만을 새로 고칩니다. 하위 그림은 내 문제가있는 곳입니다. 이전 서브 플로트 및 그와 관련된 모든 항목 (축척 축척, 주석 등)이 차트에 남아 있습니다. 새로 고침을하면 새 서브 플로트 데이터가 이전 데이터 위에 그려져 상당히 엉망이됩니다. 주 플롯과 관련된 모든 것이 올바르게 그려집니다. 나는 clf와 cla를 포함하여 내가 시도한 모든 것을 시도했다. 파이썬 (x, y)에 포함 된 QT MPL 위젯으로 서브 플로트를 새로 고치는 방법은 무엇입니까? self.mpl_widget.axes 만드는 방법이 명확하지 않다 예에서파이썬 (x, y)을 사용하여 서브 플로트를 새로 고치는 방법 QT Designer Matplotlib Widget?

def mpl_plot(self, plot_page, replot = 0): #Data stored in lists 

    if plot_page == 1:    #Plot 1st Page       
     plt = self.mplwidget.axes         
     fig = self.mplwidget.figure #Add a figure   

    if plot_page == 2:   #Plot 2nd Page 
     plt = self.mplwidget_2.axes 
     fig = self.mplwidget_2.figure #Add a figure 

    if plot_page == 3:   #Plot 3rd Page 
     plt = self.mplwidget_3.axes 
     fig = self.mplwidget_3.figure #Add a figure  

    if replot == 1: 

     #self.mplwidget_2.figure.clear()   

     print replot 

    par1 = fig.add_subplot(111) 
    par2 = fig.add_subplot(111)  


    #Add Axes 
    ax1 = par1.twinx()   
    ax2 = par2.twinx() 



    impeller = str(self.comboBox_impellers.currentText()) #Get Impeller 
    fac_curves = self.mpl_factory_specs(impeller)  
    fac_lift = fac_curves[0]   
    fac_power = fac_curves[1] 
    fac_flow = fac_curves[2] 
    fac_eff = fac_curves[3]   
    fac_max_eff = fac_curves[4] 
    fac_max_eff_bpd = fac_curves[5] 
    fac_ranges = self.mpl_factory_ranges() 
    min_range = fac_ranges[0] 
    max_range = fac_ranges[1] 
    #bep = fac_ranges[2] 
    #Plot Chart 
    plt.hold(False) #Has to be included for multiple curves 
    #Plot Factory Pressure 
    plt.plot(fac_flow, fac_lift, 'b', linestyle = "dashed", linewidth = 1) 



    #Plot Factory Power 
    ax1.plot(fac_flow, fac_power, 'r', linestyle = "dashed", linewidth = 1)  
    ax2.plot(fac_flow, fac_eff, 'g', linestyle = "dashed", linewidth = 1) 


    #Move spines 
    ax2.spines["right"].set_position(("outward", 25)) 
    self.make_patch_spines_invisible(ax2) 
    ax2.spines["right"].set_visible(True) 
    #Plot x axis minor tick marks 
    minorLocatorx = AutoMinorLocator()   
    ax1.xaxis.set_minor_locator(minorLocatorx) 
    ax1.tick_params(which='both', width= 0.5) 
    ax1.tick_params(which='major', length=7) 
    ax1.tick_params(which='minor', length=4, color='k') 

    #Plot y axis minor tick marks 
    minorLocatory = AutoMinorLocator() 
    plt.yaxis.set_minor_locator(minorLocatory) 
    plt.tick_params(which='both', width= 0.5) 
    plt.tick_params(which='major', length=7) 
    plt.tick_params(which='minor', length=4, color='k') 
    #Make Border of Chart White 


    #Plot Grid   
    plt.grid(b=True, which='both', color='k', linestyle='-') 

    #set shaded Area 
    plt.axvspan(min_range, max_range, facecolor='#9BE2FA', alpha=0.5) #Yellow rectangular shaded area 

    #Set Vertical Lines 
    plt.axvline(fac_max_eff_bpd, color = '#69767A') 

    #BEP MARKER *** Can change marker style if needed 
    bep = fac_max_eff * 0.90  #bep is 90% of maximum efficiency point 

    bep_corrected = bep * 0.90 # We knock off another 10% to place the arrow correctly on chart 

    ax2.annotate('BEP', xy=(fac_max_eff_bpd, bep_corrected), xycoords='data', #Subtract 2.5 shows up correctly on chart 
      xytext=(-50, 30), textcoords='offset points', 
      bbox=dict(boxstyle="round", fc="0.8"), 
      arrowprops=dict(arrowstyle="-|>", 
          shrinkA=0, shrinkB=10, 
          connectionstyle="angle,angleA=0,angleB=90,rad=10"), 
        ) 
    #Set Scales   
    plt.set_ylim(0,max(fac_lift) + (max(fac_lift) * 0.40)) #Pressure 
    #plt.set_xlim(0,max(fac_flow)) 

    ax1.set_ylim(0,max(fac_power) + (max(fac_power) * 0.40))  #Power 
    ax2.set_ylim(0,max(fac_eff) + (max(fac_eff) * 0.40)) #Effiency 


    # Set Axes Colors 
    plt.tick_params(axis='y', colors='b') 
    ax1.tick_params(axis='y', colors='r') 
    ax2.tick_params(axis='y', colors='g') 

    # Set Chart Labels   
    plt.set_xlabel("BPD") 
    plt.set_ylabel("Feet" , color = 'b') 

    #To redraw plot 


    fig.canvas.draw() 

def mpl_plot(self, plot_page, replot = 0): #Data stored in lists 

    if plot_page == 1:    #Plot 1st Page       
     plt = self.mplwidget.axes         
     fig = self.mplwidget.figure #Add a figure   

    if plot_page == 2:   #Plot 2nd Page 
     plt = self.mplwidget_2.axes 
     fig = self.mplwidget_2.figure #Add a figure 

    if plot_page == 3:   #Plot 3rd Page 
     plt = self.mplwidget_3.axes 
     fig = self.mplwidget_3.figure #Add a figure  

    if replot == 1: 

     #self.mplwidget_2.figure.clear()   

     print replot 

    par1 = fig.add_subplot(111) 
    par2 = fig.add_subplot(111)  


    #Add Axes 
    ax1 = par1.twinx()   
    ax2 = par2.twinx() 



    impeller = str(self.comboBox_impellers.currentText()) #Get Impeller 
    fac_curves = self.mpl_factory_specs(impeller)  
    fac_lift = fac_curves[0]   
    fac_power = fac_curves[1] 
    fac_flow = fac_curves[2] 
    fac_eff = fac_curves[3]   
    fac_max_eff = fac_curves[4] 
    fac_max_eff_bpd = fac_curves[5] 
    fac_ranges = self.mpl_factory_ranges() 
    min_range = fac_ranges[0] 
    max_range = fac_ranges[1] 
    #bep = fac_ranges[2] 
    #Plot Chart 
    plt.hold(False) #Has to be included for multiple curves 
    #Plot Factory Pressure 
    plt.plot(fac_flow, fac_lift, 'b', linestyle = "dashed", linewidth = 1) 



    #Plot Factory Power 
    ax1.plot(fac_flow, fac_power, 'r', linestyle = "dashed", linewidth = 1)  
    ax2.plot(fac_flow, fac_eff, 'g', linestyle = "dashed", linewidth = 1) 


    #Move spines 
    ax2.spines["right"].set_position(("outward", 25)) 
    self.make_patch_spines_invisible(ax2) 
    ax2.spines["right"].set_visible(True) 
    #Plot x axis minor tick marks 
    minorLocatorx = AutoMinorLocator()   
    ax1.xaxis.set_minor_locator(minorLocatorx) 
    ax1.tick_params(which='both', width= 0.5) 
    ax1.tick_params(which='major', length=7) 
    ax1.tick_params(which='minor', length=4, color='k') 

    #Plot y axis minor tick marks 
    minorLocatory = AutoMinorLocator() 
    plt.yaxis.set_minor_locator(minorLocatory) 
    plt.tick_params(which='both', width= 0.5) 
    plt.tick_params(which='major', length=7) 
    plt.tick_params(which='minor', length=4, color='k') 
    #Make Border of Chart White 


    #Plot Grid   
    plt.grid(b=True, which='both', color='k', linestyle='-') 

    #set shaded Area 
    plt.axvspan(min_range, max_range, facecolor='#9BE2FA', alpha=0.5) #Yellow rectangular shaded area 

    #Set Vertical Lines 
    plt.axvline(fac_max_eff_bpd, color = '#69767A') 

    #BEP MARKER *** Can change marker style if needed 
    bep = fac_max_eff * 0.90  #bep is 90% of maximum efficiency point 

    bep_corrected = bep * 0.90 # We knock off another 10% to place the arrow correctly on chart 

    ax2.annotate('BEP', xy=(fac_max_eff_bpd, bep_corrected), xycoords='data', #Subtract 2.5 shows up correctly on chart 
      xytext=(-50, 30), textcoords='offset points', 
      bbox=dict(boxstyle="round", fc="0.8"), 
      arrowprops=dict(arrowstyle="-|>", 
          shrinkA=0, shrinkB=10, 
          connectionstyle="angle,angleA=0,angleB=90,rad=10"), 
        ) 
    #Set Scales   
    plt.set_ylim(0,max(fac_lift) + (max(fac_lift) * 0.40)) #Pressure 
    #plt.set_xlim(0,max(fac_flow)) 

    ax1.set_ylim(0,max(fac_power) + (max(fac_power) * 0.40))  #Power 
    ax2.set_ylim(0,max(fac_eff) + (max(fac_eff) * 0.40)) #Effiency 


    # Set Axes Colors 
    plt.tick_params(axis='y', colors='b') 
    ax1.tick_params(axis='y', colors='r') 
    ax2.tick_params(axis='y', colors='g') 

    # Set Chart Labels   
    plt.set_xlabel("BPD") 
    plt.set_ylabel("Feet" , color = 'b') 

    #To redraw plot 


    fig.canvas.draw() 

답변

1

:

여기 내 코드입니다. 사용되지 않고 아래에 설명 된 것처럼 축 인스턴스의 이전 참조로 그리면 문제가 발생할 수 있습니다.

mpl_widget.axes이 전혀 사용되지 않았기 때문에 축에 대한 참조를 보유하지 않을 것을 제안합니다. 예에서 twinx의 사용이 올바르지 않습니다. 다음은 일할 수 :

if plot_page == 1:    #Plot 1st Page 
    widget = self.mplwidget 
if plot_page == 2:   #Plot 2nd Page 
    widget = self.mplwidget_3 
if plot_page == 3:   #Plot 3rd Page 
    widget = self.mplwidget_3 
if replot == 1: 
    widget.figure.clear() 
ax1 = widget.figure.add_subplot(111) 
ax2 = ax1.twinx() 

... 

widget.figure.canvas.draw() 

또 다른 문제는 self.mpl_widgetXX.axesplt에 할당하고 더 아래의 예 plt에 새로운 데이터를 그리는 데 사용된다는 점이다. self.mpl_widgetXX.axes은 새로 생성 된 축 인스턴스 중 하나를 포함하도록 업데이트되지 않으므로 예제 코드는 이전 축을 사용하여 잠재적으로 질문에 설명 된 효과를 이끌어 낼 수 있습니다. 그림 인스턴스에 액세스하려면 그림 및 눈금 설정에는 ax1ax2을 사용해야하고 widget.figure 만 사용해야합니다.

+0

신의 축복. 그것은 효과가 있었다. –

+0

plt 축을 서브 플로트로 이동하면 수직 그리드 선이 사라지고 mpl이 원하지 않는 축을 표시합니다. 이러한 문제 중 하나에 대한 해결책을 알고 있습니까? –