Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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

share|improve this question
add comment

1 Answer

up vote 5 down vote accepted

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}
share|improve this answer
    
Hi Jake, that's it, thank you very much. You provided two ways to get what I wanted: add the "sharp plot" instead of solid in the legend image post style that I had, and how I can set the legend image style for a single plot. Thanks a lot! –  Juhui Oct 26 '12 at 14:36
    
Is this actually a line of best fit? –  Magpie Jan 19 '13 at 16:16
    
@Magpie: Why wouldn't it be? –  Jake Jan 19 '13 at 16:24
1  
@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
1  
@Magpie: After you do the regression, the regression parameters are saved in two macros \pgfplotstableregressiona and \pgfplotstableregressionb. –  Jake Jan 19 '13 at 17:09
show 2 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.