Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 windowafter 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)
like image 498
Hanan Shteingart Avatar asked Nov 11 '22 22:11

Hanan Shteingart


1 Answers

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.

like image 160
Hans Avatar answered Nov 14 '22 22:11

Hans