Plotting sections of 2D plot (data) in GNUPLOT - gnuplot

I am trying to plot a 'heatmap' and 'a cut of that heatmap' at a specific position using the data at the bottom. I would like to use use the every command in gnuplot to do so, but I fail. every ::13::17: is supposed to plot data from the line 13 to line 17. I am getting an error from gnuplot.
reset
set terminal pngcairo
set output 'stack.png'
set palette defined (0 "white", 1 "red")
unset key
set style fill solid
#----------------- 2D plot ---------------
plot 'stack.dat' using 1:2:(log10($3)) with boxes linecolor palette notitle
# ---- PLot a section of the 2D plot ----------
set output 'oups.png'
plot 'stack.dat' using 2:(log10($3)) every ::13::17: with lines notitle
'stack.dat' is the datafile name
1 1 0.081051938235759735
1 2 0.039051856845617294
1 3 0.017708625644445419
1 4 0.053782138973474503
1 5 0.069525197148323059
2 1 0.046054631471633911
2 2 0.005992549471557140
2 3 0.010819308459758759
2 4 0.001308800885453820
2 5 0.032604649662971497
3 1 0.078480839729309082
3 2 0.000435109512181953
3 3 0.073167815804481506
3 4 0.052882101386785507
3 5 0.016808584332466125
4 1 0.060769289731979370
4 2 0.028200428932905197
4 3 0.031424820423126221
4 4 0.052520859986543655
4 5 0.078694045543670654
5 1 0.029850590974092484
5 2 0.027807384729385376
5 3 0.036195535212755203
5 4 0.026242787018418312
5 5 0.048620097339153290
How can I make it works?
Is there a better way to plot sections of heatmap?

The short answer is to use
plot 'stack.dat' using 2:(log10($3)) every :::2::2 with lines notitle
When you have multiple blocks of records there is a distinction between lines in a datafile and records. So in order to specify a data sequence, you need the block and record indexes. Check the docs for more info (see Every): http://gnuplot.sourceforge.net/docs_5.2/Gnuplot_5.2.pdf

Related

gnuplot - printing multiple plots from one dataset in same file as script

Ive this script in gnuplot and I want to print multiple plots from 1 dataset. Ive tried this command but it seems that command needs another sama dataset to execute this command correctly. Do you know how to solve it?
plot '-' using 1:2, '=' using 1:3
1 1 5
2 2 5
3 3 5
e
With '-' you would have to enter the same data again. Check help special-filenames.
You better do:
$Data <<EOD
1 1 5
2 2 5
3 3 5
EOD
plot $Data u 1:2, '' u 1:3

3D view of 2D histogram (heat map) in gnuplot

How can I (or can I) present this gnuplot histogram:
in this style of 3D histogram view - in gnuplot?:
Using the same data and data format as in the gnuplot example in answer would be best.
The questions of the OP: How can I ...
There are a few workarounds that produce an output resembling the sample that you show.
They do not have all the bells and whistles you may want for formatting, but they are approximations which look "ok".
As per http://lowrank.net/gnuplot/plotpm3d-e.html#6.9 , pm3d should do the trick for what you need.
See also pm3d down in this link.
If shading is essential, you can try using some of this. But you should work out quite a bit.
Another poor-man approximation to what you are asking for is given here.
I do not have gnuplot here to test these.
... (or can I)
It is widely documented that there is no facility for a 3D histogram plot, and anything to do a real such plot with the corresponding options (shading, spacing, back walls, perspective view, selection of point of view, etc.) requires complex user programming.
Closest thing available in gnuplot that doesn't involve significant hackery and/or preprocessing seems to be impulses style. The help docs even suggest their use for 3d bar charts:
To use this style effectively in 3D plots, it is
useful to choose thick lines (linewidth > 1). This approximates a 3D bar chart.
Here is a simple example using impulses, and matrix style data as in the linked heatmap example:
set title ''
unset key
set xyplane 0
set view 60,300,1.2
set xrange [-0.5:5]
set yrange [-0.5:5]
splot '-' matrix with impulses lw 20
0 0 0 0 0 0
0 1 2 3 4 5
0 2 4 6 8 10
0 3 6 9 12 15
0 4 8 12 16 20
0 5 10 15 20 25
e
The issue is gnuplot renders the impulses as 2d pen strokes. It would be ideal if there was a way to some how apply a 3d surface effect to those lines, but I don't think there is a way, since they are just lines, not surfaces.
You can also use vectors style to achieve similar result to impulses above, but with support for "rgb variable" (AFAIK impulses doesn't support this). This allows you to change color based on z-value - but still no 3d surface. You'll have to use a different data format for vectors style (AFAIK), but it's a simpler transform from matrix style data than some other hack require:
set xyplane 0
set view 60,301,1.2
set xrange [0.5:5]
set yrange [0.5:5]
rgb(r,g,b) = 65536 * int(r) + 256 * int(g) + int(b)
splot '-' using 1:2:(0):(0):(0):3:(rgb($3*(255/25),0,0)) with vectors nohead lw 40
5 5 25
5 4 20
5 3 15
5 2 10
5 1 5
4 5 20
4 4 16
4 3 12
4 2 8
4 1 4
3 5 15
3 4 12
3 3 9
3 2 6
3 1 3
2 5 10
2 4 8
2 3 6
2 2 4
2 1 2
1 5 5
1 4 4
1 3 3
1 2 2
1 1 1
e

How does gnuplot skip irrelevent column in plot?

I have a data file like this
# Time A irrelevent_col B
1 2 3 4
2 3 4 5
3 4 5 6
4 5 6 7
I am trying to plot two lines Time vs A, Time vs B with labels "A" and "B". How can I skip the "irrelevent_col" column?
I did the following, but the code still plots the "irrelevent_col" column. Shouldn't the ? : operator gets ride of that column?
set datafile commentschars "!!!"
plot for [i=2:4] filename using 1:(columnhead(i+1) ne "irrelevent_col" ? column(i) : 1/0) title columnhead(i+1)
Thanks!
If I understood correctly your question:
plot "filename" using 1:2 title "A" with lines,\
"filename" using 1:4 title "B" with lines
Let me repeat what I've understood from your question:
You have a large number of columns and you want to plot them all in a loop, but exclude a single column (or a few) by name.
Of course, you can specify all columns you do want to plot, like in #ViniciusPlacco's answer, however, as I understand that's what you wanted to avoid, since you have many more columns in your real data. You can also always use external tools to pre-process your data, but here I would like to suggest a gnuplot-only and hence platform-independent solution.
Why your solution is not working, I can only speculate: I guess using columnheader twice in a plot iteration creates problems (at least for gnuplot<=5.2). But I could be wrong. But as I will show below, your solution will work for gnuplot>=5.4.0.
Furthermore, you want to specify the columns by header not by column number.
In addition, your header line starts with the comment char '#', but you can easily change that to access the columnheader information.
In the example below you can specify a list of several headers which you don't want to plot. Maybe the script(s) can be further simplified.
Script: (works for gnuplot>=5.4.0, July 2020)
### exclude some columns by header from plotting loop (gnuplot>=5.4.0)
reset session
$Data <<EOD
# Time A B C D E
1 2 3 4 5 6
2 3 4 5 6 7
3 4 5 6 7 8
4 5 6 7 8 9
EOD
set datafile commentschars '' # no commentchar
set key top left noenhanced noautotitle
inList(w,list) = int(sum[_i=1:words(list)] w eq word(list,_i))
doNotPlot = 'B C'
color = 1
plot for [col=2:6] $Data u 1:((b=inList(myHeader=columnhead(col+1),doNotPlot)) ? \
NaN : ($0==1?color=color+1:0, column(col))) w lp pt 7 lc color ti (b ? '' : myHeader)
### end of script
Result:
For older gnuplot versions <5.4.0 you need a different approach:
get all headers into a string
specify all your headers of the columns you don't want to plot in a string
for gnuplot>=5.0.0, subtract two lists and keep the column numbers for the header you do want to plot
Script: (works for gnuplot>=5.2.2, Nov. 2017; result same as graph above)
### exclude some columns by header from plotting loop (gnuplot>=5.2.2)
reset session
$Data <<EOD
# Time A B C D E
1 2 3 4 5 6
2 3 4 5 6 7
3 4 5 6 7 8
4 5 6 7 8 9
EOD
set datafile commentschars '' # no commentchar
set key top left noenhanced noautotitle
inList(w,list) = int(sum[_i=1:words(list)] w eq word(list,_i))
doNotPlot = 'B C'
myHeaders = ''
color = 1
plot for [col=2:6] $Data u 1:((b=inList(myHeader=columnhead(col+1),doNotPlot)) ? NaN : \
($0==1 ? (color=color+1, myHeaders=myHeaders.' '.myHeader) : 0, column(col))) w lp pt 7 lc color, \
for [i=1:color] NaN w lp pt 7 lc i ti word(myHeaders,i)
### end of script
Script: (works for gnuplot>=5.0.0, Jan. 2015; result same as graph above)
### exclude some columns by header from plotting loop (gnuplot>=5.0.0)
reset session
$Data <<EOD
# Time A B C D E
1 2 3 4 5 6
2 3 4 5 6 7
3 4 5 6 7 8
4 5 6 7 8 9
EOD
set datafile commentschars '' # no commentchar
set datafile separator "\n" # or another character which is not in the header line
stats $Data u (allHeaders = strcol(1)[2:]) ever ::::0 nooutput # get header line into string
set datafile commentschar # reset to default
set datafile separator whitespace # ditto
inList(w,list) = int(sum[_i=1:words(list)] w eq word(list,_i))
subtractLists(list1,list2) = (_s=' ', sum[_j=1:words(list1)] (_s0=word(list1,_j), \
inList(_s0,list2) ? 0 : (_s=_s._s0.' ', \
myColNos=myColNos.' '._j), 0), _s)
doNotPlot = 'B C'
myColNos = ''
myHeaders = subtractLists(allHeaders,doNotPlot)
myColNo(i) = column((word(myColNos,i)))
set key top left noenhanced noautotitle
plot for [i=2:words(myHeaders)] $Data u 1:(myColNo(i)) w lp pt 7 ti word(myHeaders,i)
### end of script

gnuplot : using a logarithmic axis for a histogram

I have a data file that I am creating a histogram from.
The data file is :
-0.1 0 0 JANE
1 1 1 BILL
2 2 1 BILL
1 3 1 BILL
6 4 0 JANE
35 5 0 JANE
9 6 1 BILL
4 7 1 BILL
24 8 1 BILL
28 9 1 BILL
9 10 0 JANE
16 11 1 BILL
4 12 0 JANE
45 13 1 BILL
My gnuplot script is :
file='test.txt'
binwidth=10
bin(x,width)=width*floor(x/width)
set boxwidth 1
plot file using (bin($1,binwidth)):(1.0) smooth freq with boxes, \
file using (1+(bin($2,binwidth))):(1.0) smooth freq with boxes
I would like to plot this data on a logscale in y. However there are some 0 values (because some of the bins are empty) that cannot be handled by set logscale y. I get the error Warning: empty y range [1:1], adjusting to [0.99:1.01].
According to gnuplot's help, "The frequency option makes the data monotonic in x; points with the same x-value are replaced by a single point having the summed y-values."
How can I take the log10() of the summed y-values computed by smooth freq with boxes?
There are at least two things that you could do. One is to use a linear axis between 0 and 1 and then use the logarithmic one as explained in this answer. The other one is to plot to a table first and then set the log scale ignoring the points with zero value.
With a normal linear axis and your code (plus set yrange [0:11]) your data looks:
Now lets plot to a table, then set the log scale, then plot ignoring the zero values:
file='test.txt'
binwidth=10
bin(x,width)=width*floor(x/width)
set table "data"
plot file using (bin($1,binwidth)):(1.0) smooth freq, \
file using (1+(bin($2,binwidth))):(1.0) smooth freq
unset table
set boxwidth 1
set logscale y
set yrange [0.1:11]
plot "data" index 0 using ($1):($2 == 0 ? 1/0 : $2) with boxes lc 1, \
"data" index 1 using ($1):($2 == 0 ? 1/0 : $2) with boxes lc 2
set table sometimes generates some undesirable points in the plot, which you can see at x = 0. To get rid of them you can use "< grep -v u data" instead of "data".

histograms with shaded lines bars ( no color bars )

I have a set of data to plot using histogram bars. I want the bars to be shaded in lines but not filled with colros like red, blue or any other colors. The sample data is as below:
1 23 2 9 13
2 25 5 7 12
3 26 5 3 13
4 20 6 8 5
5 23 5 12 15
6 23 5 12 11
7 22 9 12 5
8 24 16 5 8
9 23 2 12 10
10 23 15 5 9
I have the sample code as well,
set terminal jpeg medium
set output "histo.jpeg"
set boxwidth 0.75 absolute
set style fill solid 1.00 border -1
set style histogram rowstacked
set style data histograms
set xtics 1000 nomirror
set ytics 100 nomirror
set mxtics 2
set mytics 2
set ytics 10
set yrange [0:50]
set ylabel "Total time"
set xlabel "Session number"
plot 'papa2.dat' using 3 t "Server", '' using 4 t "Client", '' using 5:xtic(1) t "Network"
But when I use this I get bars in histogram filled with colors.
Can anyone help me to on how to plot the graphs in lines instead of colors?
Many thanks in advance.
(note: I got these sample data and code from internet)
I wonder what you want is something shown in the first figure of this page: histograms.dem.
If that is the case, you can either do set style data linespoint as shown in the demo page, or plot 'papa2.dat' using 3 t "Server" with line when you use the plot command.
Note that you have a lot more demo scripts available in the gnuplot project website: Demo scripts for gnuplot 4.4

Resources