python - Updating a graphs coordinates in matplotlib -
i have below code plot sphere, it's proportions defined prop, i'd when button pressed prop's value changes 5 , graph adjusted accordingly. how go this?
i know tkinter has .configure(), allows 1 adjust widget settings. i'm looking similar can reconfigure plot.
#!/usr/bin/env python import matplotlib matplotlib.use('tkagg') mpl_toolkits.mplot3d import axes3d,axes3d import matplotlib.pyplot plt matplotlib import cm import numpy np numpy import arange, sin, pi matplotlib.backends.backend_tkagg import figurecanvastkagg, navigationtoolbar2tkagg matplotlib.figure import figure matplotlib.ticker import linearlocator, fixedlocator, formatstrformatter import tkinter import sys class e(tkinter.tk): def __init__(self,parent): tkinter.tk.__init__(self,parent) self.parent = parent self.protocol("wm_delete_window", self.dest) self.main() def main(self): self.fig = plt.figure() self.fig = plt.figure(figsize=(3.5,3.5)) ax = axes3d(self.fig) prop = 10 u = np.linspace(0, 2 * np.pi, 100) v = np.linspace(0, np.pi, 100) x = prop * np.outer(np.cos(u), np.sin(v)) y = prop * np.outer(np.sin(u), np.sin(v)) z = prop * np.outer(np.ones(np.size(u)), np.cos(v)) t = ax.plot_surface(x, y, z, rstride=4, cstride=4,color='lightgreen',linewidth=0) self.frame = tkinter.frame(self) self.frame.pack(padx=15,pady=15) self.canvas = figurecanvastkagg(self.fig, master=self.frame) self.canvas.get_tk_widget().pack(side='top', fill='both') self.canvas._tkcanvas.pack(side='top', fill='both', expand=1) self.toolbar = navigationtoolbar2tkagg( self.canvas, self ) self.toolbar.update() self.toolbar.pack() self.btn = tkinter.button(self,text='button',command=self.alt) self.btn.pack(ipadx=250) def alt (self): prop = 5 def dest(self): self.destroy() sys.exit() if __name__ == "__main__": app = e(none) app.title('embedding in tk') app.mainloop()
modify main
function in class e:
def main(self): self.fig = plt.figure() self.fig = plt.figure(figsize=(3.5,3.5)) self.frame = tkinter.frame(self) self.frame.pack(padx=15,pady=15) self.canvas = figurecanvastkagg(self.fig, master=self.frame) self.canvas.get_tk_widget().pack(side='top', fill='both') self.canvas._tkcanvas.pack(side='top', fill='both', expand=1) self.toolbar = navigationtoolbar2tkagg( self.canvas, self ) self.toolbar.update() self.toolbar.pack() self.btn = tkinter.button(self,text='button',command=self.alt) self.btn.pack(ipadx=250) self.draw_sphere()
function alt
is:
def alt (self): self.draw_sphere(5)
and add new function draw_sphere
(also in class e):
def draw_sphere(self, prop=10): self.fig.clear() ax = axes3d(self.fig) u = np.linspace(0, 2 * np.pi, 100) v = np.linspace(0, np.pi, 100) x = prop * np.outer(np.cos(u), np.sin(v)) y = prop * np.outer(np.sin(u), np.sin(v)) z = prop * np.outer(np.ones(np.size(u)), np.cos(v)) t = ax.plot_surface(x, y, z, rstride=4, cstride=4,color='lightgreen',linewidth=0) self.canvas.draw()
Comments
Post a Comment