Alex Lin
Alex Lin

Reputation: 19

Embedding Mapplotlib pie chart into Tkinter Gui Issue

Embedding Mapplotlib pie chart into Tkinter Gui help!

I am trying to embed a pie chart into my Tkinter window! So far I already have a frame in mind for the graph to be embedded in, frameChartsLT. This frame also already has a canvas, canvasChartsLT, placed over the entire area of the frame so I was hoping to place it on either of the of these but I keep getting the error.

AttributeError: 'tuple' object has no attribute 'set_canvas'

I checked my entire code but I can't even find anywhere where I wrote set_canvas so I am completely lost. Any help will be truly appreciated! I am also a beginner so the simpler the explanation or fix the better for me haha!

This is the portion of my code!

import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg

# Some in-between code that sets the frame and canvas on my window

stockListExp = [ 'AMZN' , 'AAPL', 'JETS', 'CCL', 'NCLH']
stockSplitExp = [15,25,40,10,10]


plt.pie(stockSplitExp, radius=1, labels=stockListExp,autopct='%0.2f%%', shadow=True,) # 2 decimal points after plot

figChart1 = plt.pie(stockSplitExp, radius=1, labels=stockListExp,autopct='%0.2f%%', shadow=True)
plt.axis("equal")
chart1 = FigureCanvasTkAgg(figChart1,frameChartsLT)
chart1.get_tk_widget().place(x=10,y=10

Upvotes: 1

Views: 3619

Answers (1)

Henry Yik
Henry Yik

Reputation: 22503

You should use matplotlib.figure.Figure instead of pyplot when you combine tkinter with matplotlib. Below with modifications to your code:

import tkinter as tk

from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure

root = tk.Tk()
frameChartsLT = tk.Frame(root)
frameChartsLT.pack()

stockListExp = ['AMZN' , 'AAPL', 'JETS', 'CCL', 'NCLH']
stockSplitExp = [15,25,40,10,10]

fig = Figure() # create a figure object
ax = fig.add_subplot(111) # add an Axes to the figure

ax.pie(stockSplitExp, radius=1, labels=stockListExp,autopct='%0.2f%%', shadow=True,)

chart1 = FigureCanvasTkAgg(fig,frameChartsLT)
chart1.get_tk_widget().pack()

root.mainloop()

Upvotes: 3

Related Questions