最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - tkinter resizes when adding matplotlib to reserverd space - Stack Overflow

programmeradmin2浏览0评论

I have a gui with an empty tk.Canvas widget which I want to fill during runtime with a matplotlib plot. However, when adding the plot, it forces the gui to use the plot size instead of using the provided space, moving stuff around in the gui. What can I do such that the plot is created with the same dimensions as the Canvas?

MWE:

import matplotlib as mpl
mpl.use('TkAgg')

import tkinter as tk
from matplotlib import figure
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg

root = tk.Tk()

def show_plot():
    fig = figure.Figure()
    ax=fig.add_subplot()
    ax.scatter([1, 2, 3, 4], [4, 7, 2, 4])
    canv = FigureCanvasTkAgg(fig, canvas)
    canv.draw()
    canv.get_tk_widget().pack(fill=tk.BOTH, expand=1)

canvas = tk.Canvas(root, width=600, height=600)
canvas.pack(fill=tk.NONE, expand=0)
tk.Button(root, text='show plot', command=show_plot).pack()

root.mainloop()

I already tried changing the fill and expand parameters in the .pack() calls for both the plot and the canvas but that didn't work.

I have a gui with an empty tk.Canvas widget which I want to fill during runtime with a matplotlib plot. However, when adding the plot, it forces the gui to use the plot size instead of using the provided space, moving stuff around in the gui. What can I do such that the plot is created with the same dimensions as the Canvas?

MWE:

import matplotlib as mpl
mpl.use('TkAgg')

import tkinter as tk
from matplotlib import figure
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg

root = tk.Tk()

def show_plot():
    fig = figure.Figure()
    ax=fig.add_subplot()
    ax.scatter([1, 2, 3, 4], [4, 7, 2, 4])
    canv = FigureCanvasTkAgg(fig, canvas)
    canv.draw()
    canv.get_tk_widget().pack(fill=tk.BOTH, expand=1)

canvas = tk.Canvas(root, width=600, height=600)
canvas.pack(fill=tk.NONE, expand=0)
tk.Button(root, text='show plot', command=show_plot).pack()

root.mainloop()

I already tried changing the fill and expand parameters in the .pack() calls for both the plot and the canvas but that didn't work.

Share Improve this question edited Mar 17 at 13:49 Xoriun asked Mar 17 at 12:58 XoriunXoriun 16810 bronze badges 2
  • So you actually want your plot to be chopped off on the right side, due to your Canvas not being wide enough to hold it??? Note that matplotlib is never actually going to draw its plot into your Canvas, it creates a Canvas of its own, which is a child of the widget you pass to it. You might as well have used a Frame, and could either call .pack_propagate(0), or use .place() instead of .pack(), to keep it from resizing to fit the actual Canvas. Or, if the purpose of using a Canvas was to make the plot scrollable, add the plot with .create_window() rather than .place(). – jasonharper Commented Mar 17 at 13:38
  • no, I just want the plot to the created with the size of the parent Canvas. I edited my question to be more precise. – Xoriun Commented Mar 17 at 13:48
Add a comment  | 

1 Answer 1

Reset to default 0

It turned out to be quite simple: I just have to specify the size of the matplotlib figure:

fig = figure.Figure(fig_size=(6, 8), dpi=100)

will generate a plot that is 600 by 800 pixels. Replacing 6 and 8 by the width and height (each divided by the dpi) of the parent canvas solves the problem.

For more information on how to work with exact pixels in matplotlib see Specifying and saving a figure with exact size in pixels

发布评论

评论列表(0)

  1. 暂无评论