Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Bug]: Erroneous text wrapping when text vertical position is 1.0 #31537

@AL4N23

Description

@AL4N23

Bug summary

Setting Figure.text y to 1.0 results in it not being able to properly adjust line width for text wrapping. Given example makes use of tkinter embedding

Code for reproduction

import tkinter
import matplotlib
import matplotlib.backends.backend_tkagg
import matplotlib.figure


matplotlib.use("tkagg")

main_window = tkinter.Tk()
canvas_figure = matplotlib.figure.Figure(figsize=(1.0, 1.0))
canvas_text = canvas_figure.text(0.0, 1.0, "Helo World! thisisalongassstring aneventextralongassstring", verticalalignment="top", horizontalalignment="left", multialignment="left", wrap=True, snap=True)

mpl_canvas = matplotlib.backends.backend_tkagg.FigureCanvas(figure=canvas_figure, master=main_window)

mpl_canvas.get_tk_widget().grid(row=0, column=0, rowspan=1, columnspan=1, sticky="nsew")
main_window.rowconfigure(0, weight=1)
main_window.columnconfigure(0, weight=1)

main_window.mainloop()

Actual outcome

Image

when dynamically resizing:
Image

Expected outcome

Image Image

Additional information

Wrapping seems to work fine for ranges inside 0-1 but not exactly 1. Simple workaround is to set position not exactly 1 (0.99 in working example). it would be nice however, to be explicit and either warn or handle the exact case as I would argue that it is the natural setting when attempting to position the text on the top.

Looking around, I think the issue has to do with the Text method get_dist_to_box being unable to handle edge cases. Specifically cases where either the text position exceeds the bounds of the figure, or when division by zero occurs due to cosine evaluating to 0.

Operating system

Windows 10 Pro Version 22H2 9045.6466

Matplotlib Version

3.10.8

Matplotlib Backend

tkagg

Python version

3.14.4

Jupyter version

No response

Installation

pip

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions