plotting data with too many zero on x axis - gnuplot

I have this data1.dat data that I'd plot using gnuplot.
x y
0.007 3.09216
0.008 3.60607
0.009 5.86643
then I tried to plot this with this plot script
set terminal svg size 400,300 enhanced fname 'arial'
set output 'out2.png'
set xlabel 'diameter'
set ylabel 'number density'
set title 'density number plot'
plot "data1.dat" using 3:4 title "" with lines
and the output in the x axis was like this
how to convert the number data on the x axis so it only shows only for example 7 or 8 only? this is an example and probably my data would getting bigger, so i can't change the data on data1.dat one by one in the x column, thanks in advance

Apparently, gnuplot's tic algorithm doesn't take the length of the ticlabels into account. That's why the numbers are overlapping.
You can do the following:
set the xtic increment manually to avoid overlap, e.g. set xtic 0.0005
multiply your values by a factor, e.g. 1000, especially if you have meters you can display millimeters, i.e. ($1*1000) which is identical to (column(1)*1000).
Code:
### avoid overlap of tic labels
reset session
$Data <<EOD
x y
0.007 3.09216
0.008 3.60607
0.009 5.86643
EOD
set ytics 1
set multiplot layout 3,1
set xlabel 'diameter / m'
plot $Data using 1:2 w l notitle
set xlabel 'diameter / m'
set xtic 0.0005
plot $Data using 1:2 w l notitle
set xlabel 'diameter / mm'
set xtics 0.2
plot $Data using ($1*1000):2 w l notitle
unset multiplot
### end of code
Result:

Related

Align ylabel in gnuplot multiplot

Is there a way to align vertically the y labels of in gnuplot's multiplot so that they are below each other? The problematic example is below (the red line is annotation showing the problem):
set xrange[0:10]
set multiplot layout 2,1 margins 0.1,0.95,0.1,0.95 spacing 0,0
set ylabel "y1\nlabel"
set ytics
set format y "%.2f"
plot -1000*cos(x)**2 w l lt 4 lw 2
set ylabel "y2\nlabel"
set format y "%.1f"
plot cos(x) w l lt 5 lw 2
unset multiplot
which generates:
And I would like to automatically position the labels such, that the red annotated line "touches the same text". Note that I am really interested in automatic way or more correct way that a workaround using a trial and error with set ylabel "lable" offset -x,0
As you already noted, you can set an offset to the x- or y-label (check help xlabel), however, no absolute position.
This you can do with setting your own label. You can set the position relative to the screen and/or relative to the graph. gnuplot keeps these values for the second plot, no need to specify again. Check help label.
Check the following example:
Code:
### align ylabels in multiplot
reset session
set xrange[0:10]
set multiplot layout 2,1 margins 0.15,0.95,0.1,0.95 spacing 0,0
set format x ""
unset ylabel
set label 1 "y1\nlabel" at screen 0.02, graph 0.5 center rotate by 90
set ytics 200
set ytics add ("" -1000) # remove -1000
set format y "%.2f"
set grid x,y
plot -1000*cos(x)**2 w l lt 4 lw 2
set format x
set label 1 "y2\nlabel"
set ytics 0.4
set format y "%.1f"
plot cos(x) w l lt 5 lw 2
unset multiplot
### end of code
Result:

How to display y-labels on top of histogram bars on gnuplot

I desgined a histogram in gnuplot however the y-scale needs to be in log2 due to huge difference in values. Therefore, to improve readability of the plot I pretend to display the concrete values on top of each bar. The values represent bytes and so I would like for this values also be in log2 and to be formated to display kb, Mb, ... as is being done in the y-axis.
How can I achieve this?
This is the comands I'm currently using:
set terminal postscript eps enhanced dash color "" 13
reset
set datafile separator ","
set title "Bytes per Protocol"
set xlabel "Protocol"
set ylabel "Bytes" rotate by 90
set yrange [0:1342177280]
set logscale y 2
set format y '%.0s%cB'
set style data histogram
set boxwidth 0.5
set style fill solid
set xtics format ""
set grid ytics
set style data histogram
set style histogram clustered gap 2
set grid ytics
set tic scale 0
set size 1,0.9
set size ratio 0.5
set key autotitle columnhead
set output "ex_a_1_BIG.eps"
plot "ex_a_1_BIG.csv" using ($3):xtic(1) title "IN", \
'' using ($5):xtic(1) title "OUT", \
'' using 0:($3):($3) with labels center offset -2,1 notitle, \
'' using 0:($5):($5) with labels center offset 2,1 notitle
This is the content of the csv I want to plot (I only want the bytes in and out):
protocol,packets in,bytes in,packets out,bytes out
ICMP,1833,141562,979,60334
IGMP,0,0,283,14006
TCP,158214,129221151,130101,47734355
UDP,68476,9571677,72530,24310734
Check help format_specifiers and help gprintf. And the example below.
What is a bit unfortunate, that in gnuplot apparently the prefix for 1 to 999 is a single space instead of an empty string.
For example, with the format '%.1s %cB' this leads to two spaces for 1-999 B and one space for the others, e.g. 1 kB. However, if you use '%.1s%cB' this leads to one space for 1-999 B and no space for the others e.g. 100kB. As far as I know, correct would be one space between the number and the units. I'm not sure whether there is an easy fix for this.
Code:
### prefixes
reset session
$Data <<EOD
1 1
2 12
3 123
4 1234
5 12345
6 123456
7 1234567
8 12345678
9 123456789
10 1234567890
11 12345678901
12 123456789012
13 1234567890123
EOD
set boxwidth 0.7
set style fill solid 1.0
set xtics 1
set yrange [0.5:8e13]
set multiplot layout 2,1
set logscale y # base of 10
set format y '%.0s %cB'
plot $Data u 1:2 w boxes lc rgb "green" notitle, \
'' u 1:2:(gprintf('%.1s %cB',$2)) w labels offset 0,1 not
set logscale y 2 # base of 2
set format y '%.0b %BB'
plot $Data u 1:2 w boxes lc rgb "red" notitle, \
'' u 1:2:(gprintf('%.1b %BB',$2)) w labels offset 0,1 not
unset multiplot
### end of code
Result:
Addition:
a workaround for number/unit space issue at least for the labels in the graph would be:
myFmt(c) = column(c)>=1 && column(c)<1000 ? \
gprintf('%.1s%cB',column(c)) : gprintf('%.1s %cB',column(c))
and
plot $Data u 1:2 w boxes lc rgb "green" notitle, \
'' u 1:2:(myFmt(2)) w labels offset 0,1 not
But for the ytics labels I still don't have an idea.

x axis on top of graph in gnu plot

I'm able to put an x axis on the top of a graph in gnu plot via
set x2label "label" and
set x2tics
I have a column of data of data1 values and I'd like to plot these values multiplied by two on the upper x axis against data data2 in the lower x axis. Both data1 and data2 are in the same data file. Here is some sample data
data2 data1
20 1.2e-2
40 3.0e-3
60 1.4e-3
... ...
I'd like to plot 2*data1 on upper axis against data2 on lower axis. Preferably, I'd like to just put a tick mark on the 2*data1 axis for every data2 value. On the y axis I will plot some other quantity against data2 but all I want to ask about here is how to plot x2 versus x1.
Thanks!
still guessing what exactly you want. Maybe we'll find out faster with this example.
reset session
$Data <<EOD
# x2 x1 y
20 1.2e-2 1
40 3.0e-3 2
60 1.4e-3 3
EOD
set key top center
set xtics nomirror
set x2tics nomirror
plot $Data u ($1*2):3 axes x2y1 w lp pt 7 title "y-data vs. x2-axis", \
'' u 2:3 axes x1y1 w lp pt 7 title "y-data vs. x1-axis"
Result:
Edit:
You can link x1 and x2 axes via a function (in the example below conversion between nm and eV) and then set your x2tics as desired.
Graph1: corresponding "odd" values from x1,
Graph2: "even" values by given interval x2tics 0.2,
Graph3: manual setting of x2tics.
Example:
### linked axes x1, x2
reset session
set xlabel "Wavelength / nm"
set xtics nomirror
set x2label "Energy / eV"
set x2tics nomirror
set link x via 1239.8/x inverse 1239.8/x
set ylabel "Intensity / a.u."
set ytics 0.2
set samples 400
Spectrum(x) = exp(-(x-500)**2/(400))
set xrange[380:780]
set multiplot layout 3,1
set format x2 "%.2f"
plot Spectrum(x) w l title "Spectrum"
set format x2 "%.1f"
set x2tics 0.2
replot
set x2tics ()
myTics = "1.7 1.8 1.9 2.0 2.1 2.3 2.5 2.7 3.0"
do for [i=1:words(myTics)] { set x2tics add (word(myTics,i) real(word(myTics,i))) }
replot
unset multiplot
### end of code
Result:

Fitting a normalized histogram using gnuplot

I have a datafile containing N random numbers generated from a C-code. Now I want to normalize the histogram from this datafile and, then, fit it to a given distribution function. How can I do that?
This is my gnuplot code for the histogram plot:
width = 5000
hist(x,width)=width*floor(x/width)+width/2.0
set boxwidth width
set style fill solid 0.5
set xrange [0:500000]
set yrange [0:20]
plot "out.dat" u (hist($1,width)):(1.0) smooth freq w boxes lc rgb"green"
Since gnuplot version 5.2 there is an new smoothing type smooth fnormal which does exactly that: sum up all values with same x-value and normalize the data so that the overall sum is 1.
A simple example:
set boxwidth 0.9
set style fill solid 0.5
set yrange [0:*]
$data <<EOD
1
1
2
2
2
3
3
EOD
set style data boxes
plot $data u 1:(1) smooth freq title 'smooth frequency',\
'' u 1:(1) smooth fnormal title 'smooth fnormal'
Applied to you example you must only update the actual plotting line to
plot "out.dat" u (hist($1,width)):(1.0/(sum)) smooth fnormal w boxes lc rgb "green"

gnuplot: string values xticlabel & adjusting fontsize

I have data I would like to plot in a histogram style with a "cumulated" curve on top. I have the following problem:
My data consists of one column with the categories ("discharge") and one column with the quantity of values ("probability") that belong to the respective category. The last value of the category-column is ">100" summarizing all power plants that have a bigger discharge than the last numeric value ("100 m^3/s"). I have not found a solution to plot this last category and the respective values with the command plot 'datafile.dat' using 1:2 with boxes ... because (as I assume) in this case only numerical values are read out for the x-ticlabels, so the last category is missing. If
I plot it with this command plot 'datafile.dat' using 2:xtics(1) with boxes ... I get the last category ">100" plotted just fine.
BUT: if I use the latter command the x-axis labels appear in the normal font size. Even though I have the line set format x '\footnotesize \%10.0f' in my code.
I have read about explicit labels in the plotcommand line that overwrite format style which was set before but was not able to adapt it to my code.
Changing ytic font size in gnuplot epslatex (multiplot)
Do you have an idea how to do this?
Excel screenshot to visualize what I want to achieve
'datafile.dat'
discharge probability cumulated
10 20 20%
20 10 10%
30 5 5%
40 6 6%
50 4 4%
60 12 12%
70 8 8%
80 15 15%
90 20 20%
100 6 6%
>100 4 4%`
[terminal=epslatex,terminaloptions={size 15cm, 8cm font ",10"}]
set xrange [*:*]
set yrange [0:20]
set y2range [0:100]
set xlabel 'Discharge$' offset 0,-1
set ylabel 'No. of power plants' offset 10.5
set y2label 'Cumulated probability' offset -10
set format xy '$\%g$'
set format x '\footnotesize \%10.0f'
set format y '\footnotesize \%10.0f'
set format y2 '\footnotesize \%10.0f'
set xtics rotate by 45 center offset 0,-1
set style fill pattern border -1
set boxwidth 0.3 relative
set style line 1 lt 1 lc rgb 'black' lw 2 pt 6 ps 1 dt 2
plot 'datafile.dat' using 1:2 with boxes axes x1y1 fs pattern 6 lc black notitle, \
'datafile.dat' using 1:3 with linespoints axes x1y2 ls 1 notitle
I am confused by your datafile; the numbers in the third column do not seem to be cumulative, and do not add up to 100%. Here is a solution that uses only the first two columns of your file:
set term epslatex standalone header "\\usepackage[T1]{fontenc}"
set output 'test.tex'
stats "datafile.dat" using 2
total = STATS_sum
set xlabel "Discharge" offset 0, 1.5
set xtics rotate
set ylabel "No. of power plants"
set ytics nomirror
set yrange [0:*]
set y2label "Cumulative probability"
set y2tics
set y2range [0:]
set boxwidth 0.3 relative
set style line 1 lt 1 lc rgb 'black' lw 2 pt 6 ps 1 dt 2
plot \
'datafile.dat' using 2:xtic("\\footnotesize " . stringcolumn(1)) with boxes axes x1y1 fs pattern 6 lc black notitle, \
'datafile.dat' using ($2/total) smooth cumulative with linespoints axes x1y2 ls 1 notitle
set output
The trick is to add the latex command \footnotesize in front of each label in the using command. It also first computes the total number of power plants so that it can compute probabilities, and computes cumulative values with the smooth cumulative option.

Resources