# pgfplots, change legende image style

I'd like to plot data marks and a linear regressions for two data sets in the same plot and add a legend entry for one data/regression pair: Here is my current state:

\documentclass{article}
\usepackage{pgfplotstable}
x,   y1,      y2,
1,    1.0,     3.0,
2,    2.2,     6.3,
4,    3.9,    10.0,
8,    8.1,    25.0,
} \data
\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}
\begin{axis}[legend pos=north west,
legend image post style={solid}
]
\addplot[only marks,mark=o,color=red] table [x=x, y=y1] {\data};
\addplot[no markers,color=red,forget plot] table [x=x, y={create col/linear regression={y=y1}}] {\data};
\addplot[no markers,color=blue,forget plot] table [x=x, y={create col/linear regression={y=y2}}] {\data};
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}


It creates the following image:

I'd like to have a line and a mark for each single legend entry, but currently only the marks appear. How can I draw the lines as well? When I change the described plots to the line plots (i.e. moving the 'forget plot' option one line up), I guess I can not add different marks according to each entry in the legend, can I? Thanks, Juhui

-

You can change the plot options used for generating the legend image using the legend image post style key:

\documentclass[border=5mm]{standalone}
\usepackage{pgfplotstable}
x,   y1,      y2,
1,    1.0,     3.0,
2,    2.2,     6.3,
4,    3.9,    10.0,
8,    8.1,    25.0,
} \data
\begin{document}
\begin{tikzpicture}
\begin{axis}[legend pos=north west,
legend image post style={solid}
]
\addplot[only marks,mark=o,color=red, legend image post style={sharp plot}] table [x=x, y=y1] {\data};
\addplot[no markers,color=red,forget plot] table [x=x, y={create col/linear regression={y=y1}}] {\data};

@Magpie: Yes, PGFPlots uses a least squares regression. The parameters for the first regression found by PGFPlots are a=1.00174, b=0.0043474, using R's lm command finds a=1.00174, b=0.04348, so I'd say the method used by PGFPlots is valid. –  Jake Jan 19 '13 at 16:47
@Magpie: After you do the regression, the regression parameters are saved in two macros \pgfplotstableregressiona and \pgfplotstableregressionb. –  Jake Jan 19 '13 at 17:09