[英]Embedding Matplotlib in Tkinter, display problems
I'm currently trying to graph a sphere in a tkinter window using matplotlib. 我目前正在尝试使用matplotlib在tkinter窗口中绘制球体图。 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'
You can make use of pyplot.figure()
's figsize
paramater to set the figure size. 您可以使用
pyplot.figure()
的figsize
参数来设置图形大小。
eg 例如
self.fig = plt.figure(figsize=(5,5))
Did you try to set the figure size attributes? 您是否尝试设置图形尺寸属性? fig.set_figwidth and fig.set_figheigh t
fig.set_figwidth和fig.set_figheigh t
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.