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 haven't been able to find anything on this after searching for a while, so I apologize if this is a duplicate.

I need to replicate the following image:

enter image description here

Here's what I've got so far:

enter image description here

\documentclass{minimal}

%%for graphs
\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{pgfplots}
\pgfplotsset{compat=1.10}
\usepgfplotslibrary{fillbetween}
%%end graph code

    \begin{document}
    \begin{tikzpicture}[scale=1]
    \begin{axis}[
            axis x line=bottom,%need to change to bottom to put 0 in 
            axis y line=middle,
            x label style={at={(axis cs: 2.6, -0.12)},anchor=south},%rotate = degrees number can be used to rotate the label
            y label style={at={(axis cs:0, 2.5)},anchor=south},
            xtick={0, 1, 2}, %or {listofvalues, , },
            ytick={0, 1, 2},
            yticklabels={$0$, $1$, $2$},
            xticklabels={$0$, $1$, $2$},
            ymin=0,
            ymax=2.5,
            xmin=0,
            xmax=2.5,
            ylabel=$y$, 
            xlabel=$x$,
            area style,
            ]
        %thick lines
        \draw[draw = black, ultra thick] (axis cs: 0, 0) -- (axis cs: 0, 2);
        \addplot+[draw = black, ultra thick, name path = upper, domain=0:2] {2-x};
        \addplot+[draw = black, ultra thick, name path = lower, domain=0:2] {0};  
        %shading    
        \addplot[gray!40] fill between[of = upper and lower];
    \end{axis}
    \end{tikzpicture}
    \end{document}

It doesn't matter to me where the axis labels are - the main problem is trying to get the x + y = 2 on the line.

How can I get the x + y = 2 on the line?

share|improve this question
    
Follow up question: Circular ends of lines and positioning of nodes?. –  Peter Grill Jun 13 at 18:37

1 Answer 1

up vote 7 down vote accepted

One way would be to place a node:

\node [rotate=-45, above] at (axis cs: 1,1) {$x + y = 2$};

which requires the use of the axis cs coordinate system and a rotate=.

Alternatively, you can place the node as part of the \addplot using the sloped option:

    \addplot+[draw = black, ultra thick, name path = upper, domain=0:2] {2-x} 
          node [midway, sloped, above, red] {$x + y = 2$};

enter image description here

Notes:


Code: Manual Node:

\documentclass{article}

%%for graphs
\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{pgfplots}
\pgfplotsset{compat=1.10}
\usepgfplotslibrary{fillbetween}
%%end graph code

    \begin{document}
    \begin{tikzpicture}[scale=1]
    \begin{axis}[
            axis x line=bottom,%need to change to bottom to put 0 in 
            axis y line=middle,
            x label style={at={(axis cs: 2.6, -0.12)},anchor=south},%rotate = degrees number can be used to rotate the label
            y label style={at={(axis cs:0, 2.5)},anchor=south},
            xtick={0, 1, 2}, %or {listofvalues, , },
            ytick={0, 1, 2},
            yticklabels={$0$, $1$, $2$},
            xticklabels={$0$, $1$, $2$},
            ymin=0,
            ymax=2.5,
            xmin=0,
            xmax=2.5,
            ylabel=$y$, 
            xlabel=$x$,
            area style,
            ]
        %thick lines
        \draw[draw = black, ultra thick] (axis cs: 0, 0) -- (axis cs: 0, 2);
        \addplot+[draw = black, ultra thick, name path = upper, domain=0:2] {2-x};
        \addplot+[draw = black, ultra thick, name path = lower, domain=0:2] {0};  
        %shading    
        \addplot[gray!40] fill between[of = upper and lower];
        \node [rotate=-45, above] at (axis cs: 1,1) {$x + y = 2$};
    \end{axis}
    \end{tikzpicture}
    \end{document}

Code: Node as part of \addplot:

\documentclass{article}

%%for graphs
\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{pgfplots}
\pgfplotsset{compat=1.10}
\usepgfplotslibrary{fillbetween}
%%end graph code

    \begin{document}
    \begin{tikzpicture}[scale=1]
    \begin{axis}[
            axis x line=bottom,%need to change to bottom to put 0 in 
            axis y line=middle,
            x label style={at={(axis cs: 2.6, -0.12)},anchor=south},%rotate = degrees number can be used to rotate the label
            y label style={at={(axis cs:0, 2.5)},anchor=south},
            xtick={0, 1, 2}, %or {listofvalues, , },
            ytick={0, 1, 2},
            yticklabels={$0$, $1$, $2$},
            xticklabels={$0$, $1$, $2$},
            ymin=0,
            ymax=2.5,
            xmin=0,
            xmax=2.5,
            ylabel=$y$, 
            xlabel=$x$,
            area style,
            ]
        %thick lines
        \draw[draw = black, ultra thick] (axis cs: 0, 0) -- (axis cs: 0, 2);
        \addplot+[draw = black, ultra thick, name path = upper, domain=0:2] {2-x} node [midway, sloped, above, red] {$x + y = 2$};
        \addplot+[draw = black, ultra thick, name path = lower, domain=0:2] {0};  
        %shading    
        \addplot[gray!40] fill between[of = upper and lower];
    \end{axis}
    \end{tikzpicture}
    \end{document}
share|improve this answer

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.