Closed as not planned
Closed as not planned
Copy link
Description
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
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