Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: inconsistent results when formatting ticklabels by get_text() in time axis #26791

Closed
stevenlis opened this issue Sep 15, 2023 · 2 comments

Comments

@stevenlis
Copy link

stevenlis commented Sep 15, 2023

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

@jklymak
Copy link
Member

jklymak commented Sep 15, 2023

You set the tick fontsize, but then change the tick values because you set_xlim. If you put the set_xlim before the tick manipulation it works fine.

If you don't want this to be brittle, set the major locator to years, and the minor locator to months 4, 7, 10, and change the size of the minor tick labels...

for ax, x, y in zip(axs, xs, ys):
    ax.plot(x, y)

    locator = YearLocator()
    ax.xaxis.set_major_locator(locator)
    ax.xaxis.set_major_formatter(ConciseDateFormatter(locator))

    locator = MonthLocator((4, 7, 10))
    ax.xaxis.set_minor_locator(locator)
    ax.xaxis.set_minor_formatter(ConciseDateFormatter(locator))
    ax.tick_params(axis='x', labelsize=7, which='minor')

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

I'm going to close this because there its relatively well understood that ticks need to be finalized before you reach into them and manually muck with them.

@jklymak jklymak closed this as not planned Won't fix, can't repro, duplicate, stale Sep 15, 2023
@stevenlis
Copy link
Author

Thank you @jklymak Could you pls take a look at this: SO. Does this also have something to do with the finalization of ticks?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants