Matplotlib Linien löschen

mit matplotlib, NumPy, pandas, SciPy, SymPy und weiteren mathematischen Programmbibliotheken.
Antworten
stefanxfg
User
Beiträge: 85
Registriert: Sonntag 2. April 2017, 14:11

Hallo, mein Probelm ist das Löschen von Linien in Matplotlib. EIgentlich funktioniert es schon, aber ich muss den Löschen-Button mehrmals betätigen, was ja nicht im Sinne des Erfinders ist. Ich binde hier nur die Ausgabe in der Matplotlib-Klasse ein, welche ich getrennt von der GUI habe. Der Code ist natürlich noch im Entwicklungsstadium, so das da nich einiges Leer oder auskommentiert steht.

Code: Alles auswählen

class PlotYZ_Output:

    def __init__(self, container):
        self.container = container 
        
        self.fig = Figure (figsize=(5,4), dpi=100)
        self.canvas = FigureCanvasTkAgg(self.fig,self.container) 
        self.ax = self.fig.add_subplot(111)
        self.ax.set_title('Fahrzeugbegrenzungslinien')
        self.ax.set_xlabel('Fahrzeugbreite y [mm]')
        self.ax.set_ylabel('Fahrzeughöhe z [mm]')
        self.ax.axis([-2000,2000,0, 5000])
        self.ax.grid(True)
        
        self.toolbar = NavigationToolbar2TkAgg(self.canvas,self.container)
        self.toolbar.update()
        self.plot_widget = self.canvas.get_tk_widget()
        self.plot_widget.pack(side=tk.TOP, fill=tk.BOTH, expand=1)
        self.toolbar.pack(side=tk.BOTTOM, fill=tk.BOTH, expand=1)
    
        self.lhor = self.ax.axhline(0.5)
        self.lver = self.ax.axvline(1)
        
        
        
        self.canvas.mpl_connect('motion_notify_event', self.onMouseMove)
        self.canvas.draw()

        subscribe('CONNECTION_TO_MATPLOT',self.receive_linestyles)
        
        subscribe('DEL_SINGLELLINE',self.remove_singleline)
        subscribe('DEL_ALLLINES', self.remove_all)
        #subscribe('SEND_ALL_LINES',self.redraw)
        
    def onMouseMove(self,event):
        
        self.lhor.set_ydata(event.ydata)
        self.lver.set_xdata(event.xdata)
        self.canvas.draw()
       


    def receive_linestyles(self,styles):
                
        self.linestyles = {}        
        self.linestyles = styles
        self.figline_create()
        
    
    def receive_draw_data(self):
        print('DATA')

    def redraw(self,dict):

        self.drawlines = {}
        self.drawlines = dict
        #print(self.drawlines)
        default_linecolour = "black"
        default_markercolour = "black"
        default_linestyle = "solid"
        default_markerstyle = " "
        
        ya = np.array([0,500,1000]) #später löschen
        za = np.array([100,100,100]) #später löschen
        yb = np.array([0,0,0]) #später löschen
        
        self.plot_Handling = []
        zahl_plot = 0
        for key,linedict in self.drawlines.items(): # nichts verwendet als die Loop-Anzahl -> Erzeugung Linien nach Anzahl Dictionary        
            line1, = self.ax.plot([],[],color=default_linecolour,linestyle=default_linestyle,markerfacecolor=default_markercolour,marker=default_markerstyle)
            self.plot_Handling.append(line1)
            self.plot_Handling[zahl_plot].set_xdata(ya)
            self.plot_Handling[zahl_plot].set_ydata(za)
            zahl_plot += 1
          
          
    def figline_create(self):

        default_linecolour = "black"
        default_markercolour = "black"
        default_linestyle = "solid"
        default_markerstyle = " "
        
        self.plot_Handling = []
        
        for key,linedict in self.linestyles.items(): # nichts verwendet als die Loop-Anzahl -> Erzeugung Linien nach Anzahl Dictionary        
            line1, = self.ax.plot([],[],color=default_linecolour,linestyle=default_linestyle,markerfacecolor=default_markercolour,marker=default_markerstyle)
            self.plot_Handling.append(line1)
        
        self.figline_update()    
  

    def figline_update(self):
        print('UPDATE')

        ya = np.array([0,500,1000]) #später löschen
        za = np.array([100,100,100]) #später löschen
        yb = np.array([0,0,0]) #später löschen
        
        zahl_plot = 0
        for key,linedict in self.linestyles.items():  
            if linedict['marker'] == 'square':
                marker_translate = 's'
            if linedict['marker'] == 'circle':
                marker_translate = 'o'
            if linedict['marker'] == 'cross':
                marker_translate = 'x'
            if linedict['marker'] == 'star':
                marker_translate = '*'        
            
            self.plot_Handling[zahl_plot].set_color(linedict['line_color'])
            self.plot_Handling[zahl_plot].set_linestyle(linedict['line'])
            self.plot_Handling[zahl_plot].set_markerfacecolor(linedict['marker_color'])
            self.plot_Handling[zahl_plot].set_marker(marker_translate)
            self.plot_Handling[zahl_plot].set_xdata(ya)
            self.plot_Handling[zahl_plot].set_ydata(za)
            zahl_plot += 1

        self.canvas.draw()


        
    def remove_singleline(self,line_info):
        
        zahl_plot = 0
        for key,linedict in self.linestyles.items():
            if key == line_info:
                self.plot_Handling[zahl_plot].delete()
            zahl_plot += 1
        self.canvas.draw()
        
    def remove_all(self):

        for line in self.plot_Handling:
            self.plot_Handling.remove(line)

        #for line in self.ax.lines:
        #    self.ax.lines.remove(line)
        
        
        self.canvas.draw()
Ob ich nun wirklich die erzeugten Linien in eine Liste schreiben muss, bleibt erst einmal außen vor. Wahrscheinlich nicht, da ich mit ax.lines auf die Linienliste zugreifen kann.

Ich verstehe nicht, warum das Löschen nicht die gesamte Liste auf einmal leert.
Antworten