With the code:
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import matplotlib.cbook as cbook
data = cbook.get_sample_data('goog.npz', np_load=True)['price_data'][0:200]
fig, ax = plt.subplots(figsize=(10, 7), constrained_layout=True)
ax.plot('date', 'adj_close', data=data)
# ax.xaxis.set_major_locator(mdates.MonthLocator())
# ax.grid(True)
ax.set_ylabel(r'Price [\$]')
ax.xaxis.set_major_formatter(
mdates.ConciseDateFormatter(ax.xaxis.get_major_locator()))
plt.savefig("chart.png")
And I need to change the months from English to Spanish, ie: Dec to Dic, Apr to Abr, Jan to Ene and so on.
I can do it by changing the locale like so:
locale.setlocale(locale.LC_TIME, 'es_ES')
But I can't use it in the script because it runs on a serverless vm where you can't change any of the os configuration.
So I thought in changing the labels "manually" from an English month to a Spanish one. I've seen examples using DateFormatter
but that doesn't work because again it relies in the system locale for the months names using strftime
and any other fromatter I've seen has been using numbers not dates. So is there any solution to localize the names of the months?
Update Added solution below