Menu

#281 Change Thousands Separator for Displaying (and not for Data Reading)

None
accepted
nobody
None
5
2023-02-01
2022-04-07
No

To format my output, I would like to add thousand separators to my x and y axis. My data are 'regular' english format (e.g. 3.141 is approximately pi and not three thousand, so "." is the decimal separator) , but for my graph, I would like to use german formatting.

It is possible to change the formatting using

set decimalsign locale 'de_DE.utf8'
set decimalsign "."
set format "%'.0f"

but this results in the values being read wrong (e.g. 3.141 is just read as 3, since the german locale with "," as decimal separator is used).

Using

set decimalsign locale 'en_GB.utf8'
set decimalsign "."
set format "%'.0f"

fixes the wrong values, but uses "," as thousand separator (instead ".").

Is it possible to keep the data clean and just tell gnuplot to use a different thousand separator for displaying?

Discussion

  • Ethan Merritt

    Ethan Merritt - 2022-04-07

    If I understand correctly, the only command needed is set decimalsign ",", although it is possible there is further interaction with the locale settings of the machine you run it on. For me, this produces the attached figure:

    $DATA << EOD
    0.1
    0.2
    0.3
    0.4
    0.5
    1.1
    1.2
    1.3
    1.4
    1.5
    EOD
    
    set decimalsign ","
    set yrange [0:2]
    set ytics 0.1
    set tics format "%.1f"
    plot $DATA using 0:1 with points
    
     
  • David Georg Reichelt

    Thanks for the quick reply.

    This works for the decimalsign, but not for the thousand separator.

    If I change your example to

    $DATA << EOD
    1000.1
    1000.2
    1000.3
    1000.4
    1000.5
    EOD
    
    set decimalsign ","
    set yrange [999.5:1001]
    #set ytics 0.1
    set tics format "%.1f"
    plot $DATA using 0:1 with points
    

    than the thousand separator is not displayed correctly (it should be 1.000,0, but is 1000,0, example.png).

    If I now change the locale and decimalsign, the x axis is correct, but the values are wrong:

    $DATA << EOD
    1000.1
    1000.2
    1000.3
    1000.4
    1000.5
    EOD
    
    set decimalsign locale 'de_DE.utf8'
    set decimalsign "."
    set format "%'.0f"
    set yrange [999.5:1001]
    plot $DATA using 0:1 with points
    

    (example_wrongValues.png).

    Do you see any option to get the thousand separator working correctly and preserving the reading of the values with dot as decimal separator?

     

    Last edit: David Georg Reichelt 2022-04-07
  • Ethan Merritt

    Ethan Merritt - 2022-04-07

    No, sorry.
    Gnuplot's internal formatting routine does not know anything about the locale properties LC_NUMERIC:thousands_sep or LC_NUMERIC:grouping. It does not implement anything equivalent to the C language (POSIX 2008, SUSv3) single-quote format flag character.

    I'm not sure exactly what happens when you use it anyhow, as in your set format command. It appears that the properties of the current locale setting bleed through. I suppose that is a bug, since it is clearly incorrect to have both the thousands separator and the decimal point end up being the same character.

    Work-around:
    You can do this in two stages. First read in the data and save it to a temporary data block that converts the decimal point character. Second, switch fully to the German locale and plot from the temporary copy of the data. I don't have German locales installed here, but I think this should work the same way:

    $DATA << EOD
    999.1
    999.2
    999.3
    999.4
    999.5
    1000.1
    1000.2
    1000.3
    1000.4
    1000.5
    EOD
    
    set yrange [999:1002]
    set ytics 0.1
    
    # Use decimal point . on input from $DATA but , on output to $TEMP
    set decimal locale "en_US.UTF-8"
    set decimalsign ","
    set table $TEMP
    plot $DATA using 0:1 with points
    unset table
    
    # Switch to locale with decimal , and thousands separator .
    unset decimalsign
    set decimalsign locale 'fr_BE.UTF-8'
    set format "%'.1f"
    plot $TEMP using 1:2 with points
    
     
  • David Georg Reichelt

    Thanks, your workaround works fine when having the data directly in the script.

    Is it also possible to import the data somehow, and plot them later? If I use a plot command on a CSV file, loading and plotting happens at the same time from my point of view. One workaround would be to write the data directly into the script, but that would be equally cumbersome as transforming the data to my locale.

    It seems like the load command (http://gnuplot.sourceforge.net/docs_4.2/node97.html) could do this somehow, but $DATA << load 'myData.csv' did not work and I did not find other documentation on the usage of the load command.

     
    • Ethan Merritt

      Ethan Merritt - 2022-04-08

      The temporary table is created by set table $TEMP; plot ...; unset table. The data source for the plot command was included in the script for the purpose of showing an example, but it could equally well be a csv file.

       
      • David Georg Reichelt

        Thanks for the hint, but unfortunately, I did not manage to get this running.

        If I've got a data file that contains

        13.1
        13.5
        13.8
        

        and a plot script

        set out 'test.pdf'
        set encoding iso_8859_1
        set terminal pdf size 8,10
        
        set table $TEMP
        plot 'data.csv' u 0:1
        unset table
        
        set decimalsign locale 'de_DE.utf8'
        set format "%'.0f"
        
        plot $TEMP w linespoint
        

        than this script will only plot a horizontal line (test1.pdf). If I leave out the set decimalsign command, the graph is plotted as expected (test2.pdf).

        It seems like gnuplot even does something with the values after the data are loaded. Is there maybe a workaround for this?

         
        • Ethan Merritt

          Ethan Merritt - 2022-04-09

          You left out a command. The idea of the first step is to change the decimal character from period (as it is in data.csv) to comma (to be stored in $TEMP). So you need a command set decimalsign ',' prior to filling $TEMP.

           
  • Tatsuro MATSUOKA

    The load commnad is for loading a sciprt file but not data.
    Use just "plot datafile" but not "plot $datablock".
    For CSV file, data separator should set to comma
    Use "set datafile separator commna".
    For detail
    gnuplot> help set datafile separator

     

    Last edit: Tatsuro MATSUOKA 2022-04-08
  • David Georg Reichelt

    @tmacchant3 Unfortunately, as explained earlier, just using plot datafile is not possible since the data contain dot as decimal separator (so their locale is suitable for en_GB.utf8), but the axis should contain dot as thousand separator and comma as decimal separator (like in de_DE.utf8). Since this cannot be achieved in one step, loading the data and plotting them needs to be split.

     
  • Ethan Merritt

    Ethan Merritt - 2023-02-01

    Ticket moved from /p/gnuplot/bugs/2517/

    Can't be converted:

    • _milestone:
    • _priority:
     
  • Ethan Merritt

    Ethan Merritt - 2023-02-01
    • status: open --> accepted
    • Group: -->
    • Priority: --> 5
     

Log in to post a comment.