I'm trying arrange n pie charts in a tkinter grid. Each pie chart should be displayed in a separate grid position. I know that I can achieve the same thing using subplot but I think grid is more convenient because I want to overlay the pie charts with objects afterwards and I think the positions are more accessible as with subplot. Also in this way I have more power for scaling the figure and the charts in relation to each other as well as ensuring the same distance between the charts in all directions. But other suggestions are also welcome!
The closest thing to what I want is the code below. What happens is that indeed a 2x2 grid is created containing the pie charts. The problem is that all of the pie charts are plotted in every grid position so they overlay each other completely (last one on top).
import tkinter as tk
from matplotlib import pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
a = [100, 0], [2/3, 1/3], [1/3, 2/3], [0, 100]
rows = 2
columns = 2
root = tk.Tk()
fig = plt.Figure(figsize=(3, 3))
for i in range(0, 4):
ax = fig.add_subplot()
ax.pie(a[i])
row_i = int(i / columns)
column_i = int(((i / columns) - (int(i / columns))) * columns)
chart = FigureCanvasTkAgg(fig, root)
chart.get_tk_widget().grid(row=row_i, column=column_i)
root.mainloop()
To access the different grid positions I used
row_i = int(i / columns)
column_i = int(((i / columns) - (int(i / columns))) * columns)
which is far from being elegant (because it works only for the most values but not all) but I couldn't find a solution to achieve this in another way. Maybe someone has a better idea for this as well.