我正在尝试使用tkinter将条形图绘制到Canvas小部件上。基于我在互联网上找到的“如何绘制按列分组的条形图”的示例,这是我用来绘制我想要的图形的测试程序:
df=pd.read_excel("testdata.xlsx")
x = 'Gender'
y = 'Sales'
a = df[[x, y]]
a = a.groupby(x).sum()
a.plot(kind='bar',legend=False)
plt.show()
该程序应绘制一个按“性别”分组的条形图,并绘制每个性别的“销售”总和,如this。
所以我试图把这段代码放到我的tkinter代码中,看起来像这样:
f = Figure()
myplot = f.add_subplot(111)
myplot = self.df[[x, y]] #make a subset df of the columns to be plotted
myplot = myplot.groupby(x).sum() #groupby column 'x' and calculate sum
myplot.plot(kind='bar',legend=False)
self.Canvas1 = FigureCanvasTkAgg(f, master=self.top) #self.top is the main window of the gui
self.Canvas1.get_tk_widget().place(relx=0.045, rely=0.545, relheight=0.4, relwidth=0.91)
我理解这是完全错误的,我将myplot
分配给三个不同的东西。我的问题是如何编写正确的代码来创建与上面的代码相同的图?
答案 0 :(得分:1)
您为myplot=
分配了三个不同的元素,因此我不知道您尝试绘制的内容。
如何在pandas
中绘制tkinter
数据的简单示例(使用matplotlib
)
#!/usr/bin/env python3
#
# http://matplotlib.org/examples/user_interfaces/embedding_in_tk.html
#
# --- matplotlib ---
import matplotlib
matplotlib.use('TkAgg') # choose backend
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
from matplotlib.pyplot import Figure
# --- other ---
import tkinter as tk
import pandas as pd
# --- for example data --
import random
import math
# --- random data ---
df1 = pd.DataFrame([random.randint(-100, 100) for _ in range(60)])
df2 = pd.DataFrame([math.sin(math.radians(x*6)) for x in range(60)])
# --- GUI ---
root = tk.Tk()
# top frame for canvas and toolbar - which need `pack()` layout manager
top = tk.Frame(root)
top.pack()
# bottom frame for other widgets - which may use other layout manager
bottom = tk.Frame(root)
bottom.pack()
# --- canvas and toolbar in top ---
# create figure
fig = matplotlib.pyplot.Figure()
# create matplotlib canvas using `fig` and assign to widget `top`
canvas = FigureCanvasTkAgg(fig, top)
# get canvas as tkinter widget and put in widget `top`
canvas.get_tk_widget().pack()
# create toolbar
toolbar = NavigationToolbar2TkAgg(canvas, top)
toolbar.update()
canvas._tkcanvas.pack()
# --- first plot ---
# create first place for plot
ax1 = fig.add_subplot(211)
# draw on this plot
df1.plot(kind='bar', legend=False, ax=ax1)
# --- second plot ---
# create second place for plot
ax2 = fig.add_subplot(212)
# draw on this plot
df2.plot(kind='bar', legend=False, ax=ax2)
# --- other widgets in bottom ---
b = tk.Button(bottom, text='Exit', command=root.destroy)
b.pack()
# --- start ----
root.mainloop()
Github:furas/python-examples/tkinter/matplot-canvas/example-1
编辑:我创建了示例数据
data = {
'Gender':['F', 'M', 'F','M'],
'Sales': [1, 2, 3, 4],
}
df = pd.DataFrame(data)
x = 'Gender'
y = 'Sales'
new_df = df[[x, y]].groupby(x).sum()
然后我用它来显示画布。
# --- matplotlib ---
import matplotlib
matplotlib.use('TkAgg') # choose backend
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
from matplotlib.pyplot import Figure
# --- other ---
import tkinter as tk
import pandas as pd
# --- GUI ---
root = tk.Tk()
# top frame for canvas and toolbar - which need `pack()` layout manager
top = tk.Frame(root)
top.pack()
# bottom frame for other widgets - which may use other layout manager
bottom = tk.Frame(root)
bottom.pack()
# --- canvas and toolbar in top ---
# create figure
fig = matplotlib.pyplot.Figure()
# create matplotlib canvas using `fig` and assign to widget `top`
canvas = FigureCanvasTkAgg(fig, top)
# get canvas as tkinter widget and put in widget `top`
canvas.get_tk_widget().pack()
# create toolbar
toolbar = NavigationToolbar2TkAgg(canvas, top)
toolbar.update()
canvas._tkcanvas.pack()
# --- plot ---
data = {
'Gender':['F', 'M', 'F','M'],
'Sales': [1, 2, 3, 4],
}
df = pd.DataFrame(data)
x = 'Gender'
y = 'Sales'
new_df = df[[x, y]].groupby(x).sum()
# create first place for plot
ax = fig.add_subplot(111)
# draw on this plot
new_df.plot(kind='bar', legend=False, ax=ax)
# --- other widgets in bottom ---
b = tk.Button(bottom, text='Exit', command=root.destroy)
b.pack()
# --- start ----
root.mainloop()