gnuplot: multiplots using multiple lines in each plot and in-line data - gnuplot

I am trying to call gnuplot from torch in order to plot several things. I am trying to use multiplots (like subplots in matlab), and at the same time plot different curves in the same plots. Furthermore, I'm defining the data in-line, i.e., avoiding writing the data in external files (plot '-').
I've tried plotting several curves with '-' in several ways, with multiplot previous and next, with replot... but any combination messes up the layout in one way or another. Does anybody knows how to do this or can give some hints?
Thanks!
Update: Added a small example. Blue and red lines should be plotted in the same plot (top one), while green one should be plotted alone in the bottom one.
Example:
gnuplot.figure(1)
gnuplot.raw('set terminal x11 0 position 1200,20 persist')
gnuplot.raw('set multiplot layout 2,1')
gnuplot.raw([[plot '-' lt rgb 'blue'
0 0
100 30
e]])
gnuplot.raw([[plot '-' lt rgb 'red'
0 30
100 60
e]])
gnuplot.raw([[plot '-' lt rgb 'green'
0 60
100 90
e]])
gnuplot.raw('unset multiplot')

I'm not familiar with torch, but I think your problem is with trying to do three separate plot statements. In gnuplot, to do multiple curves in the same plot (two in your top plot), you specify them in the same command separated by a comma. Feeding inline data, this means that you will need to provide one set of data, end it with e, provide another, and then end it with e.
Additionally, you can use lc (linecolor) instead of lt to set your colors.
In straight gnuplot, you will do this like so:
set terminal x11 0 position 1200,20 persist
set multiplot layout 2,1
plot '-' lc rgb 'blue', '-' lc rgb 'red'
0 0
100 30
e
0 30
100 60
e
plot '-' lc rgb 'green'
0 60
100 90
e
unset multiplot
If you want lines, just add with lines to all three plot specifications like
plot '-' lc rgb 'blue' with lines, '-' lc rgb 'red' with lines
plot '-' lc rgb 'green' with lines
or change the default data style with set style data lines.

Related

Remove duplicated outliers in gnuplot boxplot [duplicate]

I have a large set of data points. I try to plot them with a boxplot, but some of the outliers are the exact same value and they are represented on a line beside each other. I found How to set the horizontal distance between outliers in gnuplot boxplot, but it doesn't help too much, as it is apparently not possible.
Is it possible to group the outliers together, print one point and then print a number in brackets beside it to indicate how many points there are? I think this would make it more readable in a graph.
For information, I have three boxplots for one x value and that times six in one graph. I am using gnuplot 5 and already played around with the pointsize, which doesn't reduce the distance anymore.
I hope you can help!
Edit:
set terminal pdf
set output 'dat.pdf'
file0 = 'dat1.dat'
file1 = 'dat2.dat'
file2 = 'dat3.dat'
set pointsize 0.2
set notitle
set xlabel 'X'
set ylabel 'Y'
header = system('head -1 '.file0);
N = words(header)
set xtics ('' 1)
set for [i=1:N] xtics add (word(header, i) i)
set style data boxplot
plot file0 using (1-0.25):1:(0.2) with boxplot lw 2 lc rgb '#8B0000' fs pattern 16 title 'A'
plot file1 using (1):1:(0.2) with boxplot lw 2 lc rgb '#00008B' fs pattern 4 title 'B'
plot file2 using (1+0.25):1:(0.2) with boxplot lw 2 lc rgb '#006400' fs pattern 5 title 'C'
for [i=2:N] plot file0 using (i-0.25):i:(0.2) with boxplot lw 2 lc rgb '#8B0000' fs pattern 16 notitle
for [i=2:N] plot file1 using (i):i:(0.2) with boxplot lw 2 lc rgb '#00008B' fs pattern 4 notitle
for [i=2:N] plot file2 using (i+0.25):i:(0.2) with boxplot lw 2 lc rgb '#006400' fs pattern 5 notitle
What is the best way to implement it with this code already in place?
There is not option to have this done automatically. Required steps to do this manually in gnuplot are:
(In the following I assume, that the data file data.dat has only a single column.)
Analyze your data with stats to determine the boundaries for the outliers:
stats 'data.dat' using 1
range = 1.5 # (this is the default value of the `set style boxplot range` value)
lower_limit = STATS_lo_quartile - range*(STATS_up_quartile - STATS_lo_quartile)
upper_limit = STATS_up_quartile + range*(STATS_up_quartile - STATS_lo_quartile)
Count only the outliers and write them to a temporary file
set table 'tmp.dat'
plot 'data.dat' using 1:($1 > upper_limit || $1 < lower_limit ? 1 : 0) smooth frequency
unset table
Plot the boxplot without the outliers, and the outliers with the labels plotting style:
set style boxplot nooutliers
plot 'data.dat' using (1):1 with boxplot,\
'tmp.dat' using (1):($2 > 0 ? $1 : 1/0):(sprintf('(%d)', int($2))) with labels offset 1,0 left point pt 7
And this needs to be done for every single boxplot.
Disclaimer: This procedure should work basically, but having no example data I couldn't test it.

Plot vertical lines in Gnuplot to represent one dimensional data

I am trying to plot the bandwidth growth of a TCP connection using Gnuplot.
I have 2 log files, one with bandwidth at a time, and another with time stamps at which a packet drop occurs. I want to represent the packet drops in the same graph as the bandwidth, possibly with a vertical line on the X axis (time).
Please provide suggestions!
Just as a very rough example, here are the file formats.
Bandwidth.dat
0.001 2
0.002 3
0.003 5
0.004 8
Packet_Drop.dat
0.006
0.12
0.39
Required plot:
Sorry, didn't know how to make a better graph quickly!
As one option you could use the impulses plotting style, which plots vertical lines from y=0 to the given value:
max = 10
plot 'Bandwidth.dat' using 1:2 with lines linecolor rgb 'black',\
'Packet_Drop.dat' using 1:(max) with impulses linecolor rgb 'red'
The backdraw with this option is, that you must know the maximum value of the y-axis. You can get this e.g. by plotting to the unknown terminal, and then use the GPVAL_Y_MAX value:
set terminal push # save current terminal
set terminal unknown
plot 'Bandwidth.dat' using 2
set terminal pop # restore terminal
plot 'Bandwidth.dat' using 1:2 with lines linecolor rgb 'black',\
'Packet_Drop.dat' using 1:(GPVAL_Y_MAX) with impulses linecolor rgb 'red'
(One cannot use stats to get the maximum value of an autoscaled axis.)
Alteratively you can read in the x-values from your data file into a string and iterate over the words and set some arrows accordingly. On Linux, use
packet_drop = system('cat Packet_Drop.dat')
set for [w in packet_drop] arrow from first w, graph 0 to first w, graph 1 linecolor rgb 'red' nohead
plot 'Bandwidth.dat' using 1:2 with lines lc rgb 'black'
On Windows it should work with
packet_drop = system('type Packet_Drop.dat')
and you need to use the wgnuplot_pipes.exe when using version 4.6.

How to make this gnuplot diagram

This is my gnuplot digram. My digram is this:
I want to create this one:
From each point on the line. create a line to X and Y:
Change the color of the points to another thing than red.
This is my plot script:
set terminal png size 900,600 enhanced font "Helvetica,20"
set output 'All recived Packet in the network per second.png'
set grid
set xlabel "Transmision Range"
set ylabel "All of recived Packet in the network per second"
set title "Recive Packet pre second"
plot "NumOfRcvPkt.dat" using 2:3 title 'Transmision Range' with linespoints
Also here is the content of NumOfRcvPkt.dat file:
0 15 124
1 20 105
2 25 82
This is achieved as follows:
xmin=14 ; ymin=80
set xrange [xmin:*] ; set yrange [ymin:*]
plot "data" u 2:3 w l lc rgb "red", \
"" u 2:3 w p pt 7 lc rgb "blue", \
"" u (xmin):3:($2-xmin):(0) w vectors nohead lt 2 lc rgb "black", \
"" u 2:(ymin):(0):($3-ymin) w vectors nohead lt 2 lc rgb "black"
The first two lines set the ranges. This is important because you need to know where the edges lie in order to draw your black dashed lines.
Then, for the plot command, the first line plots the data with red lines, the second plots the data with blue circles, the third one plots horizontal black dashed lines and the fourth one plot vertical dashed lines. In order for your terminal to accept dashed styles (selected with lt 2) you need to add dashed, e.g. set term png dashed.
This is the result:

Gnuplot read line style from data file column

I'd like to draw an impulse graph from a text file that looks like this:
II 5 0 0 288.40 1.3033e+14
II 6 0 0 289.60 1.5621e+14
II 1 4 0 302.70 3.0084e+13
II 2 4 0 303.40 4.0230e+13
II 1 5 1 304.40 3.4089e+13
The plot conceptually should be plot "datafile.dat" using 5:6 w impulses ls $2.
Basically, given a previously defined set of line styles, I'd like to input the line style number from column 2 for every couple of plotted points from column 5 and 6.
Also I'd like to create a text box, for every plotted point, taking strings from the first four columns.
Does somebody know if that's possible?
To use the data from column two as line style use set style increment user and linecolor variable:
set style increment user
plot "datafile.dat" using 5:6:2 with impulses lc var
In order to place a label, use the labels plotting style:
plot "datafile.dat" using 5:6:1 with labels offset 0,1
Putting everything together, you have:
set style increment user
set for [i=1:6] style line i lt i
set yrange [0:*]
set offsets 0,0,graph 0.1,0
plot "datafile.dat" using 5:6:2 with impulses lc var, "" using 5:6:1 with labels offset 0,1
The result with 4.6.3 is:
Thanks for the helpful answer above. It almost solved my problem
I'm actually trying to use a column from my data file to specify a linestyle (dot, squares,triangles, whatever as long as it's user-defined), and not a linecolor. Is there any way to do that?
This line works : I get points with different colors (specified in column 4), but the point style is the same.
plot "$file" u 1:2:4 w p notitle lc var, "" using 1:2:3 with labels offset 0,1 notitle
Replacing lc with ls after defining my own styles doesn't work (ls can't have variable as an option)
I can live without different linestyles, but it would be much prettier.
You only have to replace the lineset for [i=1:6] style line i lt i for set for [i=1:6] style line i lt i pt %, Where % can be any type of point you want

Draw two pointlines in a histogram (gnuplot)?

I want to draw two pointsline in a histogram by gnuplot, The result chart is not exactly what I wanted. Actually, I want points in two pointlines(the pink one and the lightgreen one) align the center of two types of pillars, so the lightgreen one keeps still, while the pink one move a little to the right, just like the black one I have drawn.
the 'test.dat' is as follow:
1 10 15 8 22
2 11 19 7 21
3 9 14 7 19
4 10 21 8 23
5 9 17 9 21
and the 'plt' file:
set style data histogram
unset key
set yrange[0:12]
set y2range[0:25]
plot "test.dat" using 2:xticlabel(1) axis x1y1,\
"test.dat" using 3:xticlabel(1) axis x1y2 with linespoints,\
"test.dat" using 4:xticlabel(1) axis x1y1,\
"test.dat" using 5:xticlabel(1) axis x1y2 with linespoints
My answer is based on this contribution which uses boxes instead of a histogram. The benefit is that, you know exactly where those boxes are placed, which you can exploit for the line plot.
Here's the code:
dx=1.
n=2.
total_box_width_relative=0.25
gap_width_relative=0.1
d_width=(gap_width_relative+total_box_width_relative)*dx/n
set boxwidth total_box_width_relative/n relative
set style fill transparent solid 0.5 noborder
plot "test.dat" u ($1):2 w boxes lc rgb"green" notitle,\
"test.dat" u ($1+d_width):4 w boxes lc rgb"red" notitle,\
"test.dat" u ($1):3 w linespoints notitle,\
"test.dat" u ($1+d_width):5 w linespoints notitle
set yrange [0:15]
replot
Some explanations to the code:
The dx should be selected according to your data file, otherwise the spacing of the boxes will be off.
The number of data-sets for the boxes is given by n
The total_box_width_relative and gap_width_relative control the axial with and spacing of the boxes
the two set ... commands control the appearance of your boxes
within the plot command you now have to separate: You call one set of boxes and lines with the original 1st column data: ($1), however, for the second set of boxes and the corresponding line, you choose your defined axial offset: ($1+d_width) - this will ensure that the data points from the line plot will align with the boxes
it may be necessary to include a set yrange command
The plot will look like this:
Note
I changed the data for the line plots with regard to the data you provided. This was done only to bring the points closer to the boxes and illustrate the effect.

Resources