在Tkinter中嵌入Matplotlib,显示问题

发布于 2024-09-26 04:26:35 字数 1985 浏览 3 评论 0原文

我目前正在尝试使用 matplotlib 在 tkinter 窗口中绘制球体图。我该如何制作正方形的显示?我希望球体的变形尽可能小。

我的代码:

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

import Tkinter
import sys

class ElectronOrbitalGenerator(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()
        ax = Axes3D(self.fig)

        u = np.linspace(0, 2 * np.pi, 100)
        v = np.linspace(0, np.pi, 100)

        x = 10 * np.outer(np.cos(u), np.sin(v))
        y = 10 * np.outer(np.sin(u), np.sin(v))
        z = 10 * 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', expand=1)


        self.canvas._tkcanvas.pack(side='top', fill='both', expand=1)

        self.btn = Tkinter.Button(self,text='button',command=self.alt)
        self.btn.pack()

    def alt (self):
        print 9
    def dest(self):
        self.destroy()
        sys.exit()



if __name__ == "__main__":
    app = ElectronOrbitalGenerator(None)
    app.title('Embedding in TK')
    app.mainloop()

编辑

我尝试

    self.fig.set_figwidth(100.0)
    self.fig.set_figheigth(100.0)

并得到了这个

AttributeError: 'Figure' object has no attribute 'set_figheigth'

I'm currently trying to graph a sphere in a tkinter window using matplotlib. How do I go about making the display square? I'd like the sphere to have as little distortion as possible.

My code:

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

import Tkinter
import sys

class ElectronOrbitalGenerator(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()
        ax = Axes3D(self.fig)

        u = np.linspace(0, 2 * np.pi, 100)
        v = np.linspace(0, np.pi, 100)

        x = 10 * np.outer(np.cos(u), np.sin(v))
        y = 10 * np.outer(np.sin(u), np.sin(v))
        z = 10 * 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', expand=1)


        self.canvas._tkcanvas.pack(side='top', fill='both', expand=1)

        self.btn = Tkinter.Button(self,text='button',command=self.alt)
        self.btn.pack()

    def alt (self):
        print 9
    def dest(self):
        self.destroy()
        sys.exit()



if __name__ == "__main__":
    app = ElectronOrbitalGenerator(None)
    app.title('Embedding in TK')
    app.mainloop()

EDIT

I tried

    self.fig.set_figwidth(100.0)
    self.fig.set_figheigth(100.0)

and got this

AttributeError: 'Figure' object has no attribute 'set_figheigth'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜雨飘雪 2024-10-03 04:26:35

您可以使用pyplot.figure()figsize参数来设置图形大小。

例如

self.fig = plt.figure(figsize=(5,5))

You can make use of pyplot.figure()'s figsize paramater to set the figure size.

e.g.

self.fig = plt.figure(figsize=(5,5))

绅刃 2024-10-03 04:26:35

您是否尝试设置图形尺寸属性?
Fig.set_figwidth 和 Fig.set_figheigh
t

Did you try to set the figure size attributes?
fig.set_figwidth and fig.set_figheigh
t

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文