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 would like to add straight lines beneath a scatter plot for instance in order to delimit the region where |y| < x and |y| < 2 * x. I wrote something similar to th code below which give satisfactory result but requires to know the range of the data beforehand. I would like to make a template for this graphics that would work equally well if the maximum abscissa is 1e12 or 0.1. I could replace the 1 and .5 in my code by huge numbers but I am sure there is some neater way to achieve this result.

One constraint is that the straight lines should be beneath the marks of the scatter plot.

My current code:

\documentclass{article}

\usepackage{pgfplots}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
  \begin{axis}
    \tikzset{bisector/.style={black}}
    \tikzset{half bisector/.style={black, dashed}}

    \coordinate (origin) at (axis cs:0,0);
    \draw[bisector] (origin)--(axis cs:1,1);
    \draw[bisector] (origin)--(axis cs:1,-1);
    \draw[half bisector] (origin)--(axis cs:1,.5);
    \draw[half bisector] (origin)--(axis cs:1,-.5);

    \addplot[only marks] table[x=x, y=y] {
      x y
      .3 .7
      .2 .1
      .5 .6
      0.4 -0.5
    };
  \end{axis}
\end{tikzpicture}
\end{document}

Its output:

sample output

share|improve this question

2 Answers 2

up vote 4 down vote accepted

You can make use of the fact that PGFPlots stores the axis limits in the keys /pgfplots/xmin and /pgfplots/xmax: You can draw the lines using

\draw (axis cs:\pgfkeysvalueof{/pgfplots/xmin},0.5*\pgfkeysvalueof{/pgfplots/xmin})
    -- (axis cs:\pgfkeysvalueof{/pgfplots/xmax},0.5*\pgfkeysvalueof{/pgfplots/xmax});

Defined like this, the lines will always go from one edge of the plot to the other. It's a bit tedious to type, so it's probably a good idea to define a command like

\newcommand{\straightline}[2][]{%
    \draw[#1] (axis cs:\pgfkeysvalueof{/pgfplots/xmin},#2*\pgfkeysvalueof{/pgfplots/xmin})
        -- (axis cs:\pgfkeysvalueof{/pgfplots/xmax},#2*\pgfkeysvalueof{/pgfplots/xmax});
}

Then you can draw the lines by simply putting

\straightline[half bisector]{0.5}

in your code:

\documentclass{article}

\usepackage{pgfplots}
\usepackage{tikz}

\newcommand{\straightline}[2][]{%
    \draw[#1] (axis cs:\pgfkeysvalueof{/pgfplots/xmin},#2*\pgfkeysvalueof{/pgfplots/xmin})
        -- (axis cs:\pgfkeysvalueof{/pgfplots/xmax},#2*\pgfkeysvalueof{/pgfplots/xmax});
}

\begin{document}
\begin{tikzpicture}
  \begin{axis}
    \tikzset{bisector/.style={black}}
    \tikzset{half bisector/.style={black, dashed}}

    \addplot[only marks, red] table[x=x, y=y] {
      x y
      .3 .7
      .2 .1
      .5 .6
      2.4 -2.5
    };

 \straightline{1}
 \straightline{-1}
 \straightline[half bisector]{0.5}
 \straightline[half bisector]{-0.5}
  \end{axis}
\end{tikzpicture}
\end{document}
share|improve this answer
    
I cannot compile your example. I get the following error message : ! Package PGF Math Error: Could not parse input '1*-2.00134e-2' as a floating point number, sorry. The unreadable part was near '*-2.00134e-2'.. Any idea of what is causing it? –  Alfred M. Nov 5 '13 at 11:09
    
@AlfredM.: is that using my code, or did you make any changes? –  Jake Nov 5 '13 at 11:14
    
Using your code. –  Alfred M. Nov 5 '13 at 12:49
1  
@AlfredM.: What version of PGFPlots are you using? –  Jake Nov 5 '13 at 12:50
1  
The problem was indeed caused by too old a version. –  Alfred M. Nov 5 '13 at 13:07

You can save your data in a table using \pgfplotstableread, then with the \findmax command you find the maximum value of x (x because the second argument of \findmax is 0, with 1 you find the maximum y) and finally you use this maximum value stored in the macro \max to draw your bisectors.

\documentclass{article}
\usepackage{filecontents}
\usepackage{pgfplotstable}
\usepackage{pgfplots}
\usepackage{tikz}

\newcommand{\findmax}[2]{
    \pgfplotstablesort[sort key={#2},sort cmp={float >}]{\sorted}{#1}%
    \pgfplotstablegetelem{0}{#2}\of{\sorted}%
    \let\max=\pgfplotsretval%
}

\pgfplotstableread{
.3 .7
.2 .1
.5 .6
0.4 -0.5
3.0 1.2
}\data

\begin{document}
\findmax{\data}{0}
\begin{tikzpicture}
  \begin{axis}[xmin=0]
    \tikzset{bisector/.style={black}}
    \tikzset{half bisector/.style={black, dashed}}

    \coordinate (origin) at (axis cs:0,0);
    \draw[bisector] (origin)--(axis cs:\max*2,\max*2);
    \draw[bisector] (origin)--(axis cs:\max*2,\max*-2);
    \draw[half bisector] (origin)--(axis cs:\max*2,\max);
    \draw[half bisector] (origin)--(axis cs:\max*2,-\max);

    \addplot[only marks] table[x index=0, y index=1] {\data};
  \end{axis}
\end{tikzpicture}
\end{document}

enter image description here

share|improve this answer
    
I also have trouble compiling your example. The error message is ! Package PGF Math Error: Could not parse input '3.0*2' as a floating point number, sorry. The unreadable part was near '*2'.. –  Alfred M. Nov 5 '13 at 12:51
2  
@AlfredM. I think it is because you are using an old version of pgfplots.. updating it both my answers and @Jake's one will compile –  Red Nov 5 '13 at 12:52
    
The problem was indeed caused by too old a version. –  Alfred M. Nov 5 '13 at 13:06

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.