Skip to content

[Bug]: Cannot reset to original positions of x-tick labels after wrapping #31246

@smith2936

Description

@smith2936

Bug summary

This bug arises when I set wrap to True for rotated x-tick labels and draw, but resetting wrap to False cannot recover the original position of the x-tick labels.

Code for reproduction

import datetime
import numpy as np
import matplotlib.pyplot as plt

base = datetime.datetime(2005, 2, 1)
dates = [base + datetime.timedelta(hours=(2 * i)) for i in range(732)]
N = len(dates)
np.random.seed(19680801)
y = np.cumsum(np.random.randn(N))

fig, ax = plt.subplots(layout='constrained', figsize=(6, 6))
ax.plot(dates, y)
ax.set_xlim(np.datetime64('2005-02'), np.datetime64('2005-04'))
ax.tick_params(rotation=45)
xticklabels = ax.get_xticklabels()

for label in xticklabels: 
    label.set_wrap(False)
plt.savefig('plot1.png')
print("(plot1) first x-tick label position:", xticklabels[0].get_position())

for label in xticklabels: 
    label.set_wrap(True)
fig.draw_without_rendering()

for label in xticklabels: 
    label.set_wrap(False)
plt.savefig('plot2.png')
print("(plot2) first x-tick label position:", xticklabels[0].get_position())

Actual outcome

(plot1) first x-tick label position: (np.float64(12815.0), 0)
(plot2) first x-tick label position: (np.float64(12815.0), 0)

plot1.png

Image

plot2.png

Image

Expected outcome

(plot1) first x-tick label position: (np.float64(12815.0), 0)
(plot2) first x-tick label position: (np.float64(12815.0), 0)

plot1.png and plot2.png are identical.

Additional information

No response

Operating system

No response

Matplotlib Version

0.2.0.dev53778+unknown.g607d2c380.g29b356467

Matplotlib Backend

No response

Python version

No response

Jupyter version

No response

Installation

None

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions