# Legend entry not showing correct image

I don't know why the legend does not reflect the correct image for its respective entry. I want the last two entries to just show the marker and not the line (dashed line). I set the last two to just be markers (diamond and triangle) but they are not shown in the legend. It works fine for the first entry (data points). Please help. Thanks in advance.

\begin{figure}[!htbp]
\captionsetup{width=0.9\textwidth}
\centering
\begin{tikzpicture}[trim left]
\pgfplotsset{width=\textwidth,
compat=1.3,
legend style={font=\footnotesize}}
\begin{axis}[
scaled y ticks = false,
xlabel={concentration[$ppm$]},
ylabel={Area$\times 10^{3}$},
legend cell align=left,
legend pos=north west,
xmin=0, ymin=0,
ignore zero=y]
X Y
0   8.046
5   302.329
10  606.470
20  1122.1465
30  1666.8665
};
\addplot[no marks] table[y={create col/linear regression={y=Y}}] % compute a linear regression from the
%input table
{
X Y
0   8.046
5   302.329
10  606.470
20  1122.1465
30  1666.8665
};
$\pgfmathprintnumber{\pgfplotstableregressiona} \cdot x \pgfmathprintnumber[print sign]{\pgfplotstableregressionb}$ lin. regression}
X Y
1.390942009 162.4025826
5 360.5841458
10 635.1454906
20 1184.26818
30 1733.39087
};
X Y
1.390942009 44.98118766
5 243.1627508
10 517.7240956
20 1066.846785
30 1615.969475
};
\addplot[no marks, dotted, very thick] table[x=X,y=Y]{
X Y
0 103.6918851
1.390942009 103.6918851
};
\addplot[no marks, dotted, very thick] table[x=X,y=Y]{
X Y
1.390942009 103.6918851
1.390942009 1.390942009
};
\addplot[only marks, mark=triangle, mark size=4] table[x=X,y=Y]{
X Y
10.40635194 598.7485
};
\addplot[only marks, mark=diamond, mark size=4] table[x=X,y=Y]{
X Y
13.1795209 751.0295
};
\node at (axis cs:1.8,60) [anchor=west] {\textbf{LoD}};
\end{axis}
\end{tikzpicture}
\caption{Linear regression curve for caffeine using HPLC analysis, Limit of detection (LoD) also shown}
\end{figure}

• Can you add your preamble as well? – Alenanno Jun 28 '15 at 13:07

You have to reverse the order of the plots to get the right curves at the legend entries. \addlegendentry adds a legend entry for each plot, plot after plot (so, in your example for the two dashed curves).

I also added some code so that the code compiles...

\documentclass{standalone}
\usepackage{pgfplots}
\usepackage{pgfplotstable}

\begin{document}

\begin{tikzpicture}[trim left]
\pgfplotsset{width=\textwidth,
compat=1.3,
legend style={font=\footnotesize}}
\begin{axis}[
scaled y ticks = false,
xlabel={concentration[$ppm$]},
ylabel={Area$\times 10^{3}$},
legend cell align=left,
legend pos=north west,
xmin=0, ymin=0,
%ignore zero=y
]
X Y
0   8.046
5   302.329
10  606.470
20  1122.1465
30  1666.8665
};
\addplot[no marks] table[y={create col/linear regression={y=Y}}] % compute a linear regression from the
%input table
{
X Y
0   8.046
5   302.329
10  606.470
20  1122.1465
30  1666.8665
};
$\pgfmathprintnumber{\pgfplotstableregressiona} \cdot x \pgfmathprintnumber[print sign]{\pgfplotstableregressionb}$ lin. regression}
X Y
1.390942009 162.4025826
5 360.5841458
10 635.1454906
20 1184.26818
30 1733.39087
};
\addplot[only marks, mark=triangle, mark size=4] table[x=X,y=Y]{
X Y
10.40635194 598.7485
};
\addplot[only marks, mark=diamond, mark size=4] table[x=X,y=Y]{
X Y
13.1795209 751.0295
};
X Y
1.390942009 44.98118766
5 243.1627508
10 517.7240956
20 1066.846785
30 1615.969475
};
\addplot[no marks, dotted, very thick] table[x=X,y=Y]{
X Y
0 103.6918851
1.390942009 103.6918851
};
\addplot[no marks, dotted, very thick] table[x=X,y=Y]{
X Y
1.390942009 103.6918851
1.390942009 1.390942009
};
\node at (axis cs:1.8,60) [anchor=west] {\textbf{LoD}};
\end{axis}
\end{tikzpicture}

\end{document}

• great, thanks a ton! a side question, is there a way to multiply the slope of my regression by factor of 1000? i had to scale down the numbers so the y-axis looked easier to read, but for the slope I need it in its original scale. so instead of 54.91x i need it to be 54910 or 54.91x10^3, thanks again – Spencer Trinh Jun 28 '15 at 14:19
• i think, pgfplotstable can help you here... You can put your data in a table or a file, and when you plot it, do some calcuations... a very short example (with a file) would be something like \pgfplotstableread{./data.txt} \datatable \addplot table [x expr = \thisrow{freq}/1e9] from \datatable {}; – Faekynn Jun 28 '15 at 14:50
• @SpencerTrinh In your case mabe (untested): \addplot[only marks] table[x=X,y expr =\thisrow{Y}*1e3] – Faekynn Jun 28 '15 at 14:57
• \addplot[no marks] table[x=X, y={create col/linear regression={y expr=\thisrow{Y}/1e3}}]; is this how to incorporate it for the linear regression? latex doesn't like this. is there a way to multiply or divide the pgfplotsregressiona variable by a number than report it in the legend? – Spencer Trinh Jun 29 '15 at 12:27
• @SpencerTrinh maybe you could add the following two options to your axis-environment: scaled y ticks=real:1e3, and ytick scale label code/.code={$\cdot 10^3$}. Then you can use the real values in the tables, there's no need for a manual factor on the y-axis, and the regression equation is right... – Faekynn Jun 29 '15 at 13:55