0

I have a kind of specific question: I was plotting a time series that came from a dataframe with a DatetimeIndex index, which comes with pandas' specific formatting, like this:

enter image description here

The thing is, the date formatting (obviously) assumes english is my language. In this case, I need the month's legends to be in spanish, that is: change "Dec" for "Dic" and "Jan" for "Ene". I tried saving the ticks' values, modifying them and then replacing them, but I couldn't manage to accomplish the third part of my plan. This is what I tried:

from matplotlib.text import Text

df.plot()    
b = (plt.xticks())

b[1][2] = Text(2605, 0, '\n\nDic')
b[1][3] = Text(2610, 0, '\n\nEne\n2020')
ax.set_xticklabels(b)

So b was originally:

(array([2599, 2601, 2605, 2610], dtype=int64),
[Text(2599, 0, '20'), 
 Text(2601, 0, '\n\nNov'), 
 Text(2605, 0, '\n\nDec'),
 Text(2610, 0, '\n\nJan\n2020')])

And I changed it to:

(array([2599, 2601, 2605, 2610], dtype=int64),
 [Text(2599, 0, '20'),
  Text(2601, 0, '\n\nNov'),
  Text(2605, 0, '\n\nDic'),
  Text(2610, 0, '\n\nEne\n2020')])

But I don't know how to use this. Also, I feel there has to be a simpler way to do this. Do you have any recommendations?

Thanks !

Juan C
  • 5,846
  • 2
  • 17
  • 51
  • Perhaps [this answer](https://stackoverflow.com/questions/11244514/modify-tick-label-text) may help you. If not, could you provide us with a sample of data? So that we can replicate your exact situation. – Ralubrusto Oct 09 '20 at 22:07

1 Answers1

2

The easiest way is to change the locale only when necessary.

import locale
locale.setlocale(locale.LC_TIME, 'es_ES.UTF-8')

enter image description here

r-beginners
  • 31,170
  • 3
  • 14
  • 32