0

This is a follow up question to the one asked before. I have a code below:

from tkinter import *

from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
from matplotlib.figure import Figure

class PlotClass():
    def __init__(self):
         fig = Figure(figsize=(5,5),dpi=70,facecolor='cyan')
         ax = fig.subplots()
         ax.set_title("Ttile")
         ax.set_ylabel("y")
         ax.set_xlabel("x")
         ax.set_xlim(100,9000)
         ax.set_ylim(130,-10)
         ax.set_facecolor("cyan")

         x = [125,250,500,1000,2000,4000,8000]
         ticks = [125,250,500,"1K","2K","4K","8K"]
         xm = [750,1500,3000,6000]

         ax.set_xscale('log', basex=2)
         ax.set_xticks(x)
         ax.set_xticks(xm, minor=True)
         ax.set_xticklabels(ticks)
         ax.set_xticklabels([""]*len(xm), minor=True)

         ax.yaxis.set_ticks([120,110,100,90,80,70,60,50,40,30,20,10,0,-10])

         self.line, = ax.plot([],[],'r+',markersize=15.0,mew=2)
         self.line2,= ax.plot([],[],'-o',markersize=15.0,mew=2)
         ax.grid(color="grey")
         ax.grid(axis="x", which='minor',color="grey", linestyle="--")
         self.canvas = canvas = FigureCanvasTkAgg(fig, master=master)
         canvas.show()
         canvas.get_tk_widget().grid(column=0,row=2,columnspan=3,rowspan=15)
         self.spin = Spinbox(master, from_=125,to=8000,command=self.action)
         self.spin.grid(column=5,row=2)

         self.spin2 = Spinbox(master, from_=-10,to=125,command=self.action)
         self.spin2.grid(column=5,row=3)

         self.button = Button(master, text="plot here",command=self.plot)
         self.button.grid(column=5,row=4)
    def ok(self, x=1000,y=20):
        self.line.set_data([x],[y])
        self.canvas.draw_idle()

    def action(self):
        self.ok(float(self.spin.get()),float(self.spin2.get()))

    def linecreate(self, x=1000,y=20):
        self.line2.set_data([x],[y])
        self.canvas.draw_idle()

    def plot(self):
        self.linecreate(float(self.spin.get()),float(self.spin2.get()))

master = Tk()
plotter = PlotClass()
plotter.ok(125,10)
master.mainloop()

It plots graph by feeding the x and y axis with values from two tkinter spinbox, as the value in spinbox changes the plot redraws and changes the markers location based on the values provided by the spinboxes. Now, I have added a tkinter button and linked it to a function that would draw another plot using the same values coming from the spinboxes but now I could not understand how to draw the new plot without removing the previous plots when the button is pressed. What I mean is, plotting on new locations (adding more markers) on the graph without removing the previous plot locations (previous markers) that has been added by the button press. Like when button clicks it keeps adding the new markers based on the spinbox values without removing the previous ones. In the previous question it was removing the existing plots and adding new ones every time the spinbox value changes but now I could not understand how to plot on new location without removing the previous plot.

1 Answer 1

0

You may append you new point to the data of the existing line.

def linecreate(self, x=1000,y=20):
    X,Y = self.line2.get_data()
    X = np.append(X,[x])
    Y = np.append(Y,[y])
    self.line2.set_data(X,Y)
    self.canvas.draw_idle()
Sign up to request clarification or add additional context in comments.

7 Comments

Actually I forgot to add something in the question. facepalm If consider this: x = [125] and y = [50], the question was suppose to be if initially the x and y was x[125] and y[50] then whenever the value changes to x[125] and y[90] then instead of plotting it again is it possible to change the value of just y only if the x is same.
Well then find out if that value is already being in the array, if so, replace the y value, if not append it.
I did it but seems like it is also changing the plot for those that are not same as x. Could you please provide me with a sample code? Very much appreciated. :)
What exactly did you try? (How can I know what's wrong without knowing that?)
if x in X: to detect if value exists but cannot implement, I mean replace the y value corresponding to that x, could you help me out.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.