You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Bug]: Labels don't get wrapped when set_yticks() is used in subplots #28358
When plotting bar charts in subplots with very long labels, the option of wrapping text only works on the first plotted subplot, despite passing wrap=True to set_yticks() in both cases.
Code for reproduction
importmatplotlib.pyplotaspltimportnumpyasnplong_text_label='very long category label i want to wrap'labels= [f"{long_text_label}_{i}"foriinrange(5)]
values=np.arange(1, 6)
fig, axes=plt.subplots(1, 2)
axes[0].barh(np.arange(len(labels)), values)
axes[0].set_yticks(np.arange(len(labels)), labels=labels, wrap=True)
axes[1].barh(np.arange(len(labels)), values)
axes[1].set_yticks(np.arange(len(labels)), labels=labels, wrap=True)
Actual outcome
Expected outcome
The label text on the y axis should appear wrapped on both subplots
I'm not clear what the correct behaviour is here though. Currently it's wrapping on the edge of the figure. What would the inner axes wrap on? The axes to the left presumably, but that isn't conceptually straight forward as an axes knows about its figure but not about its neighbours, and it knows its own spine position but doesn't try to reserve space for itself outside those spines. Note we usually do the opposite and make the axes further apart to accommodate the ytick labels.
For the current architecture, this is the expected behavior. We should better document what warp can or can't do.
I see two possible ways to improve:
hard: Assign each axes a bounding box. This should work well with subplots/subplot_mosaic, but I'm unclear how other axes creation methods would handle this. Up to now, the Axes is defined via the data area, ticks and labels just spill outside as far as they need.
medium: Expand the wrap functionality to allow wrapping after N characters and/or specifying a maximal width.
@jklymak When #28177 is in, we could switch the wrapping box (in Text._get_wrap_line_width) to subfigure instead of figure. IMHO this boundary makes more sense. And it would at least allow to get what the OP wants using subfigures:
import matplotlib.pyplot as plt
import numpy as np
long_text_label = 'very long category label i want to wrap'
labels = [f"{long_text_label}_{i}" for i in range(5)]
values = np.arange(1, 6)
fig = plt.figure()
subfigs = fig.subfigures(1, 2)
ax0 = subfigs[0].subplots()
ax1 = subfigs[1].subplots()
ax0.barh(np.arange(len(labels)), values)
ax0.set_yticks(np.arange(len(labels)), labels=labels, wrap=True)
ax1.barh(np.arange(len(labels)), values)
ax1.set_yticks(np.arange(len(labels)), labels=labels, wrap=True)
That's likely fine. However it should be noted that even in the single subplot situation the wrap is incompatible with layout management since that adjusts the size of the axes to account for the size of the labels versus wrapping the labels.
As stated I am mildly opposed to us jumping through hoops to allow wrapping tick labels because I don't think it s a generally useful thing to do. However if the issues with wrapping tick labels also extends to other text boxes in subfigures, that might merit some effort to fix.
Activity
rcomer commentedon Jun 9, 2024
Thanks for the clear report @soogui. I confirm that I have reproduced this with our
maindevelopment branch.jklymak commentedon Jun 9, 2024
I'm not clear what the correct behaviour is here though. Currently it's wrapping on the edge of the figure. What would the inner axes wrap on? The axes to the left presumably, but that isn't conceptually straight forward as an axes knows about its figure but not about its neighbours, and it knows its own spine position but doesn't try to reserve space for itself outside those spines. Note we usually do the opposite and make the axes further apart to accommodate the ytick labels.
timhoffm commentedon Jun 11, 2024
For the current architecture, this is the expected behavior. We should better document what
warpcan or can't do.I see two possible ways to improve:
wrapfunctionality to allow wrapping after N characters and/or specifying a maximal width.jklymak commentedon Jun 11, 2024
I think the conceptually simplest is to not allow auto wrap for tick labels. Folks can manually wrap if they need to.
timhoffm commentedon Jun 11, 2024
Well, the case of one subplot with long tick labels works. IMHO we should not break that.
Just documenting that wrapping is limited to the figure boundary is good enough to manage expectations on the current behavior.
DOC: Clarify scope of wrap.
timhoffm commentedon Jun 12, 2024
@jklymak When #28177 is in, we could switch the wrapping box (in
Text._get_wrap_line_width) to subfigure instead of figure. IMHO this boundary makes more sense. And it would at least allow to get what the OP wants using subfigures:--> Created a separate issue for this #28378.
jklymak commentedon Jun 12, 2024
That's likely fine. However it should be noted that even in the single subplot situation the wrap is incompatible with layout management since that adjusts the size of the axes to account for the size of the labels versus wrapping the labels.
As stated I am mildly opposed to us jumping through hoops to allow wrapping tick labels because I don't think it s a generally useful thing to do. However if the issues with wrapping tick labels also extends to other text boxes in subfigures, that might merit some effort to fix.
FIX: Fix text wrapping
FIX: Fix text wrapping
timhoffm commentedon Jun 15, 2024
When #28401 is in, one can write the original plot using subfigures:
FIX: Fix text wrapping