【问题标题】:Updating a graphs coordinates in matplotlib在 matplotlib 中更新图形坐标
【发布时间】:2011-04-22 02:57:35
【问题描述】:

我在下面的代码将绘制一个球体,它的比例由 prop 定义,我想要这样当按下按钮时 prop 的值更改为 5 并相应调整图表。我该怎么做?

我知道 tkinter 有 .configure(),它允许调整小部件设置。我正在寻找类似的东西,以便我可以重新配置我的情节。

#!/usr/bin/env python
import matplotlib
matplotlib.use('TkAgg')


from mpl_toolkits.mplot3d import  axes3d,Axes3D
import matplotlib.pyplot as plt
from matplotlib import cm
import numpy as np
from numpy import arange, sin, pi
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
from matplotlib.figure import Figure
from 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()

【问题讨论】:

    标签: python matplotlib tkinter


    【解决方案1】:

    修改E类中的main函数:

     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()
    

    函数alt是:

     def alt (self):
            self.draw_sphere(5)
    

    并添加新函数draw_sphere(也在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()
    

    【讨论】:

    • 它有效,但我注意到按下按钮后,轨道性能似乎急剧下降。我相信这是因为它只是在旧的球体上画了另一个球体。有什么办法可以抹掉旧球体?
    • 奇怪,我还以为原来的剧情完全抹杀了。将 fig.clear() 放在 draw_sphere 函数之上可能会有所帮助。
    • 谢谢,是的,这就是问题所在。感谢您的帮助,现在我的一个项目终于可以向前推进了。
    【解决方案2】:

    这是更新后的代码,可在 python v3.9 和 Matplotlib 3.4 中使用

    #!/usr/bin/env python
    
    import matplotlib
    matplotlib.use('TkAgg')
    
    
    from mpl_toolkits.mplot3d import  axes3d,Axes3D
    import matplotlib.pyplot as plt
    from matplotlib import cm
    import numpy as np
    from numpy import arange, sin, pi
    from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk
    from matplotlib.figure import Figure
    from matplotlib.ticker import LinearLocator, FixedLocator, FormatStrFormatter
    
    import tkinter as tk
    import sys
    
    class E(tk.Tk):
        def __init__(self,parent):
            tk.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))
    
            self.frame = tk.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 = NavigationToolbar2Tk( self.canvas, self )
            self.toolbar.update()
            self.toolbar.pack()
    
            self.btn = tk.Button(self,text='button',command=self.alt)
            self.btn.pack(ipadx=250)
    
            self.draw_sphere()
    
        def alt (self):
            self.draw_sphere(5)
        def dest(self):
            self.destroy()
            sys.exit()
        def draw_sphere(self, prop=10):
            self.fig.clear()
            ax = Axes3D(self.fig, auto_add_to_figure=False)
            self.fig.add_axes(ax)
    
            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()
    
    
    
    if __name__ == "__main__":
        app = E(None)
        app.title('Embedding in TK')
        app.mainloop()
    

    【讨论】:

      猜你喜欢
      • 2011-05-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多