Tikz-Pgf Graphics – How to Create Graphs, Fill in Boxes, and Put Labels Using TikZ

graphicstikz-pgf

I need to make the following graph.enter image description here

And I carry the following, with the following code,

\begin{center}

 \begin{tikzpicture}[scale=.9]

\draw[<->] (-1,0) -- (2*pi,0) node[below] {$x$};

\draw[thick] (4,0)--(4,1/2);

\draw[thick] (4,0)--(4,-1/2);

\draw[thick] (1.3,1/2)--(1.3,-1/2);

\draw[thick] (2.6,1/2)--(2.6,-1/2);

\draw (0,-1/6) node[left] {};

\draw[<->] (0,-1) -- (0, 3) node[left] {$y$};

\draw[-,domain = 0:4,thick]

 plot (\x,{1/2}) node[right] {};

\draw[-,domain = 0:4,thick]

 plot (\x,{-1/2}) node[right] {};

 \end{tikzpicture}

\end{center}

enter image description here

I would need to fill in the table and put the labels. Someone could help me on that, thanks.

Best Answer

You can put the labels with \nodes the same way as you did in the axes. For the filling the TikZ library patterns could be helpful. And finally, you can draw the brace with the library decorations.pathreplacing.

I changed your original scale, otherwise the text will be very big, in my opinion. You can revert that or change the text size if you want.

The code:

\documentclass[border=2mm,tikz]{standalone}
\usetikzlibrary{patterns}
\usetikzlibrary{decorations.pathreplacing} % for the overbrace

\tikzset%
{% this sytle provides the overbrace (form decorations.pathreplacing)
  mybrace/.style={decorate,decoration={brace,raise=2mm,amplitude=3pt}},
}

\begin{document}
\begin{tikzpicture}[scale=1.5,line join=round,line cap=round]
% axes
\draw[<->] (-1,0) -- (2*pi,0) node[below] {$x$};
\draw[<->] (0,-1) -- (0, 3)   node[left]  {$y$};
% big rectangle (and nodes, labels)
\draw[thick] (0,-1/2) node [below left] {\strut$x=0$} -- (4,-1/2) node [below] {\strut$x=\ell$} |- (0,1/2);
% filled rectangle (and another node)
\draw[thick,pattern=north east lines] (1.3,-1/2) node [below] {\strut$x$} rectangle (2.6,1/2);
% one more node
\node at (2.6,-1/2) [below] {$x+\Delta x$};
% overbrace
\draw[mybrace]   (1.3,1/2) -- (2.6,1/2) node[midway,yshift=5mm] {\strut $\Delta x$};
\end{tikzpicture}
\end{document}

enter image description here