2

See my following work of making histogram, now i want to leave one centimeter blank space between each bars what are necessary correction to achieve that?

\documentclass[border=5mm]{standalone}

\usepackage{pgfplots}
\pgfplotsset{compat=1.7}

\begin{document}
    \begin{tikzpicture}
    \draw[<->] (-1cm,0cm) -- (13cm,0cm) node [right] {\large $x$};  %Abzisse

\draw[<->] (0cm,-1cm) -- (0cm,8cm) node [left] {\large $y$};  %Ordinate

\foreach \x in {1,...,7}
\draw[gray!50, text=black] (0 cm,\x cm) -- (13 cm,\x cm) 
    node at (-0.5 cm,\x cm) 
            {\the\numexpr\x*5};

\definecolor{myblue}{HTML}{92dcec}

\foreach \x / \y in {
    1 / 30,
    2 / 25,
    3 / 20,
    4 / 15,
    5 / 10,6 / 5} 
    {%
        \draw[fill=myblue] (\x,0) rectangle (\x + 1, \y/5 );
        \node at (\x+.5,\y/5  + .4) {\y};

};
 \node[rotate=0, below] at (3.5cm, -0.9cm) {$\longrightarrow$}; 

 \node[rotate=90, left] at (-1.3cm, 6cm) { $\longrightarrow$}; 

 \node[rotate=00, left] at (1.8cm, -0.5cm) {$1$};
 \node[rotate=00, left] at (2.8cm, -0.5cm) {$2$};
 \node[rotate=00, left] at (3.8cm, -0.5cm) {$3$};
 \node[rotate=00, left] at (4.8cm, -0.5cm) {$4$};
 \node[rotate=00, left] at (5.8cm, -0.5cm) {$5$};
 \node[rotate=00, left] at (6.8cm, -0.5cm) {$6$};


\end{tikzpicture}
\end{document}
6
  • Why aren't you using pgfplots? It's much more suited to draw plots and graphs.
    – Alenanno
    Commented Dec 16, 2015 at 8:33
  • Did you mena "black" space or "blank" space? Commented Dec 16, 2015 at 9:05
  • @TomBombadil Oh I had assumed he meant "blank", also considering the title.
    – Alenanno
    Commented Dec 16, 2015 at 9:15
  • @Alenanno: Yes there's a collision between title and body of the question. I thinks Bibi also read the "black" in the question's body. Commented Dec 16, 2015 at 9:18
  • @TomBombadil Yes. Although I have to say I would have found it odd to have black bars there, they only disturb the reader when viewing the chart.
    – Alenanno
    Commented Dec 16, 2015 at 9:20

2 Answers 2

2

I think pgfplots would be better suited for drawing an bar chart. If you have any doubts about the code, feel free to comment.

Output

example image

Code

\documentclass[margin=20pt]{standalone}
\usepackage{pgfplots}

\pgfplotsset{compat=1.7}
\definecolor{myblue}{HTML}{92dcec}

\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        ybar,
        x=2cm,
        bar width=1cm,
        axis lines=center,
        enlargelimits=false,
        enlarge x limits=false,
        xmin=0,xmax=7,
        ymin=0,ymax=40,
        ytick={5,10,15,20,25,30,35},
        xtick={1,2,3,4,5,6},
        nodes near coords,
        ymajorgrids,
        xlabel={$x$},
        ylabel={$y$},
        x label style={at={(axis description cs:1,0)}, anchor=west},
        y label style={at={(axis description cs:0,1)}, anchor=east},
        axis line style={shorten <=-1cm,<->},
        after end axis/.code={
            \node at (axis description cs:.5,-.15) {$\longrightarrow$};
            \node[rotate=90] at (axis description cs:-.1,.5) {$\longrightarrow$};
        }
        ]

        \addplot[fill=myblue] coordinates {(1,30) (2,25) (3,20) (4,15) (5,10) (6,5)};   
    \end{axis}
\end{tikzpicture}
\end{document}
0
1

Is that what you are looking for?

enter image description here

\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.7}
\begin{document}
\begin{tikzpicture}
\draw[<->] (-1cm,0cm) -- (13cm,0cm) node [right] {\large $x$}; %Abzisse
\draw[<->] (0cm,-1cm) -- (0cm,8cm) node [left] {\large $y$};  %Ordinate
\foreach \x in {1,...,7}
    \draw[gray!50, text=black] (0 cm,\x cm) -- (13 cm,\x cm) node at (-0.5 cm,\x cm) {\the\numexpr\x*5};
\definecolor{myblue}{HTML}{92dcec}
\foreach \x / \y in {
1 / 30,
2 / 25,
3 / 20,
4 / 15,
5 / 10,
6 / 5
} 
{%
\draw[fill=myblue] (\x,0) rectangle (\x + 0.5, \y/5 );
\draw[fill=black] (\x + 0.5,0) rectangle (\x + 1, \y/5 );
\node at (\x+.25,\y/5  + .4) {\y};
\node at (\x+.25,-0.5) {\x};
};
\node[rotate=0, below] at (3.5cm, -0.9cm) {$\longrightarrow$}; 
\node[rotate=90, left] at (-1.3cm, 6cm) { $\longrightarrow$};

\end{tikzpicture}
\end{document}
3
  • without reducing width of bar, i want space between each bars.
    – snehal
    Commented Dec 16, 2015 at 8:56
  • This not not exactly what i want, please help me
    – snehal
    Commented Dec 16, 2015 at 8:59
  • 1
    Well, you did ask for "black" space... :)
    – Bibi
    Commented Dec 16, 2015 at 10:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .