1

I know that here on TeX.SX a plethora of similar questions has been answered, but strangely I couldn't find the one suiting my purpose. To that end I apologise if this post is a duplicate of some other I don't know of.

The MWE is as following:

\documentclass{article}
\usepackage{mathtools}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{xcolor}

\setlength{\parindent}{0cm}

\begin{document}

\begin{tikzpicture}
\begin{axis}[xlabel=$x$,ylabel=$y$,
xmin=-15,xmax=15,ymin=-10,ymax=10, axis lines=center, axis equal, scale=1.6]

\addplot[domain=-15:15, color=blue, samples=400](\x,{\x + 1});
\addplot[domain=-15:15, color=red, samples=400](\x,{4});
\addplot[domain=-15:15, color=green, samples=400](\x,{-1/2*\x - 2});

\node at (axis cs:-2,-1) [circle, scale=0.4, draw=black!80,fill=black!80] {};
\node at (axis cs:3,4) [circle, scale=0.4, draw=black!80,fill=black!80] {};
\node at (axis cs:-12,4) [circle, scale=0.4, draw=black!80,fill=black!80] {};

\node at (axis cs:-2,0) {$P_1$};
\node at (axis cs:3,5) {$P_2$};
\node at (axis cs:-12,5) {$P_3$};
\end{axis}
\end{tikzpicture}

\end{document}

Which produces:

enter image description here

I would like to know how to color the polygon (or simplistically the triangle) defined by these three linear functions. All of the answers to similar questions only discussed the relationship between two "objects", not three. I've tried many methods, but without success. I would be gratified if you helped me out.

God bless
  • 1,767

1 Answers1

2

You have nodes at the vertices, name them and use \fill. Note that you need to specify the center anchor of the nodes. Other comments:

  • You're plotting straight lines, 400 samples is 398 more than you need. It only makes generating the PDF much slower.
  • Making a style for the dots makes the code shorter.
  • You can add a label to the dots instead of adding new nodes.
  • TikZ (which is loaded by pgfplots) loads xcolor, so you don't have to load it explicitly.
  • With pgfplots you don't need to use \x, just x is sufficient. And it's also enough to write \addplot {f(x)}; as long as you're just plotting something against x. The syntax you're using, \addplot ({x(t)}, {y(t)});, works fine, but it's not necessary here.

enter image description here

\documentclass{article}
\usepackage{mathtools}
\usepackage{pgfplots}

\setlength{\parindent}{0cm}

\begin{document}

\begin{tikzpicture}[
  dot/.style={
   circle, inner sep=0pt,minimum size=3pt, draw=black!80,fill=black!80,label=#1
  }]

\begin{axis}[
  xlabel=$x$,ylabel=$y$,
  xmin=-15,xmax=15,
  ymin=-10,ymax=10,
  axis lines=center,
  axis equal,
  width=\linewidth,
  domain=-15:15,
  samples=2
]

\addplot[color=blue] {x + 1};
\addplot[color=red] {4};
\addplot[color=green] {-1/2*x - 2};

\node (P1) at (axis cs:-2,-1) [dot={below:$P_1$}] {};
\node (P2) at (axis cs:3,4) [dot={above:$P_2$}] {};
\node (P3) at (axis cs:-12,4) [dot={above:$P_3$}] {};

\fill [red,opacity=0.1] (P1.center) -- (P2.center) -- (P3.center) -- cycle;

\end{axis}
\end{tikzpicture}

\end{document}
Torbjørn T.
  • 206,688
  • Thank you so much! My question will now be marked as a duplicate, as a solution has been found. Nonetheless, yours is more elegant and takes up less space to write. One big upvote and a tick for you, my friend! – God bless May 21 '17 at 17:20