Python 在 tkinter 中运行 matplotlib
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3845407/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Running matplotlib in tkinter
提问by rectangletangle
I have this beautiful sphere I made in matplotlib. How would I go about putting it in a tkinter frame widget? It'd be nice to be able to integrate it into an existing tkinter GUI. Also is it possible to rid of the menu bar below the display? I have no need to save the output or zoom, so it's useless to me.
我有我在 matplotlib 中制作的这个漂亮的球体。我将如何将其放入 tkinter 框架小部件中?能够将它集成到现有的 tkinter GUI 中会很好。是否可以去掉显示屏下方的菜单栏?我不需要保存输出或缩放,所以对我来说没用。
from mpl_toolkits.mplot3d import axes3d,Axes3D
import matplotlib.pyplot as plt
from matplotlib import cm
import numpy as np
fig = plt.figure()
ax = Axes3D(fig) #<-- Note the difference from your original code..
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))
ax.plot_surface(x, y, z, rstride=4, cstride=4, color='lightgreen',linewidth=0)
#,antialiased=False
#cmap=cm.jet
plt.show()
采纳答案by volting
Have a look at the examples for embedding plots in a tk GUI, it should be enough to get you started in the right direction.
看看在 tk GUI 中嵌入图的示例,它应该足以让您朝着正确的方向开始。
user_interfaces example code: embedding_in_tk.py
user_interfaces 示例代码:embedding_in_tk.py
user_interfaces example code: embedding_in_tk2.py
user_interfaces 示例代码:embedding_in_tk2.py
As for removing the toolbar, it's a case of not adding it when you are embedding plots in a GUI.
至于删除工具栏,这是一种在 GUI 中嵌入绘图时不添加它的情况。
If you are using matplotlib.pyplot the toolbar will be created automatically for every figure. If you are writing your own user interface code, you can add the toolbar as a widget.
如果您使用的是 matplotlib.pyplot,则会为每个图形自动创建工具栏。如果您正在编写自己的用户界面代码,则可以将工具栏添加为小部件。

