7
$\begingroup$

In this plot example, I get the months in English abbreviation:

DateListPlot[{#, RandomReal[]}&/@DateRange[{2020,1,1}, {2021,01,31}, "Month"]]

enter image description here How can I translate the abbreviated names using this rule:

monthNameRule = <|"Jan"-> "Jan", "Feb"-> "Fev", "Mar"-> "Mar", "Apr"-> "Abr", "May"-> "Mai", "Jun"-> "Jun", "Jul"-> "Jul", "Ago"-> "Ago", "Sep"-> "Set", "Oct"-> "Out", "Nov"-> "Nov", "Dec"-> "Dez"|>

Maybe there is some Global Variable with Month names? Any Clue?

I'm know I can use Ticks to specify manually, but I would like to use Mathematica automatic magic for ticker position, and just translate the result.

$\endgroup$
9
$\begingroup$

Update: A generalization to translate months and days:

ClearAll[translateDates, translatedDateTicks]
translateDates[f_: (Capitalize[StringTake[#, 3]] &)] := StringReplace[
  CanonicalName[#] | Capitalize[StringTake[CanonicalName[#], 3]] -> f[#2] & @@@ 
    Join @@ LanguageData[#, {"MonthNames", "Days"}]] &;

translatedDateTicks[f_: (Capitalize[StringTake[#, 3]] &)][l_String, 
  n_: All, ds_: "MonthName"] :=  Replace[System`DateListPlotDump`DateTicks[{##}, n, ds], 
    s_String :> translateDates[f][l]@s, All] &;

Examples:

data = {#, RandomReal[]} & /@ DateRange[{2020, 1, 1}, {2020, 02, 15}, "Day"];

DateListPlot[data, ImageSize -> 800, LabelStyle -> 12, 
 FrameTicks -> {{Automatic, All},
  {translatedDateTicks[]["Portuguese", 30, "DayName"], 
   translatedDateTicks[]["Spanish", 30, 
     {"DayName", "\n", "MonthName", "\n", "YearShort"}]}}]

enter image description here

data2 = {#, RandomReal[]} & /@ DateRange[{2020, 1, 1}, {2021, 01, 31}, "Month"];

DateListPlot[data2, ImageSize -> 800, LabelStyle -> 12, 
 FrameTicks -> {{Automatic, All}, 
  {translatedDateTicks[Identity]["Chinese", 15, "MonthName"], 
   translatedDateTicks[Identity]["Russian", 10, {"MonthName", " / ", "YearShort"}]}}]

enter image description here

Original answer:

ClearAll[monthShort, dTicks]
monthShort[lang_] := CanonicalName[#] -> Capitalize[StringTake[#2, 3]] & @@@ 
  LanguageData[lang, "MonthNames"]

dTicks[l_String, n_:All] := System`DateListPlotDump`DateTicks[{##}, n, "MonthName"] /. 
   monthShort[l] &;

dTicks[a_Association, n_: All] := System`DateListPlotDump`DateTicks[{##}, n, 
   "MonthNameShort"] /. a &;

Example:

monthNameRule = <|"Jan" -> "Jan", "Feb" -> "Fev", "Mar" -> "Mar", 
   "Apr" -> "Abr", "May" -> "Mai", "Jun" -> "Jun", "Jul" -> "Jul", 
   "Aug" -> "Ago", "Sep" -> "Set", "Oct" -> "Out", "Nov" -> "Nov", "Dec" -> "Dez"|>;

DateListPlot[{#, RandomReal[]} & /@ DateRange[{2020, 1, 1}, {2021, 01, 31}, "Month"], 
 ImageSize -> Large, LabelStyle -> 16,
 FrameTicks -> {{Automatic, All}, {dTicks[monthNameRule, 10], dTicks["Portuguese"]}}]

enter image description here

| improve this answer | |
$\endgroup$
  • $\begingroup$ Interesting. I'll wait 24h before accept. Tks a lot. $\endgroup$ – Murta Mar 30 at 11:26
  • $\begingroup$ what version are you using? If I copy and past your answer in V12.1, I get error, and no translation. $\endgroup$ – Murta May 9 at 21:52
  • $\begingroup$ I am using version 11.3 (windows 10 -64b) $\endgroup$ – kglr May 9 at 22:26
1
$\begingroup$

A simple answer:

DateListPlot[{#, RandomReal[]} & /@ 
  DateRange[{2020, 1, 1}, {2021, 01, 31}, "Month"], 
 FrameTicks -> {{Automatic, 
    Automatic}, {Table[{{2020, Month[[1]], 1}, 
      Month[[2]]}, {Month, {Range[12], {"Jan", "Fev", "Mar", "Abr", 
         "Mai", "Jun", "Jul", "Ago", "Set", "Out", "Nov", 
         "Dez"}}\[Transpose]}], None}}]

But this is done by manual setting, which may not meet your requirements. I would also like to know how to automatically convert the date format to Portuguese and other language formats.

| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.