[Tex/LaTex] pgfplots, change legende image style

pgfplots

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}
\pgfplotstableread[col sep = comma]{
  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};
       \addlegendentry{y1}
       \addplot[only marks,mark=square,color=blue] table [x=x, y=y2]{\data};
       \addplot[no markers,color=blue,forget plot] table [x=x, y={create col/linear regression={y=y2}}] {\data};
       \addlegendentry{y2}
     \end{axis}
   \end{tikzpicture}
  \end{figure}
 \end{document}

It creates the following image:

Legend without lines :(

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

Best Answer

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}
\pgfplotstableread[col sep = comma]{
  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};
       \addlegendentry{y1}
       \addplot[only marks,mark=square,color=blue, legend image post style={sharp plot}] table [x=x, y=y2]{\data};
       \addplot[no markers,color=blue,forget plot] table [x=x, y={create col/linear regression={y=y2}}] {\data};
       \addlegendentry{y2}
     \end{axis}
   \end{tikzpicture}
 \end{document}