Skip to content

Labels don't rotate when set by pyplot.subplots #7150

@matanagasauce

Description

@matanagasauce

I expect the code

figure, axes = plt.subplots(2, 2, subplot_kw={
    'xticks': range(30),
    'xticklabels': [matplotlib.text.Text(text=date.strftime('%m%d'),
                                         rotation=45) for date in stat_dates_list]
})

to perform like this:
46066c58-fe47-49b1-bfa9-c794a3ef2dcc

but got:
2ae8545d-12d3-49de-b6b7-272280fe1c81

And I wrote

for ax in axes.flatten():
    for label in ax.get_xticklabels():
        label.set_rotation(45)

and finally got what I want.

I look into axis.py and find that in Axes.set_ticklabels,

get_labels = []
for t in ticklabels:
    # try calling get_text() to check whether it is Text object
    # if it is Text, get label content
    try:
        get_labels.append(t.get_text())
    # otherwise add the label to the list directly
    except AttributeError:
        get_labels.append(t)
# replace the ticklabels list with the processed one
ticklabels = get_labels

It totally ignores the kwargs of t if t is Text like what's in my code above.
I think it would be more reasonable if it got not only the label content but also other kwargs if it is Text.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions