I'm looking for a way to rename the months printed in the plot, that is, map each month to a custom label. I need my plots to be in a language that is not installed in my system, so I want to manually set the names.
If I try
locale.setlocale(locale.LC_TIME, 'pt_BR.UTF-8')
, I get Error: unsupported locale setting.
Asked
Active
Viewed 206 times
-1

Raphael
- 1,518
- 2
- 14
- 27
-
Also see [What is the correct way to set Python's locale on Windows?](https://stackoverflow.com/questions/955986/what-is-the-correct-way-to-set-pythons-locale-on-windows) – JohanC Jun 02 '21 at 16:50
-
1In that case, it would also be appreciated to have some short reproducible test code into the question. – JohanC Jun 02 '21 at 18:14
1 Answers
0
Ingredients:
FuncFormatter
num2date
Method:
from datetime import datetime, timedelta
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
from matplotlib.dates import num2date
from matplotlib.ticker import FuncFormatter
months_names = [None, 'Jan', 'Fev', 'Mar','Abr', 'Mai', 'Jun', 'Jul', 'Ago', 'Set', 'Out', 'Nov', 'Dez']
# Generate some random date-time data
numdays = 500
base = datetime.today()
date_list = [base - timedelta(days=x) for x in range(0, numdays)]
y = np.random.rand(numdays)
# Set the locator
locator = mdates.MonthLocator(bymonth=(1, 4)) # every January and April
plt.scatter(date_list,y)
X = plt.gca().xaxis
X.set_major_locator(locator)
X.set_major_formatter(FuncFormatter(lambda x, i: months_names[num2date(x).month]))
plt.show()
Output:

Raphael
- 1,518
- 2
- 14
- 27