0

The following code draws thw graph everytime the button is clicked. I want the plot to get updated instead of overriting

    def plot_cr():
        section_length = 220
        section_width = 220
        angle = np.linspace(np.pi, 3 * np.pi / 2, 150)
        side_view_width = float(section_width) / 100
        outer_radius = float(12) + side_view_width
        x = float(12) * np.cos(angle)
        y = float(12) * np.sin(angle)
        fig1 = Figure(figsize=(10, 10), dpi=60)
        axes = fig1.add_subplot(111)
        axes.clear()
        axes.plot(x, y, color="green")
        axes.set_aspect(1)
        axes.set_title('Caster Diagram')
        canvas_side = FigureCanvasTkAgg(fig1, master=caster_graph)
        canvas_side.draw()
        canvas_side.get_tk_widget().pack(side="left", fill="both", expand=True)
        axes.get_xaxis().set_visible(False)
        axes.get_yaxis().set_visible(False)
        plt.rcParams['axes.formatter.useoffset'] = False
        fig1.tight_layout()
    plot_button = Button(master=cnozzle, command=plot_cr, height=1, width=20, text="Get Cooling Profile")
    plot_button.grid(row=2, column=0, padx=25, columnspan=2, pady=5)

I have tried axes.clear() command but it does not do the job . Kindly suggest how to fix that.

  • What in the plot is to be updated when the button is clicked? At the moment, everything within `plot_cr()` appears to be fixed, so updating the plot will not change anything. – Matt Pitkin Feb 22 '23 at 11:16
  • The idea is to prevent plotting the same graph every time the button is clicked. I have used pack method So it just keep adding the graph t the window. – Prashant Priyadarshi Feb 23 '23 at 12:08

2 Answers2

0

Based on your comment, one option is to put the function inside a class that records if the plot has been created and has a class method that only plots it if no plot already exists. For example:

class Plotter:
    def __init__(self):
        self._plot_exists = False  # no plot

    def plot_cr(self):
        if not self._plot_exists:
            # create plot if no plot exists
            section_length = 220
            section_width = 220
            angle = np.linspace(np.pi, 3 * np.pi / 2, 150)
            side_view_width = float(section_width) / 100
            outer_radius = float(12) + side_view_width
            x = float(12) * np.cos(angle)
            y = float(12) * np.sin(angle)
            fig1 = Figure(figsize=(10, 10), dpi=60)
            axes = fig1.add_subplot(111)
            axes.clear()
            axes.plot(x, y, color="green")
            axes.set_aspect(1)
            axes.set_title('Caster Diagram')
            canvas_side = FigureCanvasTkAgg(fig1, master=caster_graph)
            canvas_side.draw()
            canvas_side.get_tk_widget().pack(side="left", fill="both", expand=True)
            axes.get_xaxis().set_visible(False)
            axes.get_yaxis().set_visible(False)
            plt.rcParams['axes.formatter.useoffset'] = False
            fig1.tight_layout()

            # set _plot_exists to True
            self._plot_exists = True

plotter = Plotter()  # create instance of class
plot_button = Button(
    master=cnozzle,
    command=plotter.plot_cr,  # pass the class function
    height=1,
    width=20,
    text="Get Cooling Profile"
)
plot_button.grid(row=2, column=0, padx=25, columnspan=2, pady=5)
Matt Pitkin
  • 3,989
  • 1
  • 18
  • 32
  • It is working well but I want the plot to be updated . You have created for the situation if there is no plot then draw and if there is a plot it will not. I want to update the graph with new coordinates which will becoming from treeview data. Meaning is that I will keep the plot function inside the another function. Thank you so much fopr your support – Prashant Priyadarshi Feb 27 '23 at 04:08
  • i have tried all the comands but nothing works.`plt.clf()` axes.clear() axes1.clear() fig2.clear() fig1.clear() print('hello')` nothing works except printing hello, it signifies the code is running well but the plot is not gettiong cleared. – Prashant Priyadarshi Feb 27 '23 at 05:50
  • Ok, you could instead have all the stuff that creates the `Figure` object in the `__init__` method of the class, and then only have the `axes.plot` function in the `plot_cr` method. Although, I see you've found a simple solution yourself. – Matt Pitkin Feb 27 '23 at 11:12
0

I have finally solved using basic rearrangement and now it solves the purpose. I just took axes and figures out of the function and added the command axes.cla()

    fig1 = Figure(figsize=(10, 10), dpi=60)
    axes = fig1.add_subplot(111)
    def plot_cr():
        axse.cla()
        section_length = 220
        section_width = 220
        angle = np.linspace(np.pi, 3 * np.pi / 2, 150)
        side_view_width = float(section_width) / 100
        outer_radius = float(12) + side_view_width
        x = float(12) * np.cos(angle)
        y = float(12) * np.sin(angle)
        axes.plot(x, y, color="green")
        axes.set_aspect(1)
        axes.set_title('Caster Diagram')
        canvas_side = FigureCanvasTkAgg(fig1, master=caster_graph)
        canvas_side.draw()
        canvas_side.get_tk_widget().pack(side="left", fill="both", expand=True)
        axes.get_xaxis().set_visible(False)
        axes.get_yaxis().set_visible(False)
        plt.rcParams['axes.formatter.useoffset'] = False
        fig1.tight_layout()
    plot_button = Button(master=cnozzle, command=plot_cr, height=1, width=20, text="Get Cooling Profile")
    plot_button.grid(row=2, column=0, padx=25, columnspan=2, pady=5)