我正在创建一个程序,将matplotlib包含到tkinter中。该程序的大部分工作包括显示图形的画布,但是,图形标题,导航工具栏和xy图例不显示。我想知道的代码部分就在脚本的底部:
toolbar = NavigationToolbar2TkAgg( canvas, root )
toolbar.update()
canvas._tkcanvas.grid(row=9,column=0)
plt.xlabel('x label')
plt.ylabel('y label')
plt.title('Graph')
plt.show()
有没有人有任何建议?任何帮助都将非常感谢!
from matplotlib import pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
from matplotlib.figure import Figure
from tkinter import *
import tkinter as Tk
import numpy as np
import math
import matplotlib
matplotlib.use('TkAgg')
from matplotlib.backend_bases import key_press_handler
import sys
root = Tk.Tk()
#Interface-----------------------------------------------------------
title_label = Button(root,text = "Add graph title", padx=2,pady=2)
xlabel = Button(root,text = "Add X values ", padx=2,pady=2)
ylabel = Button(root,text = "Add Y values ", padx=2,pady=2)
nameXaxis = Button(root,text = "Name X axis ", padx=2,pady=2)
nameYaxis = Button(root,text = "Name Y axis ", padx=2,pady=2)
meanLabel = Button(root,text = "Mean ")
stderrorLabel = Button(root,text = "StdError: ")
barGraph = Button(root,text = "Bar Graph ",fg = "red", padx=2,pady=2)
lineGraph = Button(root,text = "Line Graph", fg = "red",padx=2,pady=2)
pieGraph = Button(root,text = "Pie Graph ",fg = "red",padx=2,pady=2)
titleEntry = Entry(root)
xentry = Entry(root)
yentry = Entry(root)
nameXaxisEntry = Entry(root)
nameYaxisEntry = Entry(root)
meanText = Text(root,height=1,width=4)
stderrText = Text(root,height=1,width=4)
title_label.grid(row = 0, column = 0,sticky = E)
xlabel.grid(row = 1, column = 0, sticky = E)
ylabel.grid(row = 2, column = 0, sticky = E)
nameXaxis.grid(row = 3, column = 0, sticky = E)
nameYaxis.grid(row = 4, column = 0, sticky = E)
barGraph.grid(row = 0,column = 1,ipadx=10,sticky=W)
lineGraph.grid(row = 1,column = 1,ipadx=10,sticky=W)
pieGraph.grid(row = 2,column = 1,ipadx=10,sticky=W)
meanLabel.grid(row = 3,column = 1,ipadx=10,sticky=W)
stderrorLabel.grid(row = 4,column = 1,ipadx=10,sticky=W)
titleEntry.grid(row = 0, column = 0,ipadx=100,sticky=W)
xentry.grid(row = 1, column = 0,ipadx=100,sticky=W)
yentry.grid(row = 2, column = 0,ipadx=100,sticky=W)
nameXaxisEntry.grid(row = 3, column = 0,ipadx=100,sticky=W)
nameYaxisEntry.grid(row = 4, column = 0,ipadx=100,sticky=W)
meanText.grid(row=3,column=3,sticky=W)
stderrText.grid(row=4,column=3,sticky=W)
# Adding line graph to Canvas--------------------------------------------
root.title("Naynts Graphs")
fig = Figure(figsize=(5,4), dpi=100)
ax = fig.add_subplot(111)
canvas = FigureCanvasTkAgg(fig,root)
canvas.show()
canvas.get_tk_widget().grid(row=7,column=0)
toolbar = NavigationToolbar2TkAgg( canvas, root )
toolbar.update()
canvas._tkcanvas.grid(row=9,column=0)
# Adding features to graph
plt.xlabel('x label')
plt.ylabel('y label')
plt.title('Graph')
plt.show()
root.mainloop()
答案 0 :(得分:2)
我知道这个答案可能会有点晚,但我最近偶然发现了这个问题,而且你可以做些什么:
对于工具栏的问题我可以参考这个问题: Displaying Matplotlib Navigation Toolbar in Tkinter via grid
NavigationToolbar在内部调用.pack()并且pack()和grid()不能一起工作。但是你可以在根框架上创建一个Tk.Frame并将工具栏打包在那里:
ToString()
如果要显示轴标签,请调用plt.figure而不是图。这适用于我的情况:
toolbar_frame = Tk.Frame(root)
toolbar_frame.grid(row=9,column=0)
toolbar = NavigationToolbar2TkAgg(canvas, toolbar_frame)
toolbar.update()
您编写代码的方式对我的设置没有影响,因此我做了一些其他更改。结果如下:
fig = plt.figure(figsize=(5,4), dpi=100)
我认为您使用了许多不必要的导入,这些导入可能会相互冲突,对我来说就是这种情况
from matplotlib import pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
#from matplotlib.figure import Figure
#from Tkinter import *
import Tkinter as Tk
#import numpy as np
#import math
#import matplotlib
#matplotlib.use('TkAgg')
#from matplotlib.backend_bases import key_press_handler
#import sys
root = Tk.Tk()
#Interface-----------------------------------------------------------
title_label = Tk.Button(root,text = "Add graph title", padx=2,pady=2)
xlabel = Tk.Button(root,text = "Add X values ", padx=2,pady=2)
ylabel = Tk.Button(root,text = "Add Y values ", padx=2,pady=2)
nameXaxis = Tk.Button(root,text = "Name X axis ", padx=2,pady=2)
nameYaxis = Tk.Button(root,text = "Name Y axis ", padx=2,pady=2)
meanLabel = Tk.Button(root,text = "Mean ")
stderrorLabel = Tk.Button(root,text = "StdError: ")
barGraph = Tk.Button(root,text = "Bar Graph ",fg = "red", padx=2,pady=2)
lineGraph = Tk.Button(root,text = "Line Graph", fg = "red",padx=2,pady=2)
pieGraph = Tk.Button(root,text = "Pie Graph ",fg = "red",padx=2,pady=2)
titleEntry = Tk.Entry(root)
xentry = Tk.Entry(root)
yentry = Tk.Entry(root)
nameXaxisEntry = Tk.Entry(root)
nameYaxisEntry = Tk.Entry(root)
meanText = Tk.Text(root,height=1,width=4)
stderrText = Tk.Text(root,height=1,width=4)
title_label.grid(row = 0, column = 0,sticky = Tk.E)
xlabel.grid(row = 1, column = 0, sticky = Tk.E)
ylabel.grid(row = 2, column = 0, sticky = Tk.E)
nameXaxis.grid(row = 3, column = 0, sticky = Tk.E)
nameYaxis.grid(row = 4, column = 0, sticky = Tk.E)
barGraph.grid(row = 0,column = 1,ipadx=10,sticky=Tk.W)
lineGraph.grid(row = 1,column = 1,ipadx=10,sticky=Tk.W)
pieGraph.grid(row = 2,column = 1,ipadx=10,sticky=Tk.W)
meanLabel.grid(row = 3,column = 1,ipadx=10,sticky=Tk.W)
stderrorLabel.grid(row = 4,column = 1,ipadx=10,sticky=Tk.W)
titleEntry.grid(row = 0, column = 0,ipadx=100,sticky=Tk.W)
xentry.grid(row = 1, column = 0,ipadx=100,sticky=Tk.W)
yentry.grid(row = 2, column = 0,ipadx=100,sticky=Tk.W)
nameXaxisEntry.grid(row = 3, column = 0,ipadx=100,sticky=Tk.W)
nameYaxisEntry.grid(row = 4, column = 0,ipadx=100,sticky=Tk.W)
meanText.grid(row=3,column=3,sticky=Tk.W)
stderrText.grid(row=4,column=3,sticky=Tk.W)
# Adding line graph to Canvas--------------------------------------------
root.title("Naynts Graphs")
#fig = Figure(figsize=(5,4), dpi=100)
fig = plt.figure(figsize=(5,4), dpi=100)
ax = fig.add_subplot(111)
canvas = FigureCanvasTkAgg(fig,root)
canvas.show()
canvas.get_tk_widget().grid(row=7,column=0)
toolbar_frame = Tk.Frame(root)
toolbar_frame.grid(row=9,column=0)
toolbar = NavigationToolbar2TkAgg(canvas, toolbar_frame)
toolbar.update()
#canvas._tkcanvas.grid(row=9,column=0)
# Adding features to graph
plt.xlabel('x label')
plt.ylabel('y label')
plt.title('Graph')
plt.show()
#root.mainloop()
Tk.mainloop()
和from Tkinter import *
。希望这有帮助!