Gnuplot interpretes locale decimal separator only in the first data line - gnuplot

My data file is
1,1
1,2
1,3
I try to plot it with the following script
set terminal pngcairo size 600,300 enhanced font "Calibri,18"
set output 'demo_err.png'
set xr [-1:3]
set yr [-1:+1]
#decimal separator in Russia is comma
set decimalsign locale "Russian_Russia.1251"
plot "demo_err.csv" u ($0):(0):(sprintf("%.1f", $1)) w labels,\
Gnuplot result
As you see, first label is OK, but in second and third label fraction is shown as 0. What am I doing wrong?
Gnuplot version is 5.2.6

In case you can't wait until the bug is fixed and #Ethan's stringcolumn(1) suggestion is not enough, because you "absolutely" need e.g. the format "%.3f", you can mimic sprintf("%.3f",x) with a formula. Well, with the drawback... it is not rounding the decimals.
### sprintf("%.nf",x) "replacement" with decimalsign locale ','
reset session
$Data <<EOD
1
-2,34567
123,45
5,6
7,8867
EOD
set locale "Russian_Russia.1251"
set decimalsign locale "Russian_Russia.1251"
f(s,n) = ((s).',0000000'[sgn(strstrt(s,','))+1:])[1:(strstrt(s,',')==0 ? strlen(s)+n+1 : strstrt(s,',')+n)]
set xrange[0.5:1.5]
set yrange[-0.5:5]
n = 3
plot $Data u (1):0:(f(stringcolumn(1),n)) w labels
### end of code
Result:

"set decimalsign" controls only the output, not the input.
Input is controlled by "set locale". I can't test this because I don't have your locale installed, but it should work to do
set locale "Russian_Russia.1251"
set decimalsign locale "Russian_Russia.1251"
plot "demo_err.csv" u ($0):(0):(sprintf("%.1f", $1)) w labels
[Edit] I have now reproduced the problem using a different locale. What is happening is that each time sprintf is called it sets the locale on entry and resets it to "C" on exit. If sprintf appears on a gnuplot command line this is exactly what you want to happen. But if it is called during evaluation of a 'using' specification this clobbers the state of the next data input read operation. A work-around for your simple test case is to avoid the use of sprintf in the 'using' spec:
plot "demo_err.csv" u ($0):(0):(stringcolumn(1)) w labels
I do not know if an equivalent work-around is possible for your full use case by avoiding sprintf inside the "using" clause of the plot command. Meanwhile I will file a bug report on the gnuplot issue tracker.

Related

Saving gretl data in a .txt to plot with gnuplot causes problems due to scientific notation. How can I circumvent that? [duplicate]

I want to plot some stockpile data.
The data is located in a csv-file and I already got an almost accurate plot, so reading from file isn't the problem.
set terminal pdf
set output "gnuplot/".MATNUM."-2012.pdf"
set datafile separator ";"
stats 'Stock-2012.csv' every ::4::18 using MATNUM nooutput
set border 3
set tics nomirror
set xzeroaxis
set xrange[0:14]
set xtics 1,1,13
set xtics rotate 90
set ylabel MATUNIT
maxplot = sprintf("Amount max:\n%.2f ".MATUNIT, STATS_max)
plot 'Stock-2012.csv' every ::4::18 using ($0+1):MATNUM:xticlabels(2) with linespoints title "Amount", STATS_max with lines lc rgb 'blue' title maxplot
Where MATNUM and MATUNIT are commandline arguments, representing materialnumber (which is the columntitle in the datafile) and the unit in which the material is measured.
The x-values in my datafile are decimals, but gnuplot seems to cut off the fractional digits. For example 12,98 (commata are used as decimal separator, because it's a german stockpile) results in a datapoint at y=12.
I'm not sure but I think this happens only at the maximum and minimum y-value, as STATS_max is an integer every time.
What can I do to get my points at the right y-value?
Unfortunately you did not show any data from you file, but I guess you are using commas as decimal separators. In that case you need to set the right decimal sign for reading the input. You can use e.g.
set decimalsign locale
or use an explicit locale (this must be installed)
set decimalsign locale "de_DE.UTF-8"
Note, that setting an explicit character with set decimalsign ',' does not work because it usually affects only the output format of e.g. tics, but not the input behaviour.

gnuplot - USING string variables

I'm trying something very simple ...
#!/usr/bin/gnuplot
reset
filename = "something_or_other"
set terminal pngcairo dashed size 800,400 enhanced font 'Verdana,10'
set output filename.".png"
set title filename."\n"
set xlabel "probably time"
set ylabel "probably something else" offset graph 0.2,0.6 rotate by 0
plot filename.".dat" using 1:2 lc rgb "#00E80000" title "measurements"
!display filename.".png"
except filename stays just like that and doesn't get interpreted as a string.
How do I get this working?
The exlamation mark ! at the beginning of the line makes gnuplot send this whole line as it is to a shell. So, here you cannot use any gnuplot variables and string concatenation. For this you can use system():
system(sprintf("display %s.png", filename))

GnuPlot: stacked histogram causes hovering bars

since two days I am trying to solve this problem. The bars of this stacked histogram are not printed above each other. They are floating freely around.
Secondly, I only want to print any 5th xtic-label. I am using GnuPlot v 4.6 patchlevel 6.hovering bars in stacked bargraph
Here are the first data rows (generated with libreoffice):
05.06,-,-,1
06.06,3,-,0
07.06,12,-,3
08.06,0,5,4
09.06,7,2,0
10.06,86,2,1
11.06,31,4,1
12.06,17,1,0
01.07,1,7,1
Here comes the command set:
gnuplot> set datafile separator ','
gnuplot> set style data histogram
gnuplot> set style histogram rowstacked
gnuplot> set style fill solid border -1
gnuplot> set xlabel "Zeit"
gnuplot> set ylabel "Anzahl"
gnuplot> set yrange [0:250]
gnuplot> plot 'test.csv' using 2:xtic(1) title "Menge A",''
gnuplot> using 3:xtic(1) title "Menge B",''
gnuplot> using 4:xtic(1) title "Menge C"
Gnuplot seems to get confused with - as only column content. Also a set datafile missing '-' doesn't help. You need a datafile with really empty fields, like
05.06,,,1
06.06,3,,0
07.06,12,,3
If you cannot get LibreOffice to save the data file properly you can use e.g. sed to process the file on-the-fly:
plot "< sed 's/-//g' test.csv" using 2:xtic(1), '' ...
(This works properly if you don't have negative values, which I suppose is the case).
To the second part: Instead of xtic(1) you can also put any expression which evaluates to a string inside of xtic, like
xtic(int($0)%5 == 0 ? strcol(1) : '')
This uses the string in the first column as xticlabel if the row number is a multiple of 5, otherwise an empty string:
set datafile separator ','
set style data histogram
set style histogram rowstacked
set style fill solid border -1
set xlabel "Zeit"
set ylabel "Anzahl"
set yrange [0:*]
plot '< sed "s/-//g" test.csv' using 2:xtic(int($0)%5 == 1 ? strcol(1) : '') title "Menge A",\
'' using 3 title "Menge B",\
'' using 4 title "Menge C"
As Christoph has already explained, the problem is caused by the - in your input data.
Therefore, the best and cleanest solution is to make LibreOffice display missing data differently.
However, everything worked fine for me when I mask the using COLUMNNUMBER part by using $COLUMNNUMBER. Hence, I changed the last line of your code to
plot 'test.csv' u ($2):xtic(1) t "Menge A", '' u ($3) t "Menge B", \
'' u ($4) t "Menge C"
As you see, you can shorten using to u and title to t. Moreover, you should use :xtic(1) only for the first data set.
Here is my outoput

gnuplot script fails - has there been a change in defaults from 4.4 to 4.6

The following script to plot data from my wind turbine worked fine in version 4.4 but now fails with
"plots", line 14: could not find column with header "11"
Something seems to have changed with variable substitution but I can't find it documented.
# data format
# 13-02-13 13:42:00 E:0 L:0 S:1 F:9 D:44 T:20.91 C:997 V:28.02 A:2.85 P:79 R:219 r:292 H:223 Y:1057 h:-225 y:-1228 I:5249 O:4921
# command line
# gnuplot -e "filename='log-130213.txt'; f1='11'; t1='Volts'; f2='14'; t2='RPM'" plots
set title filename
set datafile separator ":"
set xdata time
set timefmt "%d-%m-%y %H:%M:%S"
set format x "%H:%M"
set xlabel "Time"
set ylabel t1
set y2label t2
set y2tics
plot filename using 1:f1 with line title t1 axis x1y1 , "" u 1:f2 with line title t2 axis x1y2
pause -1
Anyone any ideas?
Answering my own question (after lots of hair pulling)... Changing the command line to NOT quote the numeric value of the column required corrects the problem. With the quotes, gnuplot looks for a column header containing '11' or '14' (in my case!!) with no way to turn this facility off!
gnuplot -e "filename='log-130213.txt'; f1=11; t1='Volts'; f2=14; t2='RPM'" plots
Note: This is a change in behaviour from version 4.4 to version 4.6
The problem seems to be that 4.6 is stricter than 4.4 with Var Types.
You can, just before the 'plot' command force the string variable to become a numeric variable by doing
var=var*1 or within the using command with 'using x*1:y*1:label*1'
This saves messing with shell scripting and puts it near to where it is required for programming clarity

GNUPlot x-axe as String

Is ist possible to plot with strings as x-axe?
I have the following file:
cw15 0,577774
cw255 0,901639
cw1023 0,927813
i tired following:
set datafile separator ","
set yrange [0.0000:1.00000]
plot 'meanFrameDropRatio.data' u 2:xticlabel(1) smooth unique ti 'PPS 20 (Tx-Power = 200mW)'
but i just get nothing, any ideas? I'm using GNUPlot 4.6
ok, i found the solution. The problem was that GNUPlot Punkt instead of Komma used.
I had to add
set decimalsign locale "de_DE.UTF-8"

Resources