Python Forum
Matplotlib figsize and centering issue on canvas
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Matplotlib figsize and centering issue on canvas
#1
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)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Centering and adding a push button to a grid window, TKinter Edward_ 15 4,703 May-25-2023, 07:37 PM
Last Post: deanhystad
  [Tkinter] canvas widget scroll issue chrisdb 2 3,841 Apr-07-2021, 05:48 AM
Last Post: chrisdb
  Centering labels and buttons Heyjoe 8 11,608 Jul-16-2020, 02:39 PM
Last Post: deanhystad
  [Tkinter] Resizing image inside Canvas (with Canvas' resize) Gupi 2 25,089 Jun-04-2019, 05:05 AM
Last Post: Gupi

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020