Konstantin Weigmann
Konstantin Weigmann

Reputation: 47

Gnuplot - How to make a legend with linespoints?

I've been playing around with the code that was posted here. Then, I was trying to add a legend with linespoints. This resulted in having the fit line being covered with points.

I've tried to set the title at different positions. But nothing resulted in what I was looking for. Shown in the pictureResults of 'with linespoints' is the legend as I would want it. But the graph doesn't show only the data points but also points of the fit where I would like to have just the line. If it matters, I've set up the code in LaTeX using 'gnuplottex' using gnuplot 5.4.

\documentclass{scrartcl}

\usepackage[ngerman]{babel} % German language
\usepackage[T1]{fontenc} % Use 8-bit encoding that has 256 glyphs

\usepackage{booktabs,array} % Packages for tables

\usepackage{siunitx}
\usepackage{amsmath} % For typesetting math
\usepackage{graphicx} % Required for including images
\usepackage{lmodern}

\usepackage[miktex]{gnuplottex} % External use of gnuplot
\usepackage{gnuplot-lua-tikz} % Use terminal 'tikz'
\usepackage{tikz} % To call tikz inside a gnuplot graph
\usetikzlibrary{tikzmark}


\begin{document}

\begin{figure}
\begin{gnuplot}[terminal=tikz, terminaloptions={color size 10cm,7cm}]
reset session
datafile = 'data.dat'
set multiplot layout 2,1
set print 'parameters.dat'

#____________________Set axis style________________________________


#________________Change the style lines____________________________
set style line 2 lc rgb '#000000' pt 7 lt 7 lw 1 # black 
set style line 3 lc rgb '#000000' pt 21 lt 7 lw 1 # black
set style line 4 lc rgb '#000000' pt 8 lt 7 lw 1 # black
set style line 5 lc rgb '#000000' pt 9 lt 7 lw 1 # black
set style line 6 lc rgb '#000000' pt 10 lt 7 lw 1 # black
set style line 7 lc rgb '#000000' pt 11 lt 7 lw 1 # black
set style line 8 lc rgb '#000000' pt 4 lt 7 lw 1 # black
set style line 9 lc rgb '#000000' pt 5 lt 7 lw 1 # black

#_____________Set the label for data points________________________
set key at 10.5,60                      # set position of legend
set key Left                                # set raggedleft
set key samplen 1 spacing 1.2 font ",8" # set fontsize and spacing
set key autotitle columnheader          # columnheader = legend

###1__________Define function and number of columns_________________________
f(x,a,b,c) = a*(x-b)**2 + c
colMin = 2
colMax = 5
set fit quiet nolog
array A[colMax]
array B[colMax]
array C[colMax]

do for [col=colMin:colMax] {
    a=1; b=1; c=1             # some initial values, sometimes 0 or NaN is not a good start
    fit f(x,a,b,c) datafile u 1:col via a,b,c
    A[col] = a;  B[col] = b;  C[col] = c
    
    print sprintf ('y%d: %.4f %.4f %.4f',col-1,A[col],B[col],C[col])
}

plot for [col=colMin:colMax] datafile u 1:col notitle ls col, \
     for [col=colMin:colMax] f(x,A[col],B[col],C[col]) ls col dt col \
         with linespoints title sprintf("$y%d$: a=%.2f, b=%.2f, c=%.2f",col-1,A[col],B[col],C[col])

\end{gnuplot}
\end{figure}

\end{document}

Upvotes: 0

Views: 192

Answers (1)

theozh
theozh

Reputation: 25843

In the legend I'd like to see the linespoints but not in the plot

Well, the basic idea of the legend is to show what you see in the plot. In your case, I assume you want to combine the original data (as filled points) and the fit (as line) into one legend entry since the points should be on (or near) the line.

With your suggestion pointinterval 100 or whatever higher number for plotting the fit with linespoints, you will always see the first point.

Alternative suggestion: check help keyentry. With this, you are flexible and independent what you put into the key/legend, e.g. empty circle pt 6 or filled circle pt 7, etc.

Script:

### different key/legend enty with keyentry
reset session

$Data <<EOD
1   2   4   2
2   5   1   3
3  10   0   6
4  17   1  11
5  26   4  18
EOD

f(x,a,b,c) = a*x**2 + b*x + c
N = 3
array A[N]; array B[N]; array C[N]
set fit  quiet nolog
do for [i=1:3] {
    a = b = c = 1   # initialize values
    fit f(x,a,b,c) $Data u 1:i+1 via a,b,c
    A[i] = a; B[i] = b; C[i] = c 
}

set key noautotitle out 

plot for [col=2:4] $Data u 1:col w p pt 7 lc col-1, \
     for [i=1:3] f(x,A[i],B[i],C[i]) w l lc i, \
     for [i=1:3] keyentry w lp pt 6 lc i ti sprintf("%d: a=%.2f, b=%.2f, c=%.2f",i,A[i],B[i],C[i])
### end of script

Result:

enter image description here

Upvotes: 1

Related Questions