4

I am trying to replicate the following image:

enter image description here

My work so far:

\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);
    \draw[draw = black, ultra thick] (axis cs: 0.5, 0) -- (axis cs: 0.5, 1.5) node [above, right] {$y = 2 - x$};
    %y=0 label
    \node [below] at (axis cs: 0.5,0) {$y=0$};
    %line with label
    \addplot+[draw = black, ultra thick, name path = upper, domain=0:2] {2-x} node [midway, sloped, above, black] {$x + y = 2$};
    %node for labeling line
    \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}

enter image description here

The main issues are that the node labels aren't appearing and and positioned correctly, and I'm not sure how to get the circular ends of lines.

Thanks for any help - sorry if these questions have already been answered elsewhere.

2
  • This is a follow up to: Labeling Plots?. Jun 13, 2014 at 18:37
  • 1
    Just for the circle ends, if you add usetikzlibrary{arrows} in your preamble you could then use \draw[-*] (0,0) -- (1,1);
    – s__C
    Jun 14, 2014 at 16:18

2 Answers 2

6

You need to apply clip=false and place circle nodes. You also had above, right which effectively is right -- I think you meant above right.

enter image description here

Also, if you want to use a different color for the vertical line (as shown above), it is better to draw outside of the axis environment so that the filled circle is displayed correctly:

enter image description here

Code:

\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,
        clip=false,
        ]
    %y=0 label
    \node [below] at (axis cs: 0.5,0) {$y=0$};
    %line with label
    \addplot+[draw = black, ultra thick, name path = upper, domain=0:2] {2-x} 
        node [midway, sloped, above, black] {$x + y = 2$};
    %node for labeling line
    \addplot+[draw = black, ultra thick, name path = lower, domain=0:2] {0};  
    %shading    
    \addplot[gray!40] fill between[of = upper and lower];
    %thick lines
    \draw[draw = black, ultra thick] (axis cs: 0, 0) -- (axis cs: 0, 2);
    \draw[draw = red, ultra thick] (axis cs: 0.5, 0) -- (axis cs: 0.5, 1.5) 
        node [draw=red, circle, fill=red, inner sep=2pt, pos=0]  {}
        node [draw=red, circle, fill=red, inner sep=2pt, pos=1]  {}
        node [above right] {$y = 2 - x$};
\end{axis}
\end{tikzpicture}
\end{document}

Code: Draw outside of axis environment:

\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,
        clip=false,
        ]
    %y=0 label
    \node [below] at (axis cs: 0.5,0) {$y=0$};
    %line with label
    \addplot+[draw = black, ultra thick, name path = upper, domain=0:2] {2-x} 
        node [midway, sloped, above, black] {$x + y = 2$};
    %node for labeling line
    \addplot+[draw = black, ultra thick, name path = lower, domain=0:2] {0};  
    %shading    
    \addplot[gray!40] fill between[of = upper and lower];
    %thick lines
    \draw[draw = black, ultra thick] (axis cs: 0, 0) -- (axis cs: 0, 2);

    \coordinate (TOP) at (axis cs: 0.5, 1.5);
    \coordinate (BOT) at (axis cs: 0.5, 0);
\end{axis}
    \draw[draw = red, ultra thick] (BOT) -- (TOP) 
        node [draw=red, circle, fill=red, inner sep=2pt, pos=0]  {}
        node [draw=red, circle, fill=red, inner sep=2pt, pos=1]  {}
        node [above right] {$y = 2 - x$};
\end{tikzpicture}
\end{document}
0
2

One way is to add two extra nodes for the circles and define the text relative to them. Note that to draw outside the axes you have to turn on clip=false.

\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,
        clip=false,%<-- This option needs to be added
        ]
    %thick lines
    \draw[draw = black, ultra thick] (axis cs: 0, 0) -- (axis cs: 0, 2);
    \node[circle,fill] at (axis cs:0.5,1.5) (a) {};%<---- Here is for drawing the circle
    \draw[draw = black, ultra thick] (axis cs: 0.5, 0) -- (axis cs: 0.5, 1.5)
        node[anchor=south west] at (a.north east) {$y = 2 - x$};
    %y=0 label
    \node[circle,fill] at (axis cs:0.5,0) (b) {};%<---- Here is for drawing the circle
    \node[anchor=north] at (b.south) {$y=0$};
    %line with label
    \addplot+[draw = black, ultra thick, name path = upper, domain=0:2] {2-x} node [midway, sloped, above, black] {$x + y = 2$};
    %node for labeling line
    \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}

enter image description here

You must log in to answer this question.

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