Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

how can i have this legend?

enter image description here

in this plot:

\documentclass[a4paper,12pt]{article}
\usepackage{pgfplots,pgfplotstable}        % Grafici
\pgfplotsset{/pgf/number format/use comma,compat=newest,%
            width=12cm,%
            height=9cm%
            }


%*********************************************************************************
% Impostazioni Pgfplots
%*********************************************************************************

\pgfplotstableread{
X Y
0.05 0.17
0.05 0.041
0.05 0.023
0.1 0.332
0.1 0.089
0.1 0.041
0.15 0.5
0.15 0.132
0.15 0.06
}\tableA

\pgfplotstablecreatecol[linear regression]
{regression}
{\tableA}

\xdef\slope{\pgfplotstableregressiona} %<-- might be handy occasionally
\xdef\intercept{\pgfplotstableregressionb}

\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}
\begin{axis}[/pgf/number format/fixed,
legend pos=outer north east,
grid=major,
xmin=0, xmax=0.2,
ymin=0, ymax=0.6,
point meta min={0},
point meta max={0.2},
yticklabel shift=2pt,
xticklabel shift=2pt,
legend pos=north west,
legend cell align=left,
minor tick num=4,
xticklabel style={text height=1.5ex},
xtick={0,0.05,...,0.3},
minor xtick={%
0.01,0.02,0.03,0.04,%
0.06,0.07,0.08,0.09,%
0.11,0.12,0.13,0.14,%
0.16,0.17,0.18,0.19},%
extra x ticks={0,0.05,...,0.3},
extra x tick style={
    xticklabel pos=right,
    xticklabel style={text depth=0pt}
},
extra y ticks={0,0.1,...,0.7},
extra y tick style={
    yticklabel pos=right
},
tick style={thin,black},
xlabel=\large $m$,
ylabel=\large $\Delta l$,
colorbar horizontal,
colorbar style={
    /pgf/number format/fixed,
    xticklabel shift=2pt,
    xtick={0,0.05,...,0.3},
    xticklabel style={text height=1.5ex}
},
axis line style={draw=none}, after end axis/.append code={\draw (rel axis cs:0,0) rectangle (rel axis cs:1,1);},
colormap={new}{color(0cm)=(violet);color(1cm)=(blue);color(2cm)=(cyan);color(3cm)=(green);color(4cm)=(yellow);color(5cm)=(orange);color(6cm)=(red)}
]
\addplot [point meta=explicit,
    scatter,
    mark=*,
    draw=none]
    table[meta=X] {\tableA};

\addplot [mesh,point meta=x,domain=0.025:0.175,very thick,samples=10] {\slope*x+\intercept};
\addlegendentry{%
$\pgfmathprintnumber{\pgfplotstableregressiona} \cdot x
\pgfmathprintnumber[print sign]{\pgfplotstableregressionb}$}
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}

UPDATE at Jake's answer:

\documentclass[border=0.5mm,12pt]{standalone}

\usepackage{pgfplots,pgfplotstable}        % Grafici

\pgfplotsset{%
            mesh line legend/.style={legend image code/.code=\meshlinelegend#1},%
            /pgf/number format/use comma,%
            compat=newest,%
%            height=9cm,%
            width=12cm%
}



%*********************************************************************************
% Impostazioni Pgfplots
%*********************************************************************************

\pgfplotstableread{
X Y
0.05 0.17
0.05 0.041
0.05 0.023
0.1 0.332
0.1 0.089
0.1 0.041
0.15 0.5
0.15 0.132
0.15 0.06
}\tableA

\pgfplotstablecreatecol[linear regression]
{regression}
{\tableA}

\xdef\slope{\pgfplotstableregressiona} %<-- might be handy occasionally
\xdef\intercept{\pgfplotstableregressionb}



%% Code for the coloured line legend
\makeatletter
\long\def\meshlinelegend#1{%
    \scope[%
        #1,
        /pgfplots/mesh/rows=1,
        /pgfplots/mesh/cols=4,
        /pgfplots/mesh/num points=,
        /tikz/x={(0.44237cm,0cm)}, 
        /tikz/y={(0cm,0.23932cm)},
        /tikz/z={(0.0cm,0cm)},
        scale=0.4,
    ]
    \let\pgfplots@metamax=\pgfutil@empty
    \pgfplots@curplot@threedimtrue

    \pgfplotsplothandlermesh
    \pgfplotstreamstart

    \def\simplecoordinate(##1,##2,##3){%
        \pgfmathparse{1000*(##3)}%
        \pgfmathfloatparsenumber\pgfmathresult
        \let\pgfplots@current@point@meta=\pgfmathresult
        \pgfplotstreampoint{\pgfqpointxyz@orig{##1}{##2}{##3}}%
    }%

    \simplecoordinate(0,0,0)
    \simplecoordinate(0.25,0,0.125)
    \simplecoordinate(0.5,0,0.25)
    \simplecoordinate(0.75,0,0.375)
    \simplecoordinate(1,0,0.5)
    \simplecoordinate(1.25,0,0.625)
    \simplecoordinate(1.5,0,0.75)
    \simplecoordinate(1.75,0,0.875)
    \simplecoordinate(2,0,1)

    \pgfplotstreamend
    \pgfusepath{stroke}
    \endscope
}%
\makeatother
%% End code for the coloured line legend



\begin{document}
\thispagestyle{empty}
%\begin{figure}
\centering
\begin{tikzpicture}
\begin{axis}[%
/pgf/number format/fixed,
legend pos=outer north east,
grid=major,
xmin=0, xmax=0.2,
ymin=0, ymax=0.6,
point meta min={0},
point meta max={0.2},
yticklabel shift=2pt,
xticklabel shift=2pt,
legend pos=north west,
legend cell align=left,
minor tick num=4,
xticklabel style={text height=1.5ex},
xtick={0,0.05,...,0.3},
minor xtick={%
0.01,0.02,0.03,0.04,%
0.06,0.07,0.08,0.09,%
0.11,0.12,0.13,0.14,%
0.16,0.17,0.18,0.19},%
extra x ticks={0,0.05,...,0.3},
extra x tick style={
    xticklabel pos=right,
    xticklabel style={text depth=0pt}
},
extra y ticks={0,0.1,...,0.7},
extra y tick style={
    yticklabel pos=right
},
tick style={thin,black},
xlabel=\large $m$,
ylabel=\large $\Delta l$,
colorbar horizontal,
colorbar style={
    /pgf/number format/fixed,
    xticklabel shift=2pt,
    xtick={0,0.05,...,0.3},
    xticklabel style={text height=1.5ex}
},
axis line style={draw=none}, after end axis/.append code={\draw (rel axis cs:0,0) rectangle (rel axis cs:1,1);},
colormap={new}{color(0cm)=(violet);color(1cm)=(blue);color(2cm)=(cyan);color(3cm)=(green);color(4cm)=(yellow);color(5cm)=(orange);color(6cm)=(red)}
]
\addplot [%
    point meta=explicit,
    forget plot,
    scatter,
    scatter/use mapped color={draw=mapped color,fill=mapped color},
    mark=*,
    draw=none]table[meta=X] {\tableA};

\addplot [mesh,mesh line legend,point meta=x,domain=0.02:0.18,thick,samples=10] {\slope*x+\intercept};%
\addlegendentry{%
$\pgfmathprintnumber{\pgfplotstableregressiona} \cdot x
\pgfmathprintnumber[print sign]{\pgfplotstableregressionb}$}
\end{axis}
\end{tikzpicture}
%\end{figure}
\end{document}

UPDATE this is an update to the percusse's answer

enter image description here

\documentclass[border=0.5mm,12pt]{standalone}

\usepackage{pgfplots,pgfplotstable}        % Grafici

\pgfplotsset{%
            colormap legend/.style={legend image code/.code={\path[draw=none,shading=tempshading,shade] (0cm,0mm) rectangle (0.5cm,0.8pt);}},
            /pgf/number format/use comma,%
            compat=newest,%
%            height=9cm,%
            width=12cm%
}


%*********************************************************************************
% Impostazioni Pgfplots
%*********************************************************************************

\pgfplotstableread{
X Y
0.05 0.17
0.05 0.041
0.05 0.023
0.1 0.332
0.1 0.089
0.1 0.041
0.15 0.5
0.15 0.132
0.15 0.06
}\tableA

\pgfplotstablecreatecol[linear regression]
{regression}
{\tableA}

\xdef\slope{\pgfplotstableregressiona} %<-- might be handy occasionally
\xdef\intercept{\pgfplotstableregressionb}

\begin{document}
\thispagestyle{empty}
%\begin{figure}
\centering
\begin{tikzpicture}
\begin{axis}[%
/pgf/number format/fixed,
legend pos=outer north east,
grid=major,
xmin=0, xmax=0.2,
ymin=0, ymax=0.6,
point meta min={0},
point meta max={0.2},
yticklabel shift=2pt,
xticklabel shift=2pt,
legend pos=north west,
legend cell align=left,
minor tick num=4,
xticklabel style={text height=1.5ex},
xtick={0,0.05,...,0.3},
minor xtick={%
0.01,0.02,0.03,0.04,%
0.06,0.07,0.08,0.09,%
0.11,0.12,0.13,0.14,%
0.16,0.17,0.18,0.19},%
extra x ticks={0,0.05,...,0.3},
extra x tick style={
    xticklabel pos=right,
    xticklabel style={text depth=0pt}
},
extra y ticks={0,0.1,...,0.7},
extra y tick style={
    yticklabel pos=right
},
tick style={thin,black},
xlabel=\large $m$,
ylabel=\large $\Delta l$,
colorbar horizontal,
colorbar style={
    /pgf/number format/fixed,
    xticklabel shift=2pt,
    xtick={0,0.05,...,0.3},
    xticklabel style={text height=1.5ex}
},
axis line style={draw=none}, after end axis/.append code={\draw (rel axis cs:0,0) rectangle (rel axis cs:1,1);},
colormap={new}{color(0cm)=(violet);color(1cm)=(blue);color(2cm)=(cyan);color(3cm)=(green);color(4cm)=(yellow);color(5cm)=(orange);color(6cm)=(red)}
]
\pgfplotscolormaptoshadingspec{new}{2cm}\result
\def\tempb{\pgfdeclarehorizontalshading{tempshading}{3cm}}%
\expandafter\tempb\expandafter{\result}%
\addplot [mesh,point meta=x,domain=0.02:0.18,thick,samples=1000,colormap legend] {\slope*x+\intercept};
\addlegendentry{%
$\pgfmathprintnumber{\pgfplotstableregressiona} \cdot x
\pgfmathprintnumber[print sign]{\pgfplotstableregressionb}$}
\addplot [%
    point meta=explicit,
    scatter,
    scatter/use mapped color={draw=mapped color,fill=mapped color},
    mark=*,
    draw=none]table[meta=X] {\tableA};

\addplot [mesh,point meta=x,domain=0.02:0.18,thick,samples=1000] {\slope*x+\intercept};
\end{axis}
\end{tikzpicture}
%\end{figure}
\end{document}
share|improve this question
    
now i am undecided ! lol –  FormlessCloud Jun 8 '12 at 19:44
    
The solution in my answer automatically uses the line thickness and the colormap used in the plot. –  Jake Jun 8 '12 at 21:44
    
You have to add forget plot to the options of the first plot command (the one you don't want a legend entry for), or reverse the order of your \addplot commands. –  Jake Jun 9 '12 at 11:20
    
how can i set the samples for the line legend ? to make it equal to the line of regression? –  FormlessCloud Jun 9 '12 at 11:24
1  
I've edited my answer, the legend now uses the same number of samples as the plot. –  Jake Jun 9 '12 at 11:53

2 Answers 2

up vote 6 down vote accepted

\addlegendentry creates legend entries starting from the first plot in the axis, it doesn't matter where you call the command. Since you only want a legend entry for the second plot, you'll have to remove the first one from the list. You do that by adding forget plot to the options of the first plot.

You will then get a coloured legend entry, but unfortunately, it shows a small mesh, not a line. Usually, you could just add line legend to the plot options to make the legend image a line, but that doesn't work with lines of varying colours like yours. You'll have to adjust the legend image code.

Here's a style mesh line legend that uses an adapted version of the code used for drawing the normal mesh legend. The legend image will be drawn with the same options as the plot (thickness, color map, number of samples). If you call

\addplot [mesh, mesh line legend, ...] ...;

you'll get

\documentclass[border=0.5mm,12pt]{standalone}

\usepackage{pgfplots,pgfplotstable}        % Grafici

\pgfplotsset{%
            mesh line legend/.style={legend image code/.code=\meshlinelegend#1},%
            /pgf/number format/use comma,%
            compat=newest,%
%            height=9cm,%
            width=12cm%
}



%*********************************************************************************
% Impostazioni Pgfplots
%*********************************************************************************

\pgfplotstableread{
X Y
0.05 0.17
0.05 0.041
0.05 0.023
0.1 0.332
0.1 0.089
0.1 0.041
0.15 0.5
0.15 0.132
0.15 0.06
}\tableA

\pgfplotstablecreatecol[linear regression]
{regression}
{\tableA}

\xdef\slope{\pgfplotstableregressiona} %<-- might be handy occasionally
\xdef\intercept{\pgfplotstableregressionb}



%% Code for the coloured line legend
\makeatletter
\long\def\meshlinelegend#1{%
    \scope[%
        #1,
        /pgfplots/mesh/rows=1,
        /pgfplots/mesh/cols=4,
        /pgfplots/mesh/num points=,
        /tikz/x={(0.44237cm,0cm)}, 
        /tikz/y={(0cm,0.23932cm)},
        /tikz/z={(0.0cm,0cm)},
        scale=0.4,
    ]
    \let\pgfplots@metamax=\pgfutil@empty
    \pgfplots@curplot@threedimtrue

    \pgfplotsplothandlermesh
    \pgfplotstreamstart

    \def\simplecoordinate(##1,##2,##3){%
        \pgfmathparse{1000*(##3)}%
        \pgfmathfloatparsenumber\pgfmathresult
        \let\pgfplots@current@point@meta=\pgfmathresult
        \pgfplotstreampoint{\pgfqpointxyz@orig{##1}{##2}{##3}}%
    }%

    \pgfplotsforeachungrouped \x in {0,...,\pgfkeysvalueof{/pgfplots/samples}}{
        \pgfmathsetmacro\y{\x/\pgfkeysvalueof{/pgfplots/samples}}
        \pgfmathsetmacro\x{\x/\pgfkeysvalueof{/pgfplots/samples}*3}
        \simplecoordinate(\x,0,\y)
    }

    \pgfplotstreamend
    \pgfusepath{stroke}
    \endscope
}%
\makeatother
%% End code for the coloured line legend



\begin{document}
\thispagestyle{empty}
%\begin{figure}
\centering
\begin{tikzpicture}
\begin{axis}[%
/pgf/number format/fixed,
legend pos=outer north east,
grid=major,
xmin=0, xmax=0.2,
ymin=0, ymax=0.6,
point meta min={0},
point meta max={0.2},
yticklabel shift=2pt,
xticklabel shift=2pt,
legend pos=north west,
legend cell align=left,
minor tick num=4,
xticklabel style={text height=1.5ex},
xtick={0,0.05,...,0.3},
minor xtick={%
0.01,0.02,0.03,0.04,%
0.06,0.07,0.08,0.09,%
0.11,0.12,0.13,0.14,%
0.16,0.17,0.18,0.19},%
extra x ticks={0,0.05,...,0.3},
extra x tick style={
    xticklabel pos=right,
    xticklabel style={text depth=0pt}
},
extra y ticks={0,0.1,...,0.7},
extra y tick style={
    yticklabel pos=right
},
tick style={thin,black},
xlabel=\large $m$,
ylabel=\large $\Delta l$,
colorbar horizontal,
colorbar style={
    /pgf/number format/fixed,
    xticklabel shift=2pt,
    xtick={0,0.05,...,0.3},
    xticklabel style={text height=1.5ex}
},
axis line style={draw=none}, after end axis/.append code={\draw (rel axis cs:0,0) rectangle (rel axis cs:1,1);},
colormap={new}{color(0cm)=(violet);color(1cm)=(blue);color(2cm)=(cyan);color(3cm)=(green);color(4cm)=(yellow);color(5cm)=(orange);color(6cm)=(red)}
]
\addplot [%
    point meta=explicit,
    scatter,
    scatter/use mapped color={draw=mapped color,fill=mapped color},
    mark=*,
    draw=none, forget plot]table[meta=X] {\tableA};

\addplot [mesh,mesh line legend,very thick,point meta=x,domain=0.02:0.18,samples=10] {\slope*x+\intercept};%
\addlegendentry{%
$\pgfmathprintnumber{\pgfplotstableregressiona} \cdot x
\pgfmathprintnumber[print sign]{\pgfplotstableregressionb}$}
\end{axis}
\end{tikzpicture}
%\end{figure}
\end{document}
share|improve this answer
    
You are awesome guys ! :O –  FormlessCloud Jun 8 '12 at 17:25
2  
now is perfect ! –  FormlessCloud Jun 9 '12 at 12:46

You can use the convenient \pgfplotscolormaptoshadingspec command to convert your colormap to a shading and then define a legend image code with it. I've defined a simple colormap legend style to draw a colorbar in the legend.

Note that \addlegendentry respects the sequence of \addplot commands so I switched the order of the plotting commands to get the line as a first legend entry.

\documentclass{article}
\usepackage{pgfplots,pgfplotstable}

\pgfplotsset{colormap legend/.style={
legend image code/.code={%
\path[draw=none,shading=tempshading,shade] (0cm,-0.4mm) rectangle (0.6cm,0.8mm);
        }
    }
}
\pgfplotsset{/pgf/number format/use comma,compat=newest,%
            width=12cm,%
            height=9cm%
            }

\pgfplotstableread{
X Y
0.05 0.17
0.05 0.041
0.05 0.023
0.1 0.332
0.1 0.089
0.1 0.041
0.15 0.5
0.15 0.132
0.15 0.06
}\tableA

\pgfplotstablecreatecol[linear regression]
{regression}
{\tableA}

\xdef\slope{\pgfplotstableregressiona} %<-- might be handy occasionally
\xdef\intercept{\pgfplotstableregressionb}


\begin{document}
\begin{tikzpicture}
\begin{axis}[/pgf/number format/fixed,
legend pos=outer north east,
grid=major,
xmin=0, xmax=0.2,
ymin=0, ymax=0.6,
point meta min={0},
point meta max={0.2},
yticklabel shift=2pt,
xticklabel shift=2pt,
legend pos=north west,
legend cell align=left,
minor tick num=4,
xticklabel style={text height=1.5ex},
xtick={0,0.05,...,0.3},
minor xtick={%
0.01,0.02,0.03,0.04,%
0.06,0.07,0.08,0.09,%
0.11,0.12,0.13,0.14,%
0.16,0.17,0.18,0.19},%
extra x ticks={0,0.05,...,0.3},
extra x tick style={
    xticklabel pos=right,
    xticklabel style={text depth=0pt}
},
extra y ticks={0,0.1,...,0.7},
extra y tick style={
    yticklabel pos=right
},
tick style={thin,black},
xlabel=\large $m$,
ylabel=\large $\Delta l$,
colorbar horizontal,
colorbar style={
    /pgf/number format/fixed,
    xticklabel shift=2pt,
    xtick={0,0.05,...,0.3},
    xticklabel style={text height=1.5ex}
},
axis line style={draw=none}, after end axis/.append code={\draw (rel axis cs:0,0) rectangle (rel axis cs:1,1);},
colormap={new}{color(0cm)=(violet);color(1cm)=(blue);color(2cm)=(cyan);color(3cm)=(green);color(4cm)=(yellow);color(5cm)=(orange);color(6cm)=(red)}
]
\pgfplotscolormaptoshadingspec{new}{2cm}\result
\def\tempb{\pgfdeclarehorizontalshading{tempshading}{3cm}}%
\expandafter\tempb\expandafter{\result}%
\addplot [mesh,point meta=x,domain=0.025:0.175,very thick,samples=10,colormap legend] {\slope*x+\intercept};
\addlegendentry{%
$\pgfmathprintnumber{\pgfplotstableregressiona} \cdot x
\pgfmathprintnumber[print sign]{\pgfplotstableregressionb}$}
\addplot [point meta=explicit,
    scatter,
    mark=*,
    draw=none,empty legend]
    table[meta=X] {\tableA};
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

share|improve this answer
    
how i can make the rectangle with the same thickness as the line of regression? what is the thickness of the command "very thick" ? –  FormlessCloud Jun 8 '12 at 20:22
2  
If you look at the definition of colormap legend/.style, you can see that the dimensions are 6mm x 1.2 mm. If you search the file tikz.code.tex you can find the definitions of predefined line widths. Starting from ultra thin to ultra thick the widths are 0.1,0.2,0.4,0.6,0.8,1.2,1.6 all in pt. The regular normal line has 1pt. –  percusse Jun 8 '12 at 20:44
    
(0cm,-0.4mm) rectangle (0.6cm,0.8mm); i see only this.. –  FormlessCloud Jun 8 '12 at 20:53
    
@FormlessCloud Yes the first coordinate is the lower left corner and the second one is the upper right. So you can try using (0mm,-1pt) rectangle (6mm,0.6pt). If the resulting line is too low shift the x coordinates up until you are happy with it. –  percusse Jun 8 '12 at 20:55
    
check my update answer –  FormlessCloud Jun 8 '12 at 21:18

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.