5

I've inserted a dataset in plotly in order to plot a time-series graph. Plotly converted it into a datetime object adequately but the display is in english and I need it to be in spanish. Can I do that? How?

Here's the code:

trace1 = Scatter(
  x=['2015-01-01', '2015-01-02'],
  y=['14.7414', '14.7414'],
  name='Precio del dlar',
  error_y=ErrorY(
    color='rgb(0,116,217)',
    thickness=1, width=1
  ),
  error_x=ErrorX(
    copy_ystyle=True
  ),
  marker=Marker(
    color='rgb(0,116,217)',
    size=8,
    symbol='circle'
  ),
  line=Line(
    color='rgb(0,116,217)',
    width=2
  ),
  xsrc='rafaeles:2294:78082d',
  ysrc='rafaeles:2294:7b41b5'
) 

The date format that I inserted is x. Plotly coded it as Jan 15, but I need that to be in spanish.

2
  • trace1 = Scatter( x=['2015-01-01', '2015-01-02'], y=['14.7414', '14.7414'], name='Precio del dlar', error_y=ErrorY( color='rgb(0,116,217)', thickness=1, width=1 ), error_x=ErrorX( copy_ystyle=True ), marker=Marker( color='rgb(0,116,217)', size=8, symbol='circle' ), line=Line( color='rgb(0,116,217)', width=2 ), xsrc='rafaeles:2294:78082d', ysrc='rafaeles:2294:7b41b5' ) The date format that I inserted is x. Plotly coded it as Jan 15 on, but I need that to be in spanish. Commented Sep 3, 2015 at 22:09
  • Edit into your question not as a comment, thanks Commented Sep 3, 2015 at 22:16

2 Answers 2

1

This is an old question but maybe it might be useful for someone with the same problem than Rafael and me. I had this problem and I asked to the Plotly Community about how I can change the date language and the answers has been really helpful.

Note that I edited your question to insert your code in a code block to ease the reading of it.

First of all try to check what is your current locale settings with the locale module of Python:

import locale
locale.getlocale()

If you get something different than the one you want to use, you can set the one you want for dates and time with locale.setlocale(). For example:

locale.setlocale(locale.LC_TIME, 'es_ES')

You can test this new locale works fine testing it with the datetime module as follows:

import datetime
today = datetime.datetime.now()

datetime.datetime(2020, 2, 14, 10, 33, 56, 487228)

today.strftime('%A %d de %B, %Y')

'viernes 14 de febrero, 2020'

Then you can try to reformat your data using datetime and strftime. In your case it might be something similar to:

dates = [datetime.datetime(2015, 1, 1).strftime('%A %d de %B, %Y'),
         datetime.datetime(2015, 1, 2).strftime('%A %d de %B, %Y')]

This works for me on iPython:

In [1]: import locale                                                                                                                                  

In [2]: locale.setlocale(locale.LC_TIME, "es_ES.utf8")                                                                                                 
Out[2]: 'es_ES.utf8'

In [3]: import datetime                                                                                                                                

In [4]: dates = [datetime.datetime(2015, 1, 1).strftime('%A %d de %B, %Y'), 
   ...:          datetime.datetime(2015, 1, 2).strftime('%A %d de %B, %Y')]                                                                            

In [5]: dates                                                                                                                                          
Out[5]: ['jueves 01 de enero, 2015', 'viernes 02 de enero, 2015']

Then if you want to use it in x axis, you can call dates inside x:

trace1 = Scatter(
  x=dates,
  y=['14.7414', '14.7414'],
  ...
)

It works for me! And I hope it works for you too.

If you need a different format for the date I recommend you to read the documentation about date-time microformats of D3. I also recommend you to read the thread How to change date language? I opened on Plotly Community Forum, because the answers are very useful and are the ones which helped me to solve my issue and to answer this question. You can also check this notebook shared in the thread, in which one of the users who answer me show how to solve the issue if you are using a pandas dataframe.

Excuse me for a lot of this stuff I am linking, but it might be very useful too to read about the methods to update axis in Plotly documentation, because you can update the format of the axis you want with as follows:

fig.update_xaxes(tickformat='%d-%b-%Y')

I want to credit to empet and eliasdabbas, users of the Plotly Community, for their answers!

Sign up to request clarification or add additional context in comments.

Comments

1

In some cases it does not really work to convert the time axis to a text label axis.
As of version 5.14, what you can do instead is to manipulate the axis labels generated by Plotly by using the labelalias property. You can see my other answer here for a tiny example how.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.