# [Tex/LaTex] Draw Rectangles with PGFPlots

graphicspgfplots

I'm a new user of LyX, and I've no much experience with LyX in general, and LaTeX packages especially.

I found that it's possible to plots graphs and shapes in 2D and 3D with PGFPlots.

I found some PDF that shows how to plot functions, but not geometrical shapes.
I was able to plots a function, but I'm trying to draw rectangles and I don't know how.

I'm working in LyX, and this is my Premables (edit: didn't post that initially because I assumed that most of it wasn't relevant):

\renewcommand{\labelenumi}{\alph{enumi}.}
\renewcommand{\theenumii}{(\roman{enumii})}

\usepackage{pgfplots}

\usepackage{dtk-logos}
\date{}
\usepackage[T1]{fontenc}
\usepackage{fancyhdr}
\fancyhf{}


This is my LaTeX code for the graph of the function:

\begin{tikzpicture}
\begin{axis}
\addplot [blue, line width = 1, smooth, domain=0:9] {sqrt(x)};
\end{axis}
\end{tikzpicture}


What I wanted to add is rectangle such that:

• x_i is the start x value
• x_f is the end x value
• h is the height of the rectangle

I'm describing them in this system, and I don't know if it actually works like that in PGFPlots.

The rectangles are:

1. (just a line – I suppose that I can draw a function for this)

• x_i = 0
• x_f = 1
• h = 0
• x_i = 1
• x_f = 4
• h = 1
• x_i = 4
• x_f = 9
• h = 2

I want in the graph to have a function, the rectangles, and also a name for the graph.

I'm sorry if I'm asking too much – I've no idea how to do this.
And if this question is against the rules, please tell me what rule(s) it's against.

Drawing rectangles is very simple.

The one with h = 0 is only a line.

Here is how a MWE should be:

\documentclass{article}
\usepackage{pgfplots}

\begin{document}

\begin{tikzpicture}
\begin{axis}
\addplot [blue, line width = 1, smooth, domain=0:9] {sqrt(x)};
\draw (0,0) -- (1,0);
\draw (1,0) rectangle (4,1);
\draw (4,0) rectangle (9,2);
\end{axis}
\end{tikzpicture}

\end{document}


I think ShareLaTeX tutorial could be useful for you.

Edit n. 1

It's not very sensible with such a simple shape, but just to show you the feature... you can create a pic with two parameters: base and height:

\documentclass{article}
\usepackage{pgfplots}

\tikzset{%
pics/myrec/.style n args={2}{code={%
\draw (0,0) rectangle (#1,#2);
}},
}
\begin{document}

\begin{tikzpicture}
\begin{axis}
\addplot [blue, line width = 1, smooth, domain=0:9] {sqrt(x)};
% for example you can create a pic with two parameter: base and height
\pic at (0,0) {myrec={1}{0}};
\pic at (1,0) {myrec={3}{1}};
\pic at (4,0) {myrec={5}{2}};
\end{axis}
\end{tikzpicture}

\end{document}


Edit n. 2

If you want to add other options to the pic:

• if they are equals for all the pics you can add them to the definition (see the thickness in the following example)
• if they are different for every pic you can put it like \pic [...] (see dashed and dotted) or create another parameter (see the colors).

You can also set a default for the parameters.

\documentclass{article}
\usepackage{pgfplots}
\tikzset{%
pics/myrec/.style n args={3}{code={%
\draw[very thick, #3] (0,0) rectangle (#1,#2);
}},
pics/myrec/.default={1}{0}{pink},
}
\begin{document}
\begin{tikzpicture}
\begin{axis}
\addplot [blue, line width = 1, smooth, domain=0:9] {sqrt(x)};
\pic at (0,0) {myrec};
\pic[dashed] at (1,0) {myrec={3}{1}{green}};
\pic[dotted] at (4,0) {myrec={5}{2}{red}};
\end{axis}
\end{tikzpicture}
\end{document}