在matplotlib中更新图形坐标 [英] Updating a graphs coordinates in matplotlib

查看:72
本文介绍了在matplotlib中更新图形坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在下面的代码中将绘出一个球体,它的比例由 prop 定义,我希望这样,当按下按钮时 prop 的值更改为 5 并相应调整图形.我该怎么办?

I have below a code that will plot a sphere, it's proportions are defined by prop, I'd like it so when the button is pressed prop's value changes to 5 and the graph is adjusted accordingly. How do I go about this?

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

I know tkinter has .configure(), which allows one to adjust widget settings. I'm looking for something similar so that I can reconfigure my plot.

#!/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()

推荐答案

修改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 类中):

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

这篇关于在matplotlib中更新图形坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆