Hanan Shteingart
Hanan Shteingart

Reputation: 9078

How to make matplotlib:pyplot resizeable with the Tkinter window in Python?

I am trying to build a grid of frames in each there is a matplotlib figure. When I resize the window the figure remain with fix size and are not resizing to fit the empty space. Is there a way to make the figure change its size according to the canvas (hopefully that it does change with the window size)?

before expanding the window after expanding the window

This is how I do the embedding in each frame:

self._figure = pyplot.figure(figsize=(4,4))       
self._axes = self._figure.add_subplot(111)
self._canvas = FigureCanvasTkAgg(self._figure, master=self._root)
self._canvas.get_tk_widget().grid(row=1,column=1,rowspan = 4)

Upvotes: 5

Views: 9260

Answers (1)

Hans
Hans

Reputation: 2615

This is most likely related to this question. The gist is that there are two parts to making a Tk grid cell grow:

  • Use the sticky keyword when applying grid to your widget, e.g., widget.grid(..., sticky=Tkinter.NSEW (Python 2.7) to make widget be able to grow in all four directions. See this documentation for more details.
  • Tell the parent/master to make the respective column/row grow when resizing by calling parent.grid_columnconfigure(...) and/or parent.grid_rowconfigure(...) with the desired row, column, and weight keyword. E.g., parent.grid_columnconfigure(col=0, weight=1) makes the first column take all available space (as long as there are no other columns, or they have not been similary configured). See the grid_columnconfigure documentation and the grid_rowconfigure documentation for more details, e.g., about how the weights affect multiple columns/rows.

This page contains many more details about grid layouts.

Upvotes: 2

Related Questions