# How can I complete this graph?

I am trying to draw a histogram graph using simple tikz codes. Here is my code

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots, pgfplotstable}
\usetikzlibrary{shapes.geometric,shapes.symbols,shapes.arrows,shapes.callouts}

\begin{document}
\begin{tikzpicture}[scale=1]
\draw [<->] (0,4) -- (0,0) -- (7,0);
\node at (-0.5,4) {$p(x)$};\node at (7,-0.5) {$x$};
\node at (0.25,-0.5) {\tiny{1}};
\node at (0.75,-0.5) {\tiny{1.5}};\node at (1.25,-0.5) {\tiny{2.0}}; \node at (1.75,-0.5) {\tiny{2.5}};\node at (2.25,-0.5) {\tiny{3.0}};\node at (2.75,-0.5) {\tiny{3.5}};\node at (3.25,-0.5) {\tiny{4.0}};\node at (3.75,-0.5) {\tiny{4.5}};
\node at (4.25,-0.5) {\tiny{5.0}};\node at (4.75,-0.5) {\tiny{5.5}};\node at (5.25,-0.5) {\tiny{6.0}};
\draw  (2,2) -- (2.5,2) rectangle(2.5,0);
\draw (1.5,1.7) -- (2,1.7) rectangle (2,0);
\draw  (1,1.4) -- (1.5,1.4) rectangle (1.5,0);
\draw  (0.5,1.1) -- (1,1.1) rectangle (1,0);
\draw  (0,0.8) -- (0.5,0.8)rectangle (0.5,0);
\draw  (3,0) -- (3,2.5) rectangle (2.5,2.5);
\draw  (3.5,0) -- (3.5,2)rectangle (3,2);
\draw  (4,0) --  (4,1.7)rectangle (3.5,1.7);
\draw  (4.5,0) -- (4.5,1.4) rectangle (4,1.4);
\draw  (5,0) -- (5,1.1)rectangle (4.5,1.1);
\draw (5.5,0) -- (5.5,0.8)rectangle (5,0.8);

\end{tikzpicture}
\end{document}


My problem here is the rectangles drawn are incomplete, you can see it clearly from the figure below:

Please I need a way out. Thanks

-
And where is the image? –  m0nhawk Jul 19 '13 at 8:21
It's definitely time to start using pfgplots :) –  Claudio Fiandrino Jul 19 '13 at 8:22
@ClaudioFiandrino like how? –  Hamif Jul 19 '13 at 8:23
@m0nhawk I am sorry I forgot the procedure to include the image, but I am trying to remember now. –  Hamif Jul 19 '13 at 8:24
With the package documentation or browsing "histograms" on the site. –  Claudio Fiandrino Jul 19 '13 at 8:24

\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[scale=1]
\draw [<->] (0,4) -- (0,0) -- (7,0);
\draw(1,0) rectangle (1.5,1.4);
\draw(1.5,0) rectangle (2,1.7);
\draw(2,0) rectangle(2.5,2);

\end{tikzpicture}
\end{document}


You need something like this - your rectanglesyntax is completely messed up. I've only adapted 3 of your rectangles - change the others the same way. Here:

\draw(Lower Left Coordinate) rectangle(Upper right coordinate)


Here an easier to use version with a macro:

\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\usetikzlibrary{calc}
\begin{tikzpicture}[scale=1]
\coordinate(O)at(0,0);
\coordinate(dX)at(0.5,0);
\newcommand{\RectangleforHamif}[1]{\draw(O)rectangle($(O)+(dX)+(0,#1)$);\coordinate(O) at ($(O)+(dX)$);}

\draw [<->] (0,4) -- (0,0) -- (7,0);
%"Plotting"
\foreach \i in {0.8,1.1,1.4,1.7,2}{\RectangleforHamif{\i}}
\end{tikzpicture}
\end{document}


You start at (O) (=0,0) and then just call \RectangleforHamif{Y-Value}

But indeed that whole thing should be done with a plot and not drawn per "hand".

-
Thanks for your simple answer. Claudio Fiandrino is suggesting an advance way of plotting. But, I have already stated clearly that my plot is simple and basic. –  Hamif Jul 19 '13 at 8:48
Updated it with a 2nd version which should be even easier. Rename the macro as you wish. dX is the width of 1 column - change it if you want or need to. And dont forget to include the calc - library. –  oerpli Jul 19 '13 at 8:51
The second version is wonderful. I am really enjoying it. –  Hamif Jul 19 '13 at 8:55
You should accept an answer if it solved your problem. Or edit your post to show, that there's still a question left. –  oerpli Jul 19 '13 at 11:15

Just for comparison, here's how you could do this with PGFPlots:

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.8}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis lines*=left,
ybar,
ymin=0,
bar width=0.5,
xlabel=$x$, ylabel=$y$,
ylabel style={rotate=-90}
]
1   0.8
1.5 1.1
2   1.4
2.5 1.7
3   2
3.5 2.5
4   2
4.5 1.7
5   1.4
5.5 1.1
6   0.8
};
\end{axis}
\end{tikzpicture}
\end{document}

-
of course PGFPlots is the best option. But simplicity also matters. –  Hamif Jul 19 '13 at 21:57

Do not draw lines twice. Use:

%....
\draw  (2,2) rectangle(2.5,0);
\draw (1.5,1.7)  rectangle (2,0);
\draw  (1,1.4) rectangle (1.5,0);
\draw  (0.5,1.1) rectangle (1,0);
\draw  (0,0.8) rectangle (0.5,0);
\draw  (3,0) rectangle (2.5,2.5);
\draw  (3.5,0) rectangle (3,2);
\draw  (4,0) rectangle (3.5,1.7);
\draw  (4.5,0) rectangle (4,1.4);
\draw  (5,0) rectangle (4.5,1.1);
\draw (5.5,0) rectangle (5,0.8);
%....

-
you are right. Thanks –  Hamif Jul 19 '13 at 8:51