0

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()
3
  • Yeah! FigureCanvasTkAgg doesn't have show function! What you are trying to do? Commented Aug 4, 2021 at 7:51
  • thx for answer. i need to insert a bars graph into a tkinter window Commented Aug 4, 2021 at 7:52
  • 2
    Call .draw() instead of .show(). Commented Aug 4, 2021 at 7:53

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.