PGFlots 彩色回归图例

PGFlots 彩色回归图例

我怎样才能拥有这个传奇?

在此处输入图片描述

在此图中:

\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}

更新杰克的回答:

\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}

更新这是对 percusse 答案的更新

在此处输入图片描述

\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}

答案1

\addlegendentry从轴上的第一个图开始创建图例条目,无论您在哪里调用该命令都没有关系。由于您只想要第二个图的图例条目,因此您必须从列表中删除第一个图。您可以通过添加到forget plot第一个图的选项来实现这一点。

然后您将获得一个彩色图例条目,但不幸的是,它显示的是一个小网格,而不是一条线。通常,您只需添加line legend到绘图选项中即可使图例图像成为一条线,但这不适用于像您这样的不同颜色的线条。您必须调整legend image code

这是使用用于绘制普通网格图例的代码的改编版本的样式mesh line legend。图例图像将使用与绘图相同的选项(厚度、颜色图、样本数)绘制。如果您调用

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

你会得到

\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}

答案2

您可以使用便捷的\pgfplotscolormaptoshadingspec命令将颜色图转换为阴影,然后用它定义图例图像代码。我定义了一个简单的colormap legend样式来在图例中绘制颜色条。

请注意,\addlegendentry要尊重命令的顺序\addplot,因此我切换了绘图命令的顺序,以将该线作为第一个图例条目。

\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}

在此处输入图片描述

相关内容