4

I wish to graph thisenter image description here

This graph has 3 functions on it

t=x-r, r<0, x<0 x=r, r>1, x>1 (x-r)/(1-r), 0

My problem is I am wishing to stop the (x-r)/(1-r) lines at the point (1,1) as seen in my drawing below. Could anyone help me with this please?

Currently have this, also struggling with drawing the vertical lines.

\documentclass[border=2mm]{standalone}
\usepackage{pgfplots}
\begin{document}
    \begin{tikzpicture}
        \begin{axis}[
            xmin=-1, xmax=1.5,
            ymin=0, ymax=1.5,
            axis lines=center,
            axis on top=true,
            domain=-2:3,
        ]
            \addplot [mark=none,draw=red,ultra thick] {x+1};
            \addplot [mark=none,draw=red,ultra thick] {x+2};
            \addplot [mark=none,draw=red,ultra thick] {x+1.5};
            \addplot [mark=none,draw=red,ultra thick] {x};
            \addplot [mark=none,draw=red,ultra thick] {x+0.5};
            \addplot [mark=none,draw=green,ultra thick] {(x-.5)/(1-.5)};
        \end{axis}
    \end{tikzpicture}
\end{document}
2
  • 1
    Please add a Minimal Working Example. Feb 28, 2016 at 13:02
  • 1
    Welcome to TeX.SX! Please make your code compilable (if possible), or at least complete it with \documentclass{...}, the required \usepackage's, \begin{document}, and \end{document}. That may seem tedious to you, but think of the extra work it represents for TeX.SX users willing to give you a hand. Help them help you: remove that one hurdle between you and a solution to your problem. Feb 28, 2016 at 13:07

3 Answers 3

4

You have to add domain as an option to addplot. The vertical lines you can simply draw using the draw macro. By adding the axis cs prefix to a coordinate you can use the axis coordinate system.

\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
    xmin=-1, xmax=1.5,
    ymin=0, ymax=1.5,
    axis lines=center,
    axis on top=true,
    ]
    \addplot [mark=none,draw=red,ultra thick] {x+1};
    \addplot [mark=none,draw=red,ultra thick] {x+2};
    \addplot [mark=none,draw=red,ultra thick] {x+1.5};
    \addplot [mark=none,draw=red,ultra thick] {x};
    \addplot [mark=none,draw=red,ultra thick] {x+0.5};
    \addplot [mark=none,draw=green,ultra thick, domain={0:1}] {(x-.5)/(1-.5)};
    \draw[draw=blue, ultra thick] (axis cs:1,0) -- (axis cs:1,1.5);
    \draw[draw=blue, ultra thick] (axis cs:1.5,0) -- (axis cs:1.5,1.5);
    \end{axis}
\end{tikzpicture}
\end{document}

Output:

pgfplots

2
  • If you use a version of PGFplots >=1.11, then axis cs is not needed and automatically used. You have to make sure to add \pgfplotsset{compat=1.13} in the preamble (1.13 is the current PGFplots version).
    – JP-Ellis
    Feb 28, 2016 at 13:18
  • @JP-Ellis Very convenient. I did not know that. Thanks for making me aware of it.
    – Benjamin
    Feb 28, 2016 at 13:44
4

Here's yet another solution (wow, they came fast!)

In order to avoid repeating yourself, I've used \foreach to loop over values; however, it requires a little trick in certain circumstances due to the way things are expanded and parsed. Essentially, a temporary macro, \tmp, is created so that it contains (in the case of the first iteration):

\tmp = \addplot [green] (0.25, 0) -- (1, 1);

In order to get \i to become 0.25 but \addplot to remain as is, \edef is used which expands all macros it encounters within the definition. In order to prevent \addplot for being expanded though, \noexpand is added in front and \tmp becomes defined as above.

I also made the red lines stop at x=1, but you can easily change that by removing domain=-1:1.

Lastly, I use \pgfkeysvalueof{/pgfplots/ymax} in order to get the value of ymax so that the vertical lines are guaranteed to go to the top of the plot.

\documentclass[tikz]{standalone}

\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.13}

\begin{document}
\begin{tikzpicture}
  \begin{axis}[ 
      xmin=-1,
      xmax=1.5,
      ymin=0,
      ymax=1.5,
      axis lines=center,
      axis on top=true,
      domain=-2:3,
    ]

    \foreach \i in {0, 0.25, ..., 2} {
      \addplot [red, domain=-1:1] {x + \i};
    }
    \foreach \i in {0.25, 0.50, 0.76} {
      \edef\tmp{
        \noexpand\draw [green] (\i, 0) -- (1, 1);
      }
      \tmp
    }
    \foreach \i in {1.00, 1.25, 1.50} {
      \edef\tmp{
        \noexpand\draw [blue] (\i, 0) -- (\i, \pgfkeysvalueof{/pgfplots/ymax});
      }
      \tmp
    }
  \end{axis}
\end{tikzpicture}
\end{document}

output

3

The domain for this specific line(s) can be set separately by adding, e.g., domain=.5:1 like this:

\documentclass [10pt] {article}
\usepackage{pgfplots}
 \pgfplotsset{compat=1.3}
\begin{document}
    \begin{tikzpicture}
    \begin{axis}[
    xmin=-1, xmax=1.5,
    ymin=0, ymax=1.5,
    axis lines=center,
    axis on top=true,
    domain=-2:3,
    ]
    \addplot [mark=none,draw=red,ultra thick] {x+1};
    \addplot [mark=none,draw=red,ultra thick] {x+2};
    \addplot [mark=none,draw=red,ultra thick] {x+1.5};
    \addplot [mark=none,draw=red,ultra thick] {x};
    \addplot [mark=none,draw=red,ultra thick] {x+0.5};
    \addplot [mark=none,draw=green,ultra thick,domain=.5:1] {(x-.5)/(1-.5)};
    \draw[draw=blue, ultra thick] (axis cs:1,0) -- (axis cs:1,1.5);
    \draw[draw=blue, ultra thick] (axis cs:1.5,0) -- (axis cs:1.5,1.5);
    \end{axis}
\end{tikzpicture}
\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .