2016-12-13 1 views
0

J'essaie de tracer un graphique à barres sur un widget Canvas en utilisant tkinter. Sur la base d'un exemple sur «comment tracer un graphique à barres groupées par une colonne que je trouve sur Internet, c'est le programme de test i utilisé pour tracer le graphique que je veux:Tracer un graphique à barres sur une toile en Python

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() 

Le programme devrait tracer un graphique à barres regroupé par «Sexe» et trace la somme de «Vente» pour chaque sexe comme this.

J'ai donc essayé de incoporate ce code dans mon code tkinter, qui ressemble à ceci:

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) 

Je comprends qu'il est complètement faux et je suis attribuer myplot à trois choses différentes. Ma question est comment écrire le bon code pour créer le même intrigue que le code ci-dessus?

+0

ce qui est 'self.top'? Créer simp, e, exemple de travail afin que tout le monde puisse l'exécuter et le tester. – furas

+0

Pourquoi attribuez-vous trois choses différentes à 'myplot ='? lequel espérez-vous obtenir à l'écran? – furas

+0

@furas tellement désolé que c'est déroutant. J'ai édité ma question un peu, j'espère que cela a plus de sens –

Répondre

1

Vous attribuez à myplot= trois éléments différents, donc je ne sais pas ce que vous essayez de tracer.

exemple simple comment tracer pandas données tkinter (utilisant 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() 

enter image description here

Github: furas/python-examples/tkinter/matplot-canvas/example-1


EDIT: Je exemple des données créées

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() 

puis j'ai utilisé ceci pour afficher le canevas.

# --- 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() 

enter image description here

+0

Merci! J'ai réussi à le comprendre en fonction de votre exemple. Tout ce dont j'avais besoin était le 'ax = myplot', donc je l'ai changé pour ceci: ' test = self.df [[x, y]] test = test.groupby (x) .sum() test.plot (kind = 'bar', legend = Faux, ax = myplot) ' –

+0

voir le nouveau code en réponse. – furas