我正在尝试延长回归线

我正在尝试延长回归线

我有以下代码:

\documentclass{standalone}
\usepackage{pgfplotstable}
\pgfplotsset{width=12cm,compat=1.9}
\usepackage{filecontents}
\begin{filecontents*}{all_women.csv}
a,b
1.431363764,-0.114988484
1.505149978,0.303005044
1.568201724,0.806163983
1.62324929,1.109635968
1.672097858,1.24886644
1.716003344,1.50350828
1.755874856,1.621079988
1.792391689,1.714388602
1.826074803,1.830499259
1.857332496,1.888497767
\end{filecontents*}

\begin{filecontents*}{select_women.csv}
a,b
1.431363764,-0.114988484
1.505149978,0.303005044
1.568201724,0.806163983
1.62324929,1.109635968
\end{filecontents*}

\begin{filecontents*}{steps_women.csv}
a,b
1.431363764,-0.114988484
1.431363764,0.303005044
1.505149978,0.303005044
1.505149978,0.806163983
1.568201724,0.806163983
1.568201724,1.109635968
1.62324929,1.109635968
1.62324929,1.24886644
1.672097858,1.24886644
1.672097858,1.50350828
1.716003344,1.50350828
1.716003344,1.621079988
1.755874856,1.621079988
1.755874856,1.714388602
1.792391689,1.714388602
1.792391689,1.830499259
1.826074803,1.830499259
1.826074803,1.888497767
1.857332496,1.888497767
\end{filecontents*}

\begin{document}
\pgfplotstableread[col sep=comma]{steps_women.csv}\datatable
\begin{tikzpicture}
\begin{axis}[
    title={Breast},
    xlabel={Age (years)},
    ylabel={rates per 100 000},
     legend pos=north west,
    ymajorgrids=true,
    grid style=dashed,
    xtick={1.431363764,1.505149978,1.568201724,1.62324929,
    1.672097858,1.716003344,1.755874856,1.792391689,1.826074803,1.857332496},
    xticklabels={[25-29],[30-34],[35-39],[40-44],[45-49],[50-54],[55-59],[60-64],[65-69],[70-75]},
    ytick={-0.114988484,0.303005044,0.806163983,1.109635968,1.24886644,1.50350828,1.621079988,1.714388602,1.830499259,1.888497767},
    yticklabels={1,2,6,13,18,32,42,52,68,77},
    x tick label style={rotate=60, anchor=east}
]
\addplot[blue] table [x=a,y=b, col sep=comma]{\datatable};
\pgfplotstableread[col sep=comma]{select_women.csv}\datatable
\addplot[red] table[
    y={create col/linear regression={y=b}}
]{\datatable};
\addlegendentry{Data}
\addlegendentry{%
$\pgfmathprintnumber{\pgfplotstableregressiona} \cdot x 
\pgfmathprintnumber[print sign]{\pgfplotstableregressionb}$}
\end{axis}
\end{tikzpicture}
\hspace{0.10cm}
\end{document}

由此得出如下图所示:

在此处输入图片描述

我正在尝试延伸从 中的 4 个点绘制的趋势线select_women.csv。有什么办法吗?谢谢!

答案1

这是使用 的尝试shorten。我已经将红线向两个方向延伸。但是,要添加图例,您必须做更多的工作,否则图例线也会延伸。在这里,我定义了一个自定义图例图像并使用了它。

\documentclass{standalone}
\usepackage{pgfplotstable}
\pgfplotsset{width=12cm,compat=1.9}
\usepackage{filecontents}
\begin{filecontents*}{all_women.csv}
a,b
1.431363764,-0.114988484
1.505149978,0.303005044
1.568201724,0.806163983
1.62324929,1.109635968
1.672097858,1.24886644
1.716003344,1.50350828
1.755874856,1.621079988
1.792391689,1.714388602
1.826074803,1.830499259
1.857332496,1.888497767
\end{filecontents*}

\begin{filecontents*}{select_women.csv}
a,b
1.431363764,-0.114988484
1.505149978,0.303005044
1.568201724,0.806163983
1.62324929,1.109635968
\end{filecontents*}

\begin{filecontents*}{steps_women.csv}
a,b
1.431363764,-0.114988484
1.431363764,0.303005044
1.505149978,0.303005044
1.505149978,0.806163983
1.568201724,0.806163983
1.568201724,1.109635968
1.62324929,1.109635968
1.62324929,1.24886644
1.672097858,1.24886644
1.672097858,1.50350828
1.716003344,1.50350828
1.716003344,1.621079988
1.755874856,1.621079988
1.755874856,1.714388602
1.792391689,1.714388602
1.792391689,1.830499259
1.826074803,1.830499259
1.826074803,1.888497767
1.857332496,1.888497767
\end{filecontents*}

\pgfplotsset{
/pgfplots/my legend/.style={
legend image code/.code={
\draw[red,mark repeat=2,mark phase=2,##1]
plot coordinates {
(0cm,0cm)
(0.3cm,0cm)
(0.6cm,0cm)
};%
}
}
}


\begin{document}
\pgfplotstableread[col sep=comma]{steps_women.csv}\datatable
\begin{tikzpicture}
\begin{axis}[
    title={Breast},
    xlabel={Age (years)},
    ylabel={rates per 100 000},
     legend pos=north west,
    ymajorgrids=true,
    grid style=dashed,
    xtick={1.431363764,1.505149978,1.568201724,1.62324929,
    1.672097858,1.716003344,1.755874856,1.792391689,1.826074803,1.857332496},
    xticklabels={[25-29],[30-34],[35-39],[40-44],[45-49],[50-54],[55-59],[60-64],[65-69],[70-75]},
    ytick={-0.114988484,0.303005044,0.806163983,1.109635968,1.24886644,1.50350828,1.621079988,1.714388602,1.830499259,1.888497767},
    yticklabels={1,2,6,13,18,32,42,52,68,77},
    x tick label style={rotate=60, anchor=east}
]
\addplot[blue] table [x=a,y=b, col sep=comma]{\datatable};
\pgfplotstableread[col sep=comma]{select_women.csv}\datatable
\addplot[red,shorten >= -4cm,,shorten <= -2cm,forget plot] table[
    y={create col/linear regression={y=b}}
]{\datatable};                      %% use forget plot means no legend for this line
\addlegendentry{Data}
\addlegendimage{%                   %% add the legend image                
my legend
}
\addlegendentry{%                  %% add the entry
$\pgfmathprintnumber{\pgfplotstableregressiona} \cdot x
\pgfmathprintnumber[print sign]{\pgfplotstableregressionb}$}
\end{axis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

您还可以通过使用postaction并限制postaction达到图例来实现这一点percusse 的这个答案

\documentclass{standalone}
\usepackage{pgfplotstable}
\pgfplotsset{width=12cm,compat=1.9}

\makeatletter
\tikzset{
  nomorepostactions/.code={\let\tikz@postactions=\pgfutil@empty},
  /pgfplots/legend image post style={
            every path/.append style={nomorepostactions}
    },  
}
\makeatother

\begin{document}
\pgfplotstableread[col sep=comma]{steps_women.csv}\datatable
\begin{tikzpicture}
\begin{axis}[
    title={Breast},
    xlabel={Age (years)},
    ylabel={rates per 100 000},
     legend pos=north west,
    ymajorgrids=true,
    grid style=dashed,
    xtick={1.431363764,1.505149978,1.568201724,1.62324929,
    1.672097858,1.716003344,1.755874856,1.792391689,1.826074803,1.857332496},
    xticklabels={[25-29],[30-34],[35-39],[40-44],[45-49],[50-54],[55-59],[60-64],[65-69],[70-75]},
    ytick={-0.114988484,0.303005044,0.806163983,1.109635968,1.24886644,1.50350828,1.621079988,1.714388602,1.830499259,1.888497767},
    yticklabels={1,2,6,13,18,32,42,52,68,77},
    x tick label style={rotate=60, anchor=east}
]
\addplot[blue] table [x=a,y=b, col sep=comma]{\datatable};
\pgfplotstableread[col sep=comma]{select_women.csv}\datatable
\addplot[red,postaction={shorten >= -4cm,,shorten <= -2cm}] table[
    y={create col/linear regression={y=b}}
]{\datatable};
\addlegendentry{Data}
\addlegendentry{%
$\pgfmathprintnumber{\pgfplotstableregressiona} \cdot x
\pgfmathprintnumber[print sign]{\pgfplotstableregressionb}$}
\end{axis}
\end{tikzpicture}
\end{document}

相关内容