From 185b4fae5dc157c45a7255d4979a1c4a35c27893 Mon Sep 17 00:00:00 2001 From: Owl Date: Wed, 30 Apr 2025 13:41:20 +0800 Subject: [PATCH] fix: Fix unstable tkagg small plot size. --- lib/matplotlib/backends/_backend_tk.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/lib/matplotlib/backends/_backend_tk.py b/lib/matplotlib/backends/_backend_tk.py index 0bbff1379ffa..b1443bf2c882 100644 --- a/lib/matplotlib/backends/_backend_tk.py +++ b/lib/matplotlib/backends/_backend_tk.py @@ -638,6 +638,14 @@ def __init__(self, canvas, window=None, *, pack_toolbar=True): window = canvas.get_tk_widget().master tk.Frame.__init__(self, master=window, borderwidth=2, width=int(canvas.figure.bbox.width), height=50) + # Avoid message_label expanding the toolbar size, and in turn expanding the + # canvas size. + # Without pack_propagate(False), when the user defines a small figure size + # (e.g. 2x2): + # 1. Figure size that is bigger than the user's expectation. + # 2. When message_label is refreshed by mouse enter/leave, the canvas + # size will also be changed. + self.pack_propagate(False) self._buttons = {} for text, tooltip_text, image_file, callback in self.toolitems: