How to add vertical lines with label using gnuplot? - gnuplot

I have this script to plot data from a CSV file using gnuplot. I want to add 3 vertical lines at different times on the plot to show where I changed the workload of my experiment. I was trying to do it with vector but it was messing the data already plotted. I attached my chart and added manually the vertical blue line as an example of what I want.
#!/usr/bin/gnuplot
# set grid
set key under left maxrows 1
set style line 1 lc rgb '#E02F44' lt 1 lw 1 ps 0.5 pt 7 # input throughput
set style line 2 lc rgb '#FF780A' lt 1 lw 1 ps 0.5 pt 1 # output throughput
set style line 3 lc rgb '#56A64B' lt 1 lw 1 ps 0.5 pt 2 # average processing latency
set style line 4 lc rgb '#000000' lt 1 lw 1 ps 0.5 pt 3 # 99th percentile processing latency
set terminal pdf
set pointintervalbox 0
set datafile separator ','
set output "efficiency-throughput-networkbuffer-baseline-TaxiRideNYC-100Kpersec.pdf"
set title "Throughput vs. processing latency consuming 50K r/s from the New York City (TLC)"
set xlabel "time (minutes)"
set ylabel "Throughput (K rec/sec)"
set y2label "processing latency (seconds)"
set ytics nomirror
set y2tics 0, 1
set xdata time # tells gnuplot the x axis is time data
set timefmt "%Y-%m-%d %H:%M:%S" # specify our time string format
set format x "%M" # otherwise it will show only MM:SS
plot "throughput-latency-increasing.csv" using 1:(column(2)/1000) title "IN throughput" with linespoints ls 1 axis x1y1 \
, "throughput-latency-increasing.csv" using 1:(column(10)/1000) title "OUT throughput" with linespoints ls 2 axis x1y1 \
, "throughput-latency-increasing.csv" using 1:(column(18)/1000) title "avg. latency" with linespoints ls 3 axis x1y2 \
, "throughput-latency-increasing.csv" using 1:(column(26)/1000) title "99th perc. latency" with linespoints ls 4 axis x1y2 \
#, "" using 1:($1):(3):(0) notitle with vectors nohead
My data file is:
"Time","pre_aggregate[0]-IN","pre_aggregate[1]-IN","pre_aggregate[2]-IN","pre_aggregate[3]-IN","pre_aggregate[4]-IN","pre_aggregate[5]-IN","pre_aggregate[6]-IN","pre_aggregate[7]-IN","pre_aggregate[0]-OUT","pre_aggregate[1]-OUT","pre_aggregate[2]-OUT","pre_aggregate[3]-OUT","pre_aggregate[4]-OUT","pre_aggregate[5]-OUT","pre_aggregate[6]-OUT","pre_aggregate[7]-OUT","pre_aggregate[0]-50","pre_aggregate[1]-50","pre_aggregate[2]-50","pre_aggregate[3]-50","pre_aggregate[4]-50","pre_aggregate[5]-50","pre_aggregate[6]-50","pre_aggregate[7]-50","pre_aggregate[0]-99","pre_aggregate[1]-99","pre_aggregate[2]-99","pre_aggregate[3]-99","pre_aggregate[4]-99","pre_aggregate[5]-99","pre_aggregate[6]-99","pre_aggregate[7]-99"
"2020-04-27 10:31:00",1428.05,1274.4666666666667,1364.6166666666666,1384.4666666666667,1327.3,1376.5,1390.9166666666667,1418.35,1428.05,1274.4666666666667,1364.6333333333334,1384.4666666666667,1327.3,1376.5,1390.9166666666667,1418.35,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1
"2020-04-27 10:31:15",1463.5833333333333,1452.3666666666666,1346.7333333333333,1380.3833333333334,1429.4833333333333,1431.6833333333334,1442.85,1425.15,1463.5833333333333,1452.3666666666666,1346.7333333333333,1380.3833333333334,1429.4833333333333,1431.6833333333334,1442.85,1425.15,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1
"2020-04-27 10:31:30",1393.4666666666667,1396.65,1369.55,1381.3833333333334,1336.8,1434.5166666666667,1440.0833333333333,1399.2833333333333,1393.45,1396.65,1369.55,1381.3833333333334,1336.8,1434.5166666666667,1440.0833333333333,1399.2833333333333,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1
"2020-04-27 10:31:45",1404.8833333333334,1448.5333333333333,1313.9,1308.1,1359.6333333333334,1329.5166666666667,1338.4166666666667,1481.5666666666666,1404.8833333333334,1448.5333333333333,1313.9,1308.1,1359.6333333333334,1329.5166666666667,1338.4166666666667,1481.5833333333333,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1

Of course you can plot your lines and labels. In the example below I'm using the newer syntax compared to set xdata time. Which requires timecolumn(1,myTimeFmt) and e.g. set format x "%M" time.
Your date is in double quotes, so you have to define the timeformat using single quotes including the double quotes.
Furthermore, you are using absolute times, so your lines ideally use the same format. You can put it into a datablock. I hope you can adapt the code to your needs.
Code:
### vertical lines with labels on time axis
reset session
$myLines <<EOD
"2020-04-27 10:34:00"
"2020-04-27 10:39:20"
"2020-04-27 10:43:50"
"2020-04-27 10:48:00"
EOD
myTimeFmt = '"%Y-%m-%d %H:%M:%S"'
StartDate = '"2020-04-27 10:30:00"'
EndDate = '"2020-04-27 10:52:00"'
set format x "%M" time
set xrange [strptime(myTimeFmt,StartDate):strptime(myTimeFmt,EndDate)]
yLow = 1.4
yHigh = 3.5
set tmargin screen 0.90
plot '+' u (strptime(myTimeFmt,StartDate)+$0*60):(rand(0)*3+0.5) w l lc rgb "red" notitle, \
$myLines u (timecolumn(1,myTimeFmt)):(yHigh):("Workload\nchanged") w labels right offset -0.5,1.5 not, \
$myLines u (timecolumn(1,myTimeFmt)):(yLow):(0):(yHigh-yLow) w vec lc rgb "blue" lw 2 nohead not
### end of code
Result:

Related

How do I edit the time values on the x-axis to start from zero when using gnuplot?

I do not want to change my data files that come with the first column containing the time values. Then I formatted it on gnuplot to show only the hour and minute. But it is a bit ugly to start the time from 8:00. I would like to start it from 0 and keep the values at the same pace of the data file. I was trying to use a constant like this example shows How do I make a plot in gnuplot with the lowest value automatically subtracted from the y data? but it is not working.
Here are my source and the plot.
#!/usr/bin/gnuplot
# set grid
set key outside bottom center horizontal
set key font ",19"
set style line 1 lc rgb '#E02F44' lt 1 lw 1 ps 0.5 pt 7 # input throughput
set style line 2 lc rgb '#FF780A' lt 1 lw 1 ps 0.5 pt 1 # output throughput
set style line 3 lc rgb '#56A64B' lt 1 lw 1 ps 0.5 pt 2 # average processing latency
set style line 4 lc rgb '#000000' lt 1 lw 1 ps 0.5 pt 3 # 99th percentile processing latency
set style arrow 1 heads ls 4
set style arrow 2 head ls 4
set terminal pdf
set pointintervalbox 0
set datafile separator ','
set output "Cost-20K-ThroughputVsLatency.pdf"
#set title ""
set xlabel "time (minutes)" font ",17" offset 0,1,0
set xtics font ",8" offset 0,0.5,0
set xdata time # tells gnuplot the x axis is time data
set timefmt "%Y-%m-%d %H:%M:%S" # specify our time string format
set format x "%H:%M" # otherwise it will show only MM:SS
set xrange ["2020-05-07 08:05:00":"2020-05-07 09:50:00"]
set ylabel "Throughput (K rec/sec)" font ",18" offset 0,0,0
set yrange [0:7]
set ytics font ",20"
#set y2label "processing latency (seconds)" font ",18" offset -1.5,0,0
set y2range [0:25]
set ytics nomirror
set y2tics 0, 5 font ",17"
plot "throughput-vs-latency-20K.csv" using 1:(column(2)/1000) title "IN throughput" with linespoints ls 1 axis x1y1 \
, "throughput-vs-latency-20K.csv" using 1:(column(10)/1000) title "OUT throughput" with linespoints ls 2 axis x1y1 \
, "throughput-vs-latency-20K.csv" using 1:(column(18)/1000) title "avg. latency" with linespoints ls 3 axis x1y2 \
, "throughput-vs-latency-20K.csv" using 1:(column(26)/1000) title "99th latency" with linespoints ls 4 axis x1y2
UPDATE
I changed my script like you said #theozh but I am still not getting the x axis starting from 0.
set key bottom right
set key font ",11"
set style line 1 lc rgb '#E02F44' lt 1 lw 1 ps 0.5 pt 7 # input throughput
set style line 2 lc rgb '#FF780A' lt 1 lw 1 ps 0.5 pt 1 # output throughput
set style line 3 lc rgb '#56A64B' lt 1 lw 1 ps 0.5 pt 2 # average processing latency
set style line 4 lc rgb '#000000' lt 1 lw 1 ps 0.5 pt 3 # 99th percentile processing latency
set style arrow 1 heads ls 4
set term pdfcairo size 5.0in,2.5in
set pointintervalbox 0
set datafile separator ','
set tmargin 1.5
set border 1+2+8
set xtics nomirror
set output "throughput-latency-increasingK-TaxiRideNYC-50Kpersec.pdf"
myTimeFmt = "%Y-%m-%d %H:%M:%S"
set xlabel "time (minutes)" font ",9" offset 0,1.5,0
set xtics font ",8" #rotate by 45 right
set ylabel "Throughput (K rec/sec)" font ",10" offset 2,0,0
set yrange [0:3.5]
set y2label "processing latency (seconds)" font ",10" offset -2,0,0
set y2range [0:14]
set ytics nomirror
set y2tics 0, 2
set xdata time # tells gnuplot the x axis is time data
set format x "%M" time
plot t=0 "throughput-latency-increasing.csv" u (t==0?(t0=timecolumn(1,myTimeFmt),t=1):NaN, timecolumn(1,myTimeFmt)-t0):(column(2)/1000) title "IN throughput" with linespoints ls 1 axis x1y1 \
, t=0 "throughput-latency-increasing.csv" u (t==0?(t0=timecolumn(1,myTimeFmt),t=1):NaN, timecolumn(1,myTimeFmt)-t0):(column(18)/1000) title "avg. latency" with linespoints ls 3 axis x1y2 \
, 4/0 t "# of tuples pre-aggregating" with vectors arrowstyle 1
values are here:
"Time","pre_aggregate-outPool[0]-avg","pre_aggregate-outPool[1]-avg","pre_aggregate-outPool[2]-avg","pre_aggregate-outPool[3]-avg","pre_aggregate-outPool[4]-avg","pre_aggregate-outPool[5]-avg","pre_aggregate-outPool[6]-avg","pre_aggregate-outPool[7]-avg","pre_aggregate-outPool[0]-99","pre_aggregate-outPool[1]-99","pre_aggregate-outPool[2]-99","pre_aggregate-outPool[3]-99","pre_aggregate-outPool[4]-99","pre_aggregate-outPool[5]-99","pre_aggregate-outPool[6]-99","pre_aggregate-outPool[7]-99","pre_aggregate[0]-param","pre_aggregate[1]-param","pre_aggregate[2]-param","pre_aggregate[3]-param","pre_aggregate[4]-param","pre_aggregate[5]-param","pre_aggregate[6]-param","pre_aggregate[7]-param"
"2020-04-27 10:22:45",33.33333432674408,33.33333432674408,33.33333432674408,33.33333432674408,33.33333432674408,33.33333432674408,33.33333432674408,33.33333432674408,33,70,75,79,33,41,62,75,50000,50000,50000,50000,50000,50000,50000,50000
"2020-04-27 10:23:00",33.33333432674408,33.33333432674408,33.33333432674408,33.33333432674408,33.33333432674408,33.33333432674408,33.33333432674408,33.33333432674408,33,33,75,79,33,33,33,37,50000,50000,50000,50000,50000,50000,50000,50000
"2020-04-27 10:23:15",33.33333432674408,33.33333432674408,33.33333432674408,33.33333432674408,33.33333432674408,33.33333432674408,33.33333432674408,33.33333432674408,33,33,33,33,33,33,33,33,50000,50000,50000,50000,50000,50000,50000,50000
"2020-04-27 10:23:30",33.33333432674408,33.33333432674408,33.33333432674408,33.33333432674408,33.33333432674408,33.33333432674408,33.33333432674408,33.33333432674408,62,66,50,62,66,45,50,66,50000,50000,50000,50000,50000,50000,50000,50000
"2020-04-27 10:23:45",33.33333432674408,33.33333432674408,33.33333432674408,33.33333432674408,33.33333432674408,33.33333432674408,33.33333432674408,33.33333432674408,62,66,50,62,66,45,50,66,50000,50000,50000,50000,50000,50000,50000,50000
"2020-04-27 10:24:00",33.33333432674408,33.33333432674408,33.33333432674408,33.33333432674408,33.33333432674408,33.33333432674408,33.33333432674408,33.33333432674408,33,33,33,33,33,33,33,33,50000,50000,50000,50000,50000,50000,50000,50000
The following example uses the newer gnuplot date time syntax (see help timecolumn), e.g. timecolumn(1,myTimeFmt) and set format x "%H:%M" time.
In order to normalize your time series to the first data point you have to store this time into a variable, e.g. t0 which you can "re-use" in successive plot commands from the same datafile.
Note the different time format for the x axis: "%H:%M" for day time and "%tH:%tM" for hours exceeding 24 hours or minutes exceeding 60 minutes, see help time_specifiers.
Edit:
for better readability of the plot command, I "outsourced" the normalization into a function Normalize(). But note that t=0 is still required at the beginning of the plot command.
in case you have some (uncommented) header lines, you need to skip them via skip <number of header lines>.
Code:
### normalize time data relative to start time
reset session
myTimeFmt = "%Y-%m-%d %H:%M:%S"
# create some test data
set table $Data
plot '+' u (strftime(myTimeFmt,time(0) + $1*3600*2)):(cos($1)) w table
unset table
# function to normalize time column to first value
Normalize(c) = (t==0?(t0=timecolumn(c,myTimeFmt),t=1):NaN, timecolumn(c,myTimeFmt)-t0)
# in case there are uncommented header lines skip them
SkipHeaderLines = 0
set multiplot layout 2,1
set format x "%Y\n%m-%d\n%H:%M" time
plot $Data u (timecolumn(1,myTimeFmt)):3 skip SkipHeaderLines w l ti "absolute time"
set format x "%tH:%tM" time
plot t=0 $Data u (Normalize(1)):3 skip SkipHeaderLines w l ti "relative time"
unset multiplot
### end of code
Result:

Separate key (legend) for colors and markers

I have a plot with several types of objects (each read from a separate file). I'm plotting the same several functions for all of them, all on the same graph (same X-axis).
I set the markers (pt) explicitly for each, and the color (lc), so the same object has the same marker, but the same function has the same color. As an example we have 2 files, one for each object (| is just to separate the files here):
0 0 0 | 0 1 1
1 1 2 | 1 1 2
Let's call the left file A, the right B. Column 1 in each file is the x axis, column 2 is using 1:2, and column 3 is using 1:3. So using the above files in an interactive session:
gnuplot> plot "A" using 1:2 with lp pt 1 lc 'black'
gnuplot> replot "A" using 1:3 with lp pt 1 lc 'red'
gnuplot> replot "B" using 1:2 with lp pt 2 lc 'black'
gnuplot> replot "B" using 1:3 with lp pt 2 lc 'red'
we get:
Is it possible to have the key separated, so A/B appear next to their respective marker, and the function name ("using...") appears next to a line (or anything) with the appropriate color?
Right now by omitting titles (notitle in the plot command) I can get one or the other, though I would have to settle on some uniform arbitrary marker/color (depending on what I chose to set as key). Can I:
Get two keys somehow? - Preferably setting the missing attribute (color or marker) to something not in the plot.
If not, can I customize a manual legend somehow?
I am not fully sure what you want to achieve, nevertheless as for the splitting of the key, I don't think that Gnuplot has some "out-of-the-box" feature for this. However, you could (ab)use multiplot to achieve this effect. The idea is basically to generate two overlapping plots - one with points and one with lines - and to position the keys independently:
set terminal pngcairo rounded font ",16"
set output 'fig.png'
$A << EOD
0 0 0
1 1 2
EOD
$B << EOD
0 1 1
1 1 2
EOD
set multiplot
set xtics out nomirror
set ytics out nomirror
eps = 0.1
set lmargin at screen eps
set rmargin at screen 1 - eps/2
set bmargin at screen eps
set tmargin at screen 1 - eps/2
#common key settings
set key left top Left reverse spacing 1.5
set key at screen 0.1,screen 1-eps
plot \
$A u 1:2 with p ps 1.5 pt 1 lc 'black' t 'A', \
$A u 1:3 with p ps 1.5 pt 1 lc 'red' t 'A' , \
$B u 1:2 with p ps 1.5 pt 2 lc 'black' t 'B', \
$B u 1:3 with p ps 1.5 pt 2 lc 'red' t 'B'
unset border; unset xtics; unset ytics
set key at screen 0.3,screen 1-eps
plot \
$A u 1:2 with l lc 'black' t 'using 1:2', \
$A u 1:3 with l lc 'red' t 'using 1:3', \
$B u 1:2 with l lc 'black' t '', \
$B u 1:3 with l lc 'red' t ''
This would give you:

Error with graphics gnuplot

I'm programming a graph with axes (y "%" and x "date") and when less than 7 records have not shown well
These are the files
data.csv:
20-04-2016 96.8 95.8 100
21-04-2016 97.07 97.99 100
22-04-2016 98.05 99.5 100
23-04-2016 98.64 97.88 100
24-04-2016 98.96 98.99 100
25-04-2016 98.68 98.11 100
graph
reset
set terminal png size 1200,500
#set xlabel "Time (days)"
set xdata time
set timefmt "%d-%m-%Y"
set ylabel "Total Petitions (%)"
set yrange [95:100]
set title "Akamai"
set key reverse Left outside
set grid
set style data linespoints
plot "datos.csv" using 1:2 title "One (%)", \
"" using 1:3 title "Two (%)", \
"" using 1:4 title "Ideal (%)"
He shows me this
As shown, the dates are repeated, and I want one date per record appears.
I also like to put the date
%d-%m\n%Y
This is solved if I put 7 or more records
And this happens if I put the full date but the description of the axis x
It could put the date vertically?
Thank you very much for your help
Use set format x;
set terminal pngcairo enhanced color dashed \
rounded size 1200,500
set title "Akamai"
set xlabel "date"
set xdata time
set timefmt "%d-%m-%Y"
set format x "%d/%m\n%Y"
set xtics 86400
set nomxtics
set ylabel "Total Petitions (%)"
set yrange [95:100]
set style data linespoints
set grid
set key right bottom
set output 'user1847844.png'
plot "datos.csv" using 1:2 title "One (%)", \
"" using 1:3 title "Two (%)", \
"" using 1:4 title "Ideal (%)"
Internally, gnuplot converts datetimes to seconds. So the line set xtics 86400 basically says to make one tick per day since one day has 24*60*60 = 86400 seconds. The set nomxtics tells gnuplot to not show minor tick marks on the x-axis.
This gives:
If you want to rotate the label, use:
set format x "%d/%m/%Y"
set xtics rotate 86400
That should format the dates as a single line and rotate the labels by 90 degrees.
Update:
Note that I'm using the pngcairo terminal. This is based on the cairo graphics library. This is designed to give consistent output on all output media. But gnuplot must be compiled with cairo support for this to work.
In general, I prefer the pdfcairo terminal. This produces PDF output which can be scaled without giving pixellation effects.
Update 2:
To get consistency in my graphs, I've defined certain styles in my gnuplotrc, which I use in my graphs. The contents of my gnuplotrc are shown below.
set encoding utf8
# See https://github.com/Gnuplotting/gnuplot-palettes
# Line styles (colorbrewer Set1)
set style line 1 lc rgb '#E41A1C' pt 1 ps 1 lt 1 lw 2 # red
set style line 2 lc rgb '#377EB8' pt 6 ps 1 lt 1 lw 2 # blue
set style line 3 lc rgb '#4DAF4A' pt 2 ps 1 lt 1 lw 2 # green
set style line 4 lc rgb '#984EA3' pt 3 ps 1 lt 1 lw 2 # purple
set style line 5 lc rgb '#FF7F00' pt 4 ps 1 lt 1 lw 2 # orange
set style line 6 lc rgb '#FFFF33' pt 5 ps 1 lt 1 lw 2 # yellow
set style line 7 lc rgb '#A65628' pt 7 ps 1 lt 1 lw 2 # brown
set style line 8 lc rgb '#F781BF' pt 8 ps 1 lt 1 lw 2 # pink
# Palette
set palette maxcolors 8
set palette defined ( 0 '#E41A1C', 1 '#377EB8', 2 '#4DAF4A', 3 '#984EA3',\
4 '#FF7F00', 5 '#FFFF33', 6 '#A65628', 7 '#F781BF' )
# Standard border
set style line 11 lc rgb '#808080' lt 1 lw 3
set border 0 back ls 11
set tics out nomirror
# Standard grid
set style line 12 lc rgb '#808080' lt 0 lw 1
set grid back ls 12
unset grid
And I use a pdfcairo terminal as standard, like this.
set terminal pdfcairo enhanced color dashed font "Alegreya, 14" \
rounded size 16 cm, 9.6 cm
The font used is an example. For graphs in reports I tend to use the same font as the body text.
The plot command uses the defined styles.
plot "user1847844.d" using 1:2 ls 1 title "One (%)", \
"" using 1:3 ls 2 title "Two (%)", \
"" using 1:4 ls 3 title "Ideal (%)"
This produces a figure looking like this.
Note: Since StackOverflow doesn't accept PDF images, I converted it to a PNG using ImageMagick.
convert -density 300 <input.pdf> -quality 100 -sharpen 0x1.0 <output.png>

values at x-axis fall in between xtics

I am trying to plot a graph where the x-values fall in between xtics.
For example, I want my xtics to be
C72 C73 C74 C75 C76 C77 C78 C79 C80 C81
and the points fall in between C72 C73 ; C73 C74 ; C74 C75 ; and so on.
My data points are
> 2.5 0.17509 C72
> 3.5 0.220434 C73
> 4.5 0.164918 C74
> 5.5 0.172477 C75
> 6.5 0.156145 C76
> 7.5 0.171699 C77
> 8.5 0.165199 C78
> 9.5 0.191207 C79
> 10.5 0.211656 C80
> 11.5 0.202233 C81
I used xticlabels() in the script definitions as below:
#OUTPUT
set terminal pngcairo size 650,450 enhanced dash
set output "xplot_gauche_malto-thermo.png"
set style line 4 lt 4 lw 10 # Please DISABLE pause -1
#MICRO
set macro
labelFONT="font 'arial,22'"
scaleFONT="font 'arial,18'"
scaleFONT2="font 'arial,18'"
keyFONT="font 'arial,18'"
# AXIS DEFINITIONS
set xrange [0:12]
set yrange [0:0.8]
set xtic (2,3,4,5,6,7,8,9,10,11) #scaleFONT2
set ytic #scaleFONT
set boxwidth 0.8
set size square
#PLOT
plot "all_dihedrals_in_layers_malto.dat" using 1:2:xticlabels(3) with linespoints lw 2 linecolor rgb "black" pointtype 1 pointsize 2 title ""
If I use the code as above, to get a plot using only column 1 and 2 from data file (as given above) I get the points fall in between 2-3, 3-4, 4-5 and so on.
Unfortunately if I use "xticlabels()", I don't get the graph as I wanted where the point supposed to fall in between C72-C73, C73-C74, C74-C75 and so on.
Appreciate in advance for any help.
Thanks
try something like this.. (Untested i dont have gnuplot on this machine..)
plot "all_dihedrals_in_layers_malto.dat" using 1:2 with linespoints \
lw 2 linecolor rgb "black" pointtype 1 pointsize 2 title "" ,\
"all_dihedrals_in_layers_malto.dat" using ($1-.5):0/0:xticlabels(3)
of course you could alternately manually key in the labels on the set xtics line..
Edit..had a chance toi try it, the 0/0 or (0/0) does not work. What you need to do is plot some value out of range.. eg:
set yrange [0:]
plot "all_dihedrals_in_layers_malto.dat" using 1:2 with linespoints \
lw 2 linecolor rgb "black" pointtype 1 pointsize 2 title "" ,\
"all_dihedrals_in_layers_malto.dat" using ($1-.5):-1:xticlabels(3) notitle

Thicker lines in the legend of gnuplot

I'm plotting some data curves with gnuplot, and they look like this:
However, the line samples in the legend are too thin. When you have more curves, it becomes hard to distinguish the colors. You can increase the thickness of the curves using "linewidth", e.g., by adding "lw 3" to the plot command, and you'd get this:
However, this increases the thickness everywhere. Is it possible to make the lines thick in the legend only? I know it can be done "the other way", by postprocessing on the output .png file. But is there a direct approach, using some gnuplot setting/wizardry?
Unfortunately, I don't know a way to control the thickness of the lines in the key, since they correspond to the lines being drawn. You can see what you can change by typing help set key in gnuplot.
Using multiplot, you can draw the plot lines first without the key, then draw the key again for 'ghost lines'. Here is a code sample which would do that:
set terminal png color size 800,600
set output 'plot.png'
set multiplot
unset key
plot '../batteries/9v/carrefour.txt' w lp, \
'../batteries/9v/philips.txt' w lp, \
'../batteries/9v/sony.txt' w lp
set key; unset tics; unset border; unset xlabel; unset ylabel
plot [][0:1] 2 title 'Carrefour' lw 4, \
2 title 'Philips' lw 4, \
2 title 'Sony' lw 4
In the second plot command, the function 2 (a constant) is being plotted with a y range of 0 to 1, so it doesn't show up.
I ran across this post and it gave me a critical idea.
The provided solution does not work in multiplot mode, since the second plot command will trigger the second plot, which is most likely not desired.
as a workaround one can set the original data as "notitle", then plot data outside of range with the same linetype and color in different thickness with the desired title. I'll just leave my current example here. It also includes linestyles that i have declared. So i just use the same linestyle (ls) to get the same color but change the thickness on the second line.
# for pngs
set terminal pngcairo size 1600,600 font ',18' enhanced
set output "pic_multi_kenngr_ana.png
set style line 2 lc rgb '#0ce90b' lt 1 lw 1.5 # --- green
set style line 3 lc rgb '#09e0b3' lt 1 lw 1.5 # .
set style line 4 lc rgb '#065fd8' lt 1 lw 1.5 # .
set style line 5 lc rgb '#4e04cf' lt 1 lw 1.5 # .
set style line 6 lc rgb '#c702a9' lt 1 lw 1.5 # .
set style line 7 lc rgb '#bf000a' lt 1 lw 1.5 # --- red
set multiplot layout 1,2
set xtics rotate
set tmargin 5
set xtics 12
set grid xtics
# set axis labels
set ylabel 'T [K]'
set xlabel 'Zeit [h]'
# select range
set xrange [0:48]
set yrange [290.15:306.15]
set title "(a) Bodentemperatur"
set key top right Right
plot 'par_crank_hom01lvls.04.dat' u 1:3 with lines ls 7 notitle,\
'par_crank_str01lvls.16.dat' u 1:3 with lines ls 2 notitle,\
500 t 'z = 4 cm' ls 7 lw 4,\
500 t 'z = 16 cm' ls 2 lw 4
################################################
set title "(b) Bodenwärmestrom an der Oberfläche"
set ylabel 'G [W m^{-2}]'
set yrange[-110:110]
unset key
plot 'par_crank_str01_ghf.dat' u 1:3 with lines
unset multiplot
I hope this will help someone
An even more simple work-around (imho) is to define the colours explicitly and plot each line twice, once with high lw for the key and also with the title to appear in the key, but adding "every ::0::0" which effectively ends up in plotting nothing, and once the normal way. See the following code snippet:
plot data u 0:1 w l linecolor rgb #1b9e77 lw 2 t "",\
data every ::0::0 u 0:1 w l linecolor rgb #1b9e77 lw 4 t "Title"
To expand on the NaN comment by #Svalorzen, the following will graph two lines of width 1 from some datafile.txt with no titles and create matching blank lines with the specified titles and width 5 for the key only:
plot [][]\
NaN title "Title1" w line lt 1 lc 1 lw 5,\
NaN title "Title2" w line lt 1 lc 2 lw 5,\
"datafile.txt" using 1:2 title "" w line lt 1 lc 1 lw 1,\
"datafile.txt" using 1:3 title "" w line lt 1 lc 2 lw 1
I find an answer for this:
Set key linewidth
in your case, must be:
plot '../batteries/9v/carrefour.txt' w l lw 1 linetype 1 notitle, 0/0 linetype 1 linewidth 5 title 'Carrefour'
rep '../batteries/9v/philips.txt' w l lw 1 linetype 2 notitle, 0/0 linetype 2 linewidth 5 title 'Philips'
rep '../batteries/9v/sony.txt' w l lw 1, linetype 3 notitle, 0/0 linetype 3 linewidth 5 title 'Sony'
Try something like:
plot # ... \
keyentry w l lw 1 lc 2 t "Title" # ...
And remove the old keys.

Resources