# Shade and insert labels in a triangular region with TikZ

I need to plot a triangle with vertices (0,0), (1,2), (0,3) with x & y coordinate axes (and labels), and tick marks on the axes. How can I accomplish this in TikZ.

My code is:

\begin{tikzpicture}
\draw[->] (-5.0,0) -- (5.0,0) node[right] {$x$} coordinate(x axis);
\draw[->] (0,-5.0) -- (0,5.0) node[above] {$y$} coordinate(y axis);
\draw (0,0) -- (1,2) -- (0,3) -- cycle;
\end{tikzpicture}


But, how do I get the points labeled and shade in the region?

-
I have part of this below: \begin{tikzpicture} \draw[->] (-5.0,0) -- (5.0,0) node[right] {$x$} coordinate(x axis); \draw[->] (0,-5.0) -- (0,5.0) node[above] {$y$} coordinate(y axis); \draw (0,0) -- (1,2) -- (0,3) -- cycle; \end{tikzpicture} But, how do I get the points labeled and shade in the region? Thanks. – Michael Dykes Jul 10 '12 at 13:00

Here is my solution:

\documentclass[11pt]{article}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}
\draw[top color=white,bottom color=red] (0,0) -- (1,2) -- (0,3) -- cycle;

\foreach \coordinate/\label/\pos in {{(0,0)/label1/above left},{(1,2)/label2/right},{(0,3)/label3/above right}}
\node[\pos] at \coordinate {\label};

\draw[->] (-5.0,0) -- (5.0,0) node[right] {$x$} coordinate(x axis);
\draw[->] (0,-5.0) -- (0,5.0) node[above] {$y$} coordinate(y axis);
\end{tikzpicture}

\end{document}


It gives:

The key points are basically two:

1. the foreach that allows to specify in one shot the coordinate (i.e. (0,0)) in which the label will be placed in a given position (i.e. above left);

2. the use of the shadings library that allows to shade the region; in the example the shading is vertical, but it is possible to insert different type of shadings (see 47 Shadings Library from the pgfmanual version April 25, 2012 / 46 Shadings Library from the pgfmanual version October 25, 2010).

-

A solution with tikz and tkz-euclide

\documentclass[11pt]{scrartcl}
\usepackage{tkz-euclide}
\usetkzobj{polygons}

\begin{document}
\begin{tikzpicture}
\tkzInit[xmax=5,ymax=5]
\tkzAxeXY
\tkzDefPoint(0,0){A}
\tkzDefPoint(1,2){B}
\tkzDefPoint(0,3){C}
\tkzDrawPolygon[top color=white,bottom color=red](A,B,C)
\tkzLabelPoints[above right](A,B,C)
\end{tikzpicture}
\end{document}


-

Here is an example using the patch plot type of pgfplots:

\documentclass{standalone}

\usepackage{pgfplots}

\begin{document}

\begin{tikzpicture}
\begin{axis}
table[point meta=\thisrow{c}] {
x y c
% first patch:
0 0 0.2
1 1 0
2 0 1

% second patch:
1 1 0
2 0 -1
3 1 0

% third patch:
2 0 0.5
3 1 1
4 0 0.5
};
\end{axis}
\end{tikzpicture}

\end{document}


and with labels and continuos shading:

\documentclass{standalone}

\usepackage{pgfplots}

\begin{document}

\begin{tikzpicture}
\begin{axis}
table[point meta=\thisrow{c}] {
x y c
% first patch:
0 0 0.2
1 1 0
2 0 1

% second patch:
1 1 0
2 0 1
3 1 0

% third patch:
2 0 1
3 1 0
4 0 0.5
};
\addplot[only marks,nodes near coords] % this produces labels
table[point meta=explicit symbolic,meta=labels] {
x y labels
0 0 $a$
1 1 $b$
2 0 $c$
3 1 $d$
4 0 $e$
};
\end{axis}
\end{tikzpicture}

\end{document}


It uses a default axis configuration and reads points from an input table. The table has three columns: one for x, one for y, and one for the color data. The color data is mapped linearly into a colormap which can be configured, what you see is the default. The shader=interp tells pgfplots to produce a linear shading between the three corners and is color data.

The second graphics has two plots in the same axis: one which produces the geometry and one which produces nodes near coords. This is a special scatter plot which assumes that point meta contains the content of nodes which are placed at the input coordinates. In this case, I used point meta=explicit symbolic which, for table input, means that the meta column contains textual data. I chose meta=labels to idenfity the "labels" column which contains math mode text.

You may also be interested in

• axis lines=left which does not produce a box but axis lines on their lower ("left") limits,

• three dimensional plots with \addplot3 (and one more table column)

• other input formats (coordinates as in your example is also supported, even with color data)

• details for the encountered options + more examples in the pdf manual on http://pgfplots.sourceforge.net/

-

The following is rather close to what you're after I guess:

\documentclass{standalone}

\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
\draw[->] (-5.0,0) -- (5.0,0) node[right] {$x$} coordinate(x axis);
% Loop to generate ticks and labels
\foreach \i in {-5,-4,...,5}
\draw (\i,-2 pt) -- (\i,2 pt) node[above]{$\i$};
\draw[->] (0,-5.0) -- (0,5.0) node[above] {$y$} coordinate(y axis);
\foreach \i in {-5,-4,...,5}
\draw (-2 pt,\i) -- (2 pt,\i) node[right]{$\i$};
\draw[red,fill] (0,0) node[left]{$a$} -- (1,2) node[right]{$b$} -- (0,3) node[left]{$c$} -- cycle;
\end{tikzpicture}
\end{document}


Most of the things are basic and can be found in the manual or in the wonderful repository of examples.

-