Hi!
Sorry to revive an old topic, but Iāve been searching around the web for hours and could not find an appropriate solution.
First of all, the standard way to set the locale in Python is
import locale
locale.setlocale(locale.LC_ALL, "pt_BR.UTF-8")
This works for anything in the Python standard library, like the datetime module and others, as long as the locale you are trying to set is installed on your system. I have even written an article about it which further explains the details.
With Plotly, inside Jupyter Notebook / Jupyter Lab however, itās not so simple. Apparently, you need to pass the locale to the configuration options, such as this .show(config={"locale": "de"})
. But that doesnāt work on its own. You need also to include the localization script in the html. I found instructions on how to do that in Dash, but not in Jupyter Notebook.
For instance, the following code will create a chart with the weekdays in English, despite setting the locale differently:
from random import choice
from datetime import date
import plotly.express as px
(
px.line(
{
'data': [date(2021,6,20 + days) for days in range(7)],
'valor': [choice(range(10)) for val in range(7)]
},
x='data',
y='valor'
)
.update_xaxes(tickformat='%A')
.show(config={'locale': 'pt-BR'})
)
@empetās solution in the messages above involves converting the dates to strings in Python outside Plotly. Something like this:
(
px.line(
{
'data': [date(2021,6,20 + days).strftime('%A') for days in range(7)],
'valor': [choice(range(10)) for val in range(7)]
},
x='data',
y='valor'
)
#.update_xaxes(tickformat='%A')
#.show(config={'locale': 'pt-BR'})
)
It may seem to āworkā if your needs are very simple. However, if you try for instance to make any adjustments to the ticks, the fact that you are using strings instead of dates will make that impossible and break things. See this example that should display every other day on the x-axis:
(
px.line(
{
'data': [date(2021,6,20 + days).strftime('%A') for days in range(7)],
'valor': [choice(range(10)) for val in range(7)]
},
x='data',
y='valor'
)
.update_xaxes(dtick=86400000*2, tickformat='%A')
#.show(config={'locale': 'pt-BR'})
)
Because weāre using strings instead of real dates, Plotly canāt figure out the proper interval.
The proper way to do this should be by passing dates to Plotly and letting it format the dates using locale internally. This might work in HTML, but inside Jupyter Notebook the locale script is not loaded and we get it in English instead:
(
px.line(
{
'data': [date(2021,6,20 + days) for days in range(7)],
'valor': [choice(range(10)) for val in range(7)]
},
x='data',
y='valor'
)
.update_xaxes(dtick=86400000*2, tickformat='%A')
.show(config={'locale': 'pt-BR'})
)
Where 86400000 is the number of milliseconds in a day, as in the example in the docs. We see every other day (Monday, Wednesday, Friday) as expected there, however, the locale is not applied at all.
How can we make Plotly really apply the locale by its own, when using Jupyter Notebook / Jupyter Lab, instead of ācheatingā and converting the dates to strings before passing them on to Plotly?