彩色拟合 pgfplots

彩色拟合 pgfplots

我想用一条直线拟合这些数据,并且直线上的点必须遵循颜色条水平的颜色。另一个问题是我无法调整网格比例。

   \documentclass[a4paper,12pt]{article}
\usepackage{pgfplots}
\usepackage{pgfplotstable}

\begin{document}
\begin{figure}
\centering
\pgfplotsset{width=10cm}
\begin{tikzpicture}
\makeatletter \newcommand{\pgfplotsdrawaxis}{\pgfplots@draw@axis} \makeatother
\pgfplotsset{axis line on top/.style={
 axis on top=false,
 after end axis/.append code={
    \pgfplotsset{axis line style=opaque,
      ticklabel style=opaque,
      tick style=opaque,
      grid=none}
    \pgfplotsdrawaxis}
  }
}
\pgfdeclarehorizontalshading{stefan}{50bp}{
color(0.00000000000000bp)=(violet);
color(8.33333333333333bp)=(blue);
color(16.66666666666670bp)=(cyan);
color(25.00000000000000bp)=(green);
color(33.33333333333330bp)=(yellow);
color(41.66666666666670bp)=(orange);
color(50.00000000000000bp)=(red)
}
\begin{axis}[legend pos=outer north east,
grid=major,
xmin=0, xmax=0.2,
ymin=0, ymax=0.4,
point meta min={0},
point meta max={0.2},
axis line on top,
xticklabel style={text height=1.5ex},
xticklabels={%
$0$,
$0.05$,
$0.1$,
$0.15$,
$0.2$,
$0.25$,
$0.3$
},
extra x ticks={0,0.05,...,0.3},
extra x tick labels={%
$0$,
$0.05$,
$0.1$,
$0.15$,
$0.2$,
$0.25$,
$0.3$
},
extra x tick style={
    xticklabel pos=right,
    xticklabel style={text depth=0pt}
},
extra y ticks={0,0.2,...,1},
extra y tick style={
    yticklabel pos=right
},
tick style={thin,black},
minor tick num=3,
xlabel=\large $\mbox{s}$,
ylabel=\large $f_{\mu,\sigma^2}(\mbox{s})$,
colorbar horizontal,
colorbar style={
    xticklabels={%
        $-0.4$,
        $-0.3$,
        $-0.2$,
        $-0.1$,
        $\mu$,
        $0.1$,
        $0.2$,
        $0.3$,
        $0.4$
    },
    xticklabel style={text height=1.5ex}
},
colormap={new}{color(0cm)=(violet);color(1cm)=(blue);color(2cm)=(cyan);color(3cm)=(green);color(4cm)=(yellow);color(5cm)=(orange);color(6cm)=(red)},
after end axis/.append code={
    \draw ({rel axis cs:0,0}-|{axis cs:0,0}) -- ({rel axis cs:0,1}-|{axis cs:0,0});
},
legend cell align=left]
\addplot [
    mark=*,
    draw=none]
    table[row sep=\\] {% plot X versus Y. This is original data.
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.05\\
0.15 0.132\\
0.15 0.06\\
};
\addplot [no marks] table[row sep=\\,
y={create col/linear regression={y=Y}}] % compute a linear regression from the input table
{
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.05\\
0.15 0.132\\
0.15 0.06\\
};
%\xdef\slope{\pgfplotstableregressiona} %<-- might be handy occasionally
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}

我想要这样的线条颜色

我可能有这个传说吗? 在此处输入图片描述

答案1

您必须设置point meta=explicitmeta=X。为了获得更平滑的颜色过渡,并能够定义绘制回归线的范围,您必须首先使用计算回归线的参数

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

\xdef\slope{\pgfplotstableregressiona}
\xdef\intercept{\pgfplotstableregressionb}

然后使用 绘制线条\addplot [mesh,point meta=x,domain=0:0.2] {\slope*x+\intercept};

\documentclass[a4paper,12pt]{article}
\usepackage{pgfplots}
\usepackage{pgfplotstable}

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

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

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


\begin{document}
\begin{figure}
\centering
\pgfplotsset{width=10cm}
\begin{tikzpicture}
\makeatletter \newcommand{\pgfplotsdrawaxis}{\pgfplots@draw@axis} \makeatother
\pgfplotsset{axis line on top/.style={
 axis on top=false,
 after end axis/.append code={
    \pgfplotsset{axis line style=opaque,
      ticklabel style=opaque,
      tick style=opaque,
      grid=none}
    \pgfplotsdrawaxis}
  }
}
\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},
minor tick num=4,
axis line on top,
xticklabel style={text height=1.5ex},
xtick={0,0.05,...,0.3},
minor xtick={0,0.01,...,0.2},
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 $\mbox{m}$,
ylabel=\large $\Delta\mbox{l}$,
colorbar horizontal,
colorbar style={
    /pgf/number format/fixed,
    xtick={0,0.05,...,0.3},
    xticklabel style={text height=1.5ex}
},
colormap={new}{color(0cm)=(violet);color(1cm)=(blue);color(2cm)=(cyan);color(3cm)=(green);color(4cm)=(yellow);color(5cm)=(orange);color(6cm)=(red)},
after end axis/.append code={
    \draw ({rel axis cs:0,0}-|{axis cs:0,0}) -- ({rel axis cs:0,1}-|{axis cs:0,0});
},
legend cell align=left]
\addplot [point meta=explicit,
    scatter,
    mark=*,
    draw=none]
    table[meta=X] {\datatable};

\addplot [mesh,point meta=x,domain=0:0.2] {\slope*x+\intercept};
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}

相关内容