6

Python 3.9 on Mac running OS 11.6.1. My application involves placing a plot on a frame inside my root window, and I'm struggling to get the plot to take up a larger portion of the window. I thought rcParams in matplotlib.pyplot would take care of this, but I must be overlooking something.

Here's what I have so far:

import numpy as np
from tkinter import Tk,Frame,TOP,BOTH

import matplotlib
from matplotlib import pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg

plt.rcParams["figure.figsize"] = [18,10]

root=Tk()
root.wm_title("Root Window")
root.geometry('1500x1000')

x = np.linspace(0, 2 * np.pi, 400)
y = np.sin(x ** 2)
fig, ax = plt.subplots()
ax.plot(x, y)

canvas_frame=Frame(root) # also tried adjusting size of frame but that didn't help
canvas_frame.pack(side=TOP,expand=True)
canvas = FigureCanvasTkAgg(fig, master=canvas_frame)
canvas.draw()
canvas.get_tk_widget().pack(side=TOP,fill=BOTH,expand=True)


root.mainloop()

For my actual application, I need for canvas to have a frame as its parent and not simply root, which is why canvas_frame is introduced above.

12
  • "I need for canvas to have a frame as its parent and not simply root" It's not clear what is stopping you from doing that. Is the problem as simple as adding fill="both" when calling canvas_frame.pack? Commented Jan 5, 2022 at 20:07
  • 3
    This problem doesn't have to do with adding the canvas to the window in the correct way so much as removing the extra space from inside the canvas. If you add a line after creating the canvas that fills it with red, and then rapidly drag the window's sides around to resize it, the plot will flicker, and you can see the red behind. The red fills the whole window, so the canvas is filling as much of the window as it can. Changing the way you add the canvas to the window is not going to do much. Just eliminating the possibilities. Commented Jan 5, 2022 at 21:06
  • 2
    To fill the canvas with a certain color, you can use bg="red". To test this yourself, just add canvas.get_tk_widget().config(bg="red") right before you call root.mainloop(). Then, unmaximize the window and drag one of the sides quickly back and forth. If your computer is slow enough (like mine), you should be able to see flickers of red filling the window. Commented Jan 8, 2022 at 19:06
  • 1
    Yes, I have--see response above from 1/5 20:23. This merely moves the plot to the top left of my window. Commented Jan 10, 2022 at 18:46
  • 2
    Would you mind including a screenshot of how this looks for you, and what improvement you would like to see? Commented Jan 12, 2022 at 16:42

2 Answers 2

4
+100

try something like this:

fig.subplots_adjust(left=0.05, bottom=0.07, right=0.95, top=0.95, wspace=0, hspace=0)

this is output, figure now takes more screen area % [figure now takes more screen realestate1

Sign up to request clarification or add additional context in comments.

1 Comment

--Thanks so much for your solution. You may be interested in a similar problem (with image) I posted at stackoverflow.com/questions/70598913/… The primary difference is that the plot is an animation, and it's being embedded on a Toplevel instead of a Frame.
1

I addition to the suggestion of changing the margins, you could also add fill=BOTH to the canvas widget.

canvas_frame.pack(side=TOP,expand=True,fill=BOTH)

This makes the figure canvas take up more space, but the plot doesn't take the whole window. However, if I resize the window, then the plot takes the whole window.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.