gnuplot plotting multiple errorbars depending on first column - gnuplot

I have a data file with the following format:
col1: plot[0-9]+
col2: x
col3: y
col4: delta
For example:
plot0, 1, 1, 2
plot1, 1, 2, 2
plot0, 2, 2, 1
plot1, 2, 3, 2
I am trying to plot a yerrorbars for each plotX of the first column with separated legend and color.
A none scalable example would be:
plot 'ex.dat' using 1:2:3 with yerrorbars, 'ex1.dat' using 1:2:3 with yerrorbars

Maybe something like this? The color of the datapoint+yerrorbar is taken from the first column via the function GetPlotNo(n) = int(strcol(n)[5:]).
The second plot command is actually plotting a dummy outside of the range just to get the colors of the legend right. If you need the datapoints of each plot1, plot2, ... to be connected, this might get a bit more complicated.
Code:
### color of datapoints depending on a column
reset session
set colorsequence classic
$Data <<EOD
plot0, 1.0, 1, 2
plot1, 1.1, 2, 2
plot0, 2.0, 2, 1
plot1, 2.1, 3, 2
plot2, 1.2, 3, 2
plot0, 3.0, 3, 1
plot1, 3.1, 4, 1
plot2, 2.2, 5, 1
plot2, 3.2, 4, 1
EOD
set datafile separator ","
set xrange[0:4]
set yrange[-1:7]
GetPlotNo(n) = int(strcol(n)[5:])
plot \
$Data u 2:3:4:(GetPlotNo(1)+1) w yerrorbars pt 7 lc var lw 2 notitle, \
for [i=0:2] -999 w lp pt 7 lc i+1 lw 2 title sprintf("plot%i",i) noautoscale
### end of code
Result:
Addition:
Actually, if you want the lines to be connected it requires a small modification. Add/exchange the following lines and the lines of each plot will be connected.
set datafile missing NaN
plot \
$Data u 2:3:4:(GetPlotNo(1)+1) w yerrorbars pt 7 lc var lw 2 notitle, \
for [i=0:2] '' u (i==GetPlotNo(1)?$2:NaN):3:(GetPlotNo(1)+1) w lp pt 7 lc var lw 2 title sprintf("plot%i",i)

Related

gnuplot: transform axis of matrix plot with "every"

I have a problem with plotting matrices with gnuplot. I am plotting one row of matrix with every option like that
plot inputfile matrix every 1:1:(4+N*M+1):100:(4+N*(M+1)):100 with linespoint
where 100 is number of row. It gave me that result:
nearly good result
I would like to get xrange from 0 to 360, but when I use something like that
plot inputfile matrix using ($1*11.25):2 every 1:1:(4+N*M+1):100:(4+N*(M+1)):100 with linespoint
it doesen't work: wrong result
What can I do with it?
You don't provide data, so I create some for the following example.
As I understand you want to plot a certain row of a matrix and adjust the x-range.
Check help matrix every.
For example, in plot FILE u 1:2:3 matrix, 1 is the column, 2 the row and 3 is the (z)-value.
And in plot FILE u 1:3 matrix every ::c:r:c:r, c is the column and r is the row (counting starts from 0).
So the example below plots the 4th row and the x-range is adjusted from 0 to 360.
Code:
### plotting a certain row while adjusting the x-range
reset session
$Data <<EOD
1 2 3 4 5 6 7
2 3 4 5 6 7 8
3 4 5 6 7 8 9
4 5 6 7 8 9 10
5 6 7 8 9 10 11
EOD
set key top left
plot $Data u ($1*60):3 matrix every :::3::3 w lp pt 7
### end of code
Result:

How to plot energy diagram using gnuplot

I have data file:
0 0 3 -0.17 6 -0.05
0 0 3 -0.23 6 0.90
0 0 3 -0.41 6 0.50
0 0 3 -0.50 6 -0.33
0 0 3 -0.20 6 0.80
I want to plot the figure like this which connects each point in the lines. Can you tell me how?
The following suggestion uses the plotting styles with boxxyerror, with vectors and with labels.
The zero level gets the color of the last entry because everything is plotted on top of each other. Check the following example as starting point for further tweaking.
Script:
### energy diagram
reset session
$Data <<EOD
0 0 3 -0.17 6 -0.05 A_{one}
0 0 3 -0.23 6 0.90 B_{two}
0 0 3 -0.41 6 0.50 C_{three}
0 0 3 -0.50 6 -0.33 D_{four}
0 0 3 -0.20 6 0.80 E_{five}
EOD
set key noautotitle
set xrange [-1:8]
myWidth = 0.3
plot for [i=1:3] $Data u (column(2*i-1)):(column(i*2)):(myWidth):(0):0:xtic(i*2-1) w boxxy lw 3 lc var, \
for [i=1:2] '' u (column(2*i-1)+myWidth):(column(i*2)): \
(column(2*i+1)-column(2*i-1)-2*myWidth):(column(i*2+2)-column(i*2)):0 w vec nohead lw 1 dt 3 lc var, \
'' u 5:6:7:0 w labels offset 6,0 tc var font ",16"
### end of script
Result:
Addition:
From your comments: If the levels are to close such that the labels overlap, you could add an individual offset (here in column 8).
Check the following example where the data is modified that the levels B and E are very close.
Script:
Edit after OP's comment: simplifications, with xerrorbar instead of with boxxyerror and with custom xtics
Edit 2: simplified input data
reduce the input data to the minimum
add xticlabel from columnheader
use column number as x-coordinate. Mind the difference in ...$Data u (col):col:...: e.g. if col=1, (col) is the fixed value of 1, and col is the value from column 1.
### energy diagram with individual offset of labels
reset session
$Data <<EOD
A B C Label Offset
0.0 -0.17 -0.05 A_{one} 0
0.0 -0.23 0.90 B_{two} 0.05
0.0 -0.41 0.50 C_{three} 0
0.0 -0.50 -0.33 D_{four} 0
0.0 -0.20 0.85 E_{five} -0.05
EOD
set key noautotitle
set errorbars 0
set offset 0.5,0.5,0,0
myWidth = 0.1
plot for [col=1:3] $Data u (col):col:(myWidth):0:xtic(columnhead(col)) w xerr lw 3 ps 0 lc var, \
for [col=1:2] '' u (col+myWidth):col:(1-2*myWidth):(column(col+1)-column(col)):0 \
w vec nohead lw 1 dt 3 lc var, \
'' u (3+myWidth):($3+$5):4:0 w labels left offset 1,0 tc var font ",16"
### end of script
Result:

colorsequence for more than 8 colors gnuplot

I normally used set colorsequence podo in gnuplot 5 to choose colors that are friendly to color blind individual.However my plot consist of 12 different keys and thus the line colors will repeat.How do i extend the colorspace to 12 colors from the default 8 while still taking care of color blindness and not having to specify the colors manually as far as possible.
Extending the colorspace can be done with a initialization file. From help set linetype:
The recommended way to do this is to add to the run-time
initialization file ~/.gnuplot a sequence of commands like
if ((GPVAL_VERSION < 4.5) \
|| (!strstrt(GPVAL_COMPILE_OPTIONS,"+USER_LINETYPES"))) \
exit
set linetype 1 lc rgb "dark-violet" lw 2 pt 0
set linetype 2 lc rgb "sea-green" lw 2 pt 7
set linetype 3 lc rgb "cyan" lw 2 pt 6 pi -1
set linetype 4 lc rgb "dark-red" lw 2 pt 5 pi -1
set linetype 5 lc rgb "blue" lw 2 pt 8
set linetype 6 lc rgb "dark-orange" lw 2 pt 3
set linetype 7 lc rgb "black" lw 2 pt 11
set linetype 8 lc rgb "goldenrod" lw 2
set linetype cycle 8
Every time you run gnuplot the line types will be initialized to
these values. You may initialize as many linetypes as you like.
For colors which are friendly to colorblind people, help colorsequence refers to an article from Wong (2011) [Nature Methods 8:441]. I don't have access to the article, but it seems that they have some images from this article online. And it seems that this article recommends only 8 colors.
Have you already thought about using dotted or dashed lines?
with dotted lines:
# color cycle 1, dt 1 = solid line
set linetype 1 lc rgb "dark-violet" lw 1 dt 1 pt 0
set linetype 2 lc rgb "sea-green" lw 1 dt 1 pt 7
set linetype 3 lc rgb "cyan" lw 1 dt 1 pt 6 pi -1
set linetype 4 lc rgb "dark-red" lw 1 dt 1 pt 5 pi -1
set linetype 5 lc rgb "blue" lw 1 dt 1 pt 8
set linetype 6 lc rgb "dark-orange" lw 1 dt 1 pt 3
set linetype 7 lc rgb "black" lw 1 dt 1 pt 11
set linetype 8 lc rgb "goldenrod" lw 1 dt 1
# color cycle 2, dt 3 = dot line
set linetype 9 lc rgb "dark-violet" lw 1 dt 3 pt 0
set linetype 10 lc rgb "sea-green" lw 1 dt 3 pt 7
set linetype 11 lc rgb "cyan" lw 1 dt 3 pt 6 pi -1
set linetype 12 lc rgb "dark-red" lw 1 dt 3 pt 5 pi -1
set linetype 13 lc rgb "blue" lw 1 dt 3 pt 8
set linetype 14 lc rgb "dark-orange" lw 1 dt 3 pt 3
set linetype 15 lc rgb "black" lw 1 dt 3 pt 11
set linetype 16 lc rgb "goldenrod" lw 1 dt 3
#
set linetype cycle 16
problem is, the png terminal will keep showing solid lines, so we need the pngcairo terminal:
set term pngcairo dashed size 800,600 font "sans" linewidth 3
to show smaller dots, use dt "."

gnuplot stacked bar chart arithmetic

tmp.data
DATE D0 D1 D2 D3 D4 D5
"2017-07-19" 10 8 6 4 2 1
"2017-07-20" 16 14 10 11 10 9
"2017-07-21" 6 5 4 4 3 1
"2017-07-22" 7 5 4 4 3 2
"2017-07-23" 8 6 4 2 1 1
tmp.gnu
set terminal png size
set output 'output.png'
set title "statistics"
set key font ",10"
D0 = "#99ffff"; D1 = "#4671d5"; D2 = "#ff0000"; D3 = "#f36e00"; D4 = "#8A2BE2#'; D5 = "#4671d5"
set auto x
unset xtics
set xtics nomirror rotate by -45 scale 0
set style data histogram
set style histogram rowstacked
set style fill solid border -1
set boxwidth 0.75
plot 'tmp.data' u 2:xtic(1) title columnheader, \
'' u 3:xtic(1) title columnheader, \
'' u 4:xtic(1) title columnheader, \
'' u 5:xtic(1) title columnheader, \
'' u 6:xtic(1) title columnheader, \
'' u 7:xtic(1) title columnheader
Creates the following:
The columns are accumulative.
What I'd like to have is have it proportional, for example in row 2.
10 - 8 = 2,
8 - 6 = 2,
6 - 4 = 2,
4 - 2 = 2,
2 - 1 = 1
If you want to plot the difference between two columns, then you must calculate the difference inside the using statement like
plot "tmp.data" using ($2 - $3):xtic(1)
to plot the difference between third and second column. For all your columns, and keeping the second as is, use (using the inline data $data requires 5.0):
$data <<EOD
DATE D0 D1 D2 D3 D4 D5
"2017-07-19" 10 8 6 4 2 1
"2017-07-20" 16 14 10 11 10 9
"2017-07-21" 6 5 4 4 3 1
"2017-07-22" 7 5 4 4 3 2
"2017-07-23" 8 6 4 2 1 1
EOD
set xtics nomirror rotate by -45 scale 0
set style data histogram
set style histogram rowstacked
set style fill solid border -1
set boxwidth 0.75
set key auto columnheader
plot $data u 2:xtic(1), \
for [i=3:7] '' u (abs(column(i) - column(i-1))):xtic(1)
Here, you must decide if you need the abs or not. The result is:

vary point color based on column value for multiple data blocks gnuplot

My question is very similar to this one, from which I was able to learn a lot. However, I am working with multiple data blocks, like this:
1 2 3
4 5 6
7 8 0
4 3 0
4 5 7
2 3 0
4 5 0
5 6 7
and I am plotting them like this:
plot "file.txt" index 0 u 1:2 w points pt 1,\
"file.txt" index 1 u 1:2 w points pt 2
which creates 2 different sets of points, each a different color. Now, my goal is to modify this script so that if the 3rd data column is 0, the color of the point will become black. I would like for the other points to remain the colors that they currently are though (meaning different from one another). I have done this:
set palette model RGB defined ( 0 'black', 1 'green' )
unset colorbox
plot file index 0 u 1:2:( $3 == 0 ? 0 : 1 ) w points pt 1 palette,\
file index 1 u 1:2:( $3 == 0 ? 0 : 1 ) w points pt 2 palette
This does exactly what I want, except of course both sets are now plotted in green. Is there any way to plot the black ones as desired, but also make each index a different color?
This is what the special "variable" color is for:
plot 'test.dat' i 0 u 1:2:($3 == 0? 0:1) w p pt 1 lc variable,\
'test.dat' i 1 u 1:2:($3 == 0? 0:2) w p pt 2 lc variable
variable in this context says to use the color of whatever "style index" was given in the third column. I set filters on the 3rd column variable which transforms the third column into a constant (1 or 2) if the data in that column isn't 0.
Another, less direct approach (which works since you're using points) is:
plot 'test.dat' i 0 u 1:($3 == 0? 1/0: $2) w p pt 1 lc rgb "red",\
'test.dat' i 0 u 1:($3 == 0? $2:1/0) w p pt 1 lc rgb "black,\
'test.dat' i 1 u 1:($3 == 0? 1/0: $2) w p pt 1 lc rgb "green",\
'test.dat' i 1 u 1:($3 == 0? $2:1/0) w p pt 1 lc rgb "black,\
It should work to define an extra point in the palette:
set palette model RGB defined ( 0 'black', 1 'green', 2 'red')
unset colorbox
plot file index 0 u 1:2:( $3 == 0 ? 0 : 1 ) w points pt 1 palette,\
file index 1 u 1:2:( $3 == 0 ? 0 : 2 ) w points pt 2 palette

Resources