Python Forum
Matplotlib figsize and centering issue on canvas - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: GUI (https://python-forum.io/forum-10.html)
+--- Thread: Matplotlib figsize and centering issue on canvas (/thread-27543.html)



Matplotlib figsize and centering issue on canvas - deffonotsean - Jun-10-2020

I'm trying to center a Matplotlib chart in a GUI using Tkinter. The issue is I can't seem to center the figure in the center of the canvas (as you can see in the image below, the figure is sticking to the top of the canvas and a little to the right). I'm also using the 'figsize' parameter for the figure but it's not in fact changing the size. I've also tried to adjust subplots but that also seems to have no effect. Why does figsize not seem to work, and how can I center the figure in the canvas?

[Image: phJ4d.png]

class PageTwo(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        top = tk.Frame(self)
        bottom = tk.Frame(self)
        top.pack(side=tk.TOP, expand = False)
        bottom.pack(side=tk.BOTTOM, fill=tk.BOTH, expand=True)

        label = tk.Label(self, text="DeReKo Stats", font=LARGE_FONT)
        btn = ttk.Button(self, text="Return",
                        command=lambda: controller.show_frame(StartPage))
        btn2 = ttk.Button(self, text="Jsyncc Stats",
                        command=lambda: controller.show_frame(PageOne))
        btn3 = ttk.Button(self, text="Weirdness",
                        command=lambda: controller.show_frame(PageThree))
        txtbox = ttk.Entry(self, width=5)

        label.pack(in_=top, side=tk.TOP,pady=10, padx=10)
        btn.pack(in_=top, side=tk.LEFT)
        btn2.pack(in_=top, side=tk.LEFT)
        btn3.pack(in_=top, side=tk.LEFT)
        txtbox.pack(in_=top, side=tk.LEFT, padx=5)

        with open(DEREKO_FILE_NAME, buffering=20000000, encoding="utf-8") as f:
            freq = FreqDist(json.loads(f.read()))

        fig = plt.figure(figsize=(1,1))
        ax1 = fig.add_subplot(111)
        df1 = DataFrame(freq.most_common(10), columns=['Token', 'Frequency'])
        df1 = df1[['Token', 'Frequency']].groupby(['Token'], sort=False).sum()
        df1.plot(kind='bar', legend=True, ax=ax1, rot=0)
        plt.subplots_adjust(hspace=5, right=.95)

        canvas = FigureCanvasTkAgg(fig, self)
        canvas.draw()
        canvas.get_tk_widget().pack(in_=bottom, expand=True, fill=tk.BOTH, pady=(30,10), padx=(15,15))

        MainWindow.grid_conf(self, tk.Frame)