Tommy
Tommy

Reputation: 144

python, tkinter, issues while creating bar graph

I'm getting the following error message when I try to create a bars graph, using python and tkinter:

AttributeError: 'FigureCanvasTkAgg' object has no attribute 'show'

Here's my code

import matplotlib,numpy,sys
matplotlib.use('TkAgg')
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
from tkinter import *
root=Tk()
f=Figure(figsize=(5,4),dpi=100)
ax=f.add_subplot(111)
data=(20,35,30,35,27)
ind=numpy.arange(5)  # the x locations for the groups
width=.5
rects1=ax.bar(ind,data,width)
canvas=FigureCanvasTkAgg(f,master=root)
canvas.show()
canvas.get_tk_widget().pack(side=TOP,fill=BOTH,expand=1)
root.mainloop()

Upvotes: 0

Views: 137

Answers (1)

imxitiz
imxitiz

Reputation: 3987

Simply delete the line that's causing the problem:

import matplotlib,numpy,sys
matplotlib.use('TkAgg')

from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
from tkinter import Tk,TOP,BOTH

root=Tk()
f=Figure(figsize=(5,4),dpi=100)
ax=f.add_subplot(111)
data=(20,35,30,35,27)
ind=numpy.arange(5)  # the x locations for the groups
width=.5
rects1=ax.bar(ind,data,width)
canvas=FigureCanvasTkAgg(f,master=root)
canvas.get_tk_widget().pack(side=TOP,fill=BOTH,expand=1)

root.mainloop()

Or replace .show() with .draw() as @acw1668 mentioned in comment.

Upvotes: 1

Related Questions