Gnuplot line and key colors - gnuplot

I'm trying to use Gnuplot to create a line chart. Each line is represented by a different color. What I want is the key has the same color as the line color. This is what I have right now, current version. Is it possible to set the text 'Line 2' colored as orange, 'Line 3' colored as red, etc?
This is what I wrote in the gp file:
set xlabel'x-axis'; \
set xrange[0:25];\
set ylabel 'y-axis';\
set yrange [2:9];\
set key left top;
p 'test.dat' using 1:2 w linespoints lw 5 lc rgb '#aadc32' pt 17 title 'Line 1' ,\
'test.dat' using 1:9 w linespoints lw 5 lc 'orange' lt 1 title 'Line 2',\
'test.dat' using 1:6 w linespoints lw 5 lc 'red' lt 8 title 'Line 3',\
'test.dat' using 1:7 w linespoints lw 5 lc 'violet' pt 6 title 'Line 4',\'test.dat' using 1:8 w linespoints lw 5 lc rgb '#b5367a' pt 19 title 'Line 5',\
'test.dat' using 1:3 w linespoints lw 5 lc 'cyan' pt 9 title'Line 6',\
'test.dat' using 1:4 w linespoints lw 5 lc 'blue' lt 9 title 'Line 7',\
'test.dat' using 1:10 w linespoints lw 5 lc rgb '#1c1044' lt 5 title 'Line 8',\
Thank you so much.

Unfortunately not. The plot title does not (now, gp5.2pl0) recognise the "textcolor" specifier.
plot x lw 3 lc rgb "blue" title "x" tc rgb "blue" # doesn't work
You can only print an empty (" ") plot title, and overprint it with a coloured label
set label 1 at 8,1 "bluetitle" tc rgb "blue"
That requires some fiddling with the placement of the label, of course.
You might put up a feature request on https://sourceforge.net/p/gnuplot/feature-requests/

Related

Can I put comments into long plot commands?

My gnuplot plot command is
tM=1.0*(a0*xmax+ymax)
set parametric
set trange [-0.2:tM]
plot 1/0,1/0 title '\ict\qquad' lc rgb 'gray80' dt 5 lw 2,\
uldx0(t),uldy0(t) title '\disct' lc rgb 'web-green' lw 3,\
hpx0(t),hpy0(t) lc rgb 'pink' lw 2,\
vpx0(t),vpy0(t) with filledcurve xy= 0.5*(xmi0+xpl0),0\
fs transparent solid 0.25 fc rgb 'cyan' ,\
for [n=1:5] ix0(n*0.05+t*n*0.005/tM ,u0(0,n*0.05)),\
iy0(n*0.05+t*n*0.005/tM,u0(0,n*0.05))\
lc rgb 'gray80' dt 5 lw 2,\
for [n=6:10] ix0(n*0.05+t*n*0.01/tM ,u0(0,n*0.05)),\
iy0(n*0.05+t*n*0.01/tM,u0(0,n*0.05))\
lc rgb 'gray80' dt 5 lw 2,\
for [n=22:36] ix0(n*0.025+t ,u0(0,n*0.025)),\
iy0(n*0.025+t,u0(0,n*0.025))\
lc rgb 'gray80' dt 5 lw 2,\
x0(t*pmax0(q1)/tM,q1), y0(t*pmax0(q1)/tM,q1) \
lc rgb q1color lw 2,\
x0(t*pmax0(ql0)/tM,ql0), y0(t*pmax0(ql0)/tM,ql0) \
lc rgb qlcolor lw 2,\
x0(t*pmax0(QqxyL0)/tM,QqxyL0), y0(t*pmax0(QqxyL0)/tM,QqxyL0) \
lc rgb qhatmicolor lw 2,\
x0(t*pmax0(q2)/tM,q2) , y0(t*pmax0(q2)/tM,q2) \
lc rgb q2color lw 2,\
x0(t*pmax0(qpl0)/tM,qpl0), y0(t*pmax0(qpl0)/tM,qpl0) \
lc rgb qplcolor lw 2,\
x0(t*pmax0(QqxyR0)/tM,QqxyR0), y0(t*pmax0(QqxyR0)/tM,QqxyR0) \
lc rgb qhatplcolor lw 2
Is there a good way to break this up with comments which would also help may should I decide to look at this later?
All inside a single plot command, right?
I think the closest is to take advantage of the fact that each clause of a plot command can start with a definition. The intended use is something like plot a=2 x**a, but you could pick some junk variable and assign it to a comment string
plot \
_c="Straight line" x title _c, \
_c="Parabola" x**2 title 'x^2', \
_c="Some weird function" real(sinh(sqrt(x))) title 'squint(x)'
If you like you can use that comment string in the plot title, which is something close to the originally intended use. But also you can just ignore it in the plot clause. It would then only be relevant to someone reading the source, not to the output from the plot command.

Gnuplot combing multiple value types in one line graph with different colours and dashes

I have three different lines, where colours and their dashes means different things which I want on one plot, instead of three. How do I accomplish that?
set datafile separator comma
$sample <<EOD
2020-01-01,4,UK,Business
2020-02-01,4,UK,Business
2020-01-01,4,UK,Social
2020-02-01,15,UK,Social
2020-01-01,1,USA,Social
2020-02-01,25,USA,Social
EOD
set format x '%Y'
set xdata time
set timefmt "%Y-%m-%d"
plot '$sample' u 1:2 title "UK/Business" with linespoints dt 3 lw 5 pt 7 lc "red"
plot '$sample' u 1:2 title "USA/Social" with linespoints dt 3 lw 1 pt 7 lc "blue"
plot '$sample' u 1:2 title "UK/Social" with linespoints dt 3 lw 5 pt 7 lc "blue"
E.g. blue is "Social" and lw 1 (fine dots) is for USA.
Please check the manual or help plot. Plot elements for the same plot are separated by comma.
Syntax:
plot {<ranges>} <plot-element> {, <plot-element>, <plot-element>}
To improve readability you can split long code lines into several lines by using \. Note that no character is allowed after \ in the same line except carriage return/line feed.
Try this:
plot '$sample' u 1:2 title "UK/Business" w lp dt 3 lw 5 pt 7 lc "red", \
'' u 1:2 title "USA/Social" w lp dt 3 lw 1 pt 7 lc "blue", \
'' u 1:2 title "UK/Social" w lp dt 3 lw 5 pt 7 lc "blue"
Addition: (a filter depending on two (string)-columns)
You can implement a filter by using the ternary operator. Check help ternary and help strcol. A value which does not pass the filter will be set to NaN. If you still want to your points being connected with lines, you need to set datafile missing NaN.
Code:
### filtered data with different line colors
reset session
set datafile separator comma
$sample <<EOD
2020-01-01,4,UK,Business
2020-02-01,4,UK,Business
2020-01-01,4,UK,Social
2020-02-01,15,UK,Social
2020-01-01,1,USA,Social
2020-02-01,25,USA,Social
EOD
myTimeFmt = "%Y-%m-%d"
set format x '%d.%m.%Y' timedate
set key top left
set datafile missing NaN
myFilter(colData,colFilter1,key1,colFilter2,key2) = (strcol(colFilter1) eq key1) && (strcol(colFilter2) eq key2) ? column(colData) : NaN
plot $sample u (timecolumn(1,myTimeFmt)):(myFilter(2,3,"UK",4,"Business")) w lp dt 3 lw 5 pt 7 lc "red" title "UK/Business", \
'' u (timecolumn(1,myTimeFmt)):(myFilter(2,3,"USA",4,"Social")) w lp dt 3 lw 1 pt 7 lc "blue" title "USA/Social" , \
'' u (timecolumn(1,myTimeFmt)):(myFilter(2,3,"UK",4,"Social")) w lp dt 3 lw 5 pt 7 lc "blue" title "UK/Social"
### end of code
or a bit shortened if the columns 2,3,4 do not change within the plot command...
...
...
myTime(col) = timecolumn(col,myTimeFmt)
myFilter(key1,key2) = (strcol(3) eq key1) && (strcol(4) eq key2) ? column(2) : NaN
plot $sample u (myTime(1)):(myFilter("UK","Business")) w lp dt 3 lw 5 pt 7 lc "red" title "UK/Business", \
'' u (myTime(1)):(myFilter("USA","Social")) w lp dt 3 lw 1 pt 7 lc "blue" title "USA/Social" , \
'' u (myTime(1)):(myFilter("UK","Social")) w lp dt 3 lw 5 pt 7 lc "blue" title "UK/Social"
Result:

How to make key colour black in Gnuplot

How can I make the key "symbols" black in colour when using palettes?
I think you cannot control this directly, here a workaround:
plot 'MOD1.dat' u 2:3:1 w p pt 7 ps 2 lt black, \
'MOD1.dat' u 2:3:1 w p pt 7 ps 2 lt palette notitle
So, we first plot the data without legend in black, then plot the data points but no legend. The nice thing about this approach is that there is no need to fix the x or y range.
I would just create a dummy line and use notitle on the real lines. Something like
set yrange[0:1]
plot "realdata1.data" u 1:2 w linespoints lt 1 notitle , \
"realdata2.data" u 1:2 w linespoints lt 2 notitle , \
1/0 w linespoints lt 1 lc rgb 'black' title 'Model1', \
1/0 w linespoints lt 1 lc rgb 'black' title 'Model2'
Then replace lt 1 with whatever you have used for style in your current plot. Note that you'll need to use set yrange for gnuplot to accept the dummy curve

when plotting several data file, last files are abandon from plotting in gnuplot

My code in gnuplot (**.p file) is:
plot '3exp0346.dat' u 1:2 w lp lt 7 lc rgb 'black' lw 2 title 'Present study' ,\
'3exp0346.dat' u 1:2 w p pt 4 lc rgb 'blue' title 'Exp in [3]' ,\
'3num0346.dat' u 1:2 w p pt 5 lc rgb 'blue' title 'Num in [3]' ,\
'2num0346.dat' u 1:2 w p pt 6 lc rgb 'red' title 'Num in [2]' ,\
'3exp02306.dat' u 1:2 w lp lt 7 lc rgb 'black' lw 2 ,\
'3exp02306.dat' u 1:2 w p pt 4 lc rgb 'blue' ,\
'3num02306.dat' u 1:2 w p pt 5 lc rgb 'blue' ,\
'2num02306.dat' u 1:2 w p pt 6 lc rgb 'red' ,\
'3exp0173.dat' u 1:2 w lp lt 7 lc rgb 'black' lw 2 ,\
'3exp0173.dat' u 1:2 w p pt 4 lc rgb 'blue' 2 ,\
'3num0173.dat' u 1:2 w p pt 5 lc rgb 'blue' ,\
'2num0173.dat' u 1:2 w p pt 6 lc rgb 'red' ,\
'3exp00865.dat' u 1:2 w lp lt 7 lc rgb 'black' lw 2 ,\
'3exp00865.dat' u 1:2 w p pt 4 lc rgb 'blue' ,\
'3num00865.dat' u 1:2 w p pt 5 lc rgb 'blue' ,\
'2num00865.dat' u 1:2 w p pt 6 lc rgb 'red'
Loading this code on gnuplot console following error appears
"meltcomp.p", line 28: ';' expected
then, Closing the console, graph will plot like this:
But last 4 data files are not present.
What would be problem in this case?
problem was 11 line. I am not allowed to leave comment.
'3exp0173.dat' u 1:2 w p pt 4 lc rgb 'blue' 2 ,\
in which 2 after color command makes problem. 2 shouldn't be there.

how to define X's label on gnuplot

This is My data :
18_AGT_s 8234.00 8234.00 8234.00
18_MAC_s 8414.36 8308.36 8246.33
9_MAC_r 8414.36 8308.36 8246.33
9_MAC_s 8414.55 8309.55 8246.45
8_MAC_r 8414.55 8309.55 8246.45
8_MAC_s 8414.56 8310.08 8246.47
6_MAC_r 8414.56 8310.08 8246.47
6_MAC_s 8416.19 8310.21 8246.49
1_MAC_r 8416.19 8310.21 8246.49
and here is my gnuplot code :
plot "dat" using ($0+1):2 with linespoints pt 8 ps 2 lt 2 lw 4 lc rgb
"green" title "DMSR","dat" using ($0+1):3 with linespoints pt 5 ps 2
lt 3 lw 4 lc rgb "blue" title "Alarm","dat" using ($0+1):4 with
linespoints pt 6 ps 2 lt 4 lw 4 lc rgb "red" title "Emergency"
and here is my out put :
But In the step part I want to have 18_AGT_s and 18_MAC_s and 9_MAC_s and .... for example on the X part I want to have 18_AGT_s in stand of 1 or I want to have 18_MAC_2 instand of 2 and 9_MAC_r instand of 3 etc. Any help thanks
Use the xticlabels() option with the column number with the labels as argument (1, in this case):
# Optionally rotate labels so they fit
set xtics rotate
plot "dat" using ($0+1):2 with linespoints pt 8 ps 2 lt 2 lw 4 lc rgb \
"green" title "DMSR","dat" using ($0+1):3 with linespoints pt 5 ps 2 \
lt 3 lw 4 lc rgb "blue" title "Alarm","dat" using \
($0+1):4:xticlabels(1) with linespoints pt 6 ps 2 lt 4 lw 4 lc rgb \
"red" title "Emergency"
Here you only need to use it for the last plot instance so that it overwrites the number options.

Resources