[Python] Clearing a subplot to update with a new one

Discussão em 'Python' iniciado por Stack, Janeiro 24, 2025.

  1. Stack

    Stack Membro Participativo

    I have some radiobuttons to select colour and width of a linestring in an embedded plot. I can't get the plot to clear when I select a new colour or width and the new plot is placed below the original plot. I have probably complicated things by using tkinter, matplotlib and shapely. I have been successful in destroying the entire window and rebuilidng it but it seemed pretty clunky. How can I remove the old plot or figure so I can update the colour and width?

    I tried plt.clf() in the Selection method and it didn't remove it at all. I also tried putting plt.clf() at the end of the initial plot. This removed the plot but the new plot was still placed below the blank figure.

    from tkinter import *
    import tkinter as tk
    import matplotlib.pyplot as plt
    from shapely.geometry import LineString
    from shapely.plotting import plot_line, plot_points
    from matplotlib.figure import Figure
    from matplotlib.backends.backend_tkagg import (FigureCanvasTkAgg,NavigationToolbar2Tk)


    root =tk.Tk()
    root.title("DATA")
    topFrame=tk.Frame(root)
    bottomFrame=tk.Frame(root)
    topFrame.pack(side='top')
    bottomFrame.pack(side='bottom')


    #Method and call to create initial plot
    def CreatePlot(lineColour,lineWidth):
    width = lineWidth
    colour = "#"+ lineColour
    peekFigure = Figure(figsize=(6, 4), dpi=100)
    fig = plt.figure (dpi = 90,clear=True)
    print("fig num: ", peekFigure)
    ax = fig.add_subplot (111)
    sample = LineString([(1.0,1.0), (2.0,2.0)])
    plot_line(sample, ax=ax, add_points=False, alpha=0.7, color = colour,linewidth = lineWidth)
    axes = plt.gca()
    axes.set_aspect('equal')
    plt.axis('off')
    canvas = FigureCanvasTkAgg(fig, master = root)
    canvas.draw()
    canvas.get_tk_widget().pack()

    width = 5
    colour = 'ff0000'
    CreatePlot(colour,width)
    #plt.clf()

    #Method for change in radiobutton selection
    def Selection():
    plt.clf()
    CreatePlot(cVar.get(),wVar.get())


    #Create buttons and labels
    buttonExit =Button (bottomFrame, text = "Exit",command = root.destroy,width = 20)
    buttonExit.pack()
    labelColour = tk.Label(topFrame, text=" Colour")
    labelColour.grid(row=0, column=1, sticky = 'w',padx=3, pady=2)
    label3 = tk.Label(topFrame, text="Line Width")
    label3.grid(row = 0, column = 2,padx=3, pady=2)


    #Create radio buttons for line colour
    colours = [{"colour":'red',"value":'ff0000'},{"colour":'blue',"value":"0000ff"},\
    {"colour": 'amber',"value":'ffa500'},{"colour":'magenta',"value":'ff00ff'}]
    cVar = tk.StringVar()
    rowNo=0
    radC={}
    for c in colours:
    columnNo = 1
    rowNo=rowNo+1
    radC[f'{rowNo}']= tk.Radiobutton(topFrame,text=(c["colour"]),value=(c["value"]),\
    variable=cVar,command = Selection)
    radC[f'{rowNo}'].grid(row = rowNo, column = columnNo,sticky='w',padx=3, pady=2)
    if rowNo==1:
    radC[f'{rowNo}'].select()



    # Create radio buttons for line widths
    widths = ["5", "10", "20","30"]
    wVar = tk.IntVar()
    rowNo=0
    WBoxList = []
    radW={}
    for width in widths:
    columnNo = 2
    rowNo=rowNo+1
    radW[f'{rowNo}']= tk.Radiobutton(topFrame,text=(width),value=(width),\
    variable=wVar,command = Selection)
    radW[f'{rowNo}'].grid(row = rowNo, column = columnNo,sticky='w',padx=3, pady=2)
    if rowNo == 1:
    radW[f'{rowNo}'].select()

    root.mainloop()

    Continue reading...

Compartilhe esta Página