8000 [Bug]: inconsistent results when formatting ticklabels by get_text() in time axis · Issue #26791 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content
[Bug]: inconsistent results when formatting ticklabels by get_text() in time axis #26791
Closed as not planned
@stevenlis

Description

@stevenlis

Bug summary

I'm trying to format ticklabels based on text on a time x axis.I have been experiencing inconsistent results with various formatters.

Code for reproduction

import pandas as pd
import numpy as np
from datetime import date

import matplotlib.pyplot as plt
from matplotlib.dates import MonthLocator
from matplotlib.dates import ConciseDateFormatter

xs = [pd.date_range(f'{y}-07-01', '2021-12-31', freq='M')
      for y in range(2016, 2019)]
ys = [np.random.rand(len(x)) for x in xs]

fig, axs = plt.subplots(3, 1, figsize=(10, 8))
for ax, x, y in zip(axs, xs, ys):
    ax.plot(x, y)

    locator = MonthLocator((1, 4, 7, 10))
    ax.xaxis.set_major_locator(locator)
    ax.xaxis.set_major_formatter(ConciseDateFormatter(locator))

    for text in ax.get_xaxis().get_ticklabels():
        # print(text.get_text())
        if text.get_text().startswith('20'):
            text.set_fontsize(10)
        else:
            text.set_fontsize(5)

    ax.set_xlim(date(2016, 7, 1))

Actual outcome

ed619ab1-d916-4d40-ab27-61d8adedeee8

If you print the get_text() function, all the texts appear to be fine. However, for unknown reasons, certain texts are not picked by the set_fontsize() method.

Expected outcome

All the "20YY" will be set to fontsize 10

Additional information

No response

Operating system

OS/X

Matplotlib Version

3.7.1

Matplotlib Backend

No response

Python version

3.10

Jupyter version

No response

Installation

None

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

      0