0

Here in my MWE, you will find a histogram. In which i would like to change labeling of y-axis. I want {10, 20, 30, .....100} in place of {1 , 2 , ..10} Please make correction in same as well as provide any other alternate for changing and making axis label in smooth way.

\documentclass[border=5mm]{standalone}

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


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

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

    \foreach \x in {1,...,14}
    \draw[gray!50, text=black] (0 cm,\x cm) -- (14 cm,\x cm) 
    node at (-0.5 cm,\x cm) {\x};

    \draw (1cm,0cm) rectangle (2cm,3cm) node at (1.5cm,3.4cm) {14};

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

    \draw[fill=myblue] (1cm,0cm) rectangle (2cm,3cm)
    node at (1.5cm,3.4cm) {14};

    \draw (2cm,0cm) rectangle (3cm,3cm) node at (2.5cm,3.4cm) {56};

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

    \draw[fill=myblue] (2cm,0cm) rectangle (3cm,3cm)
    node at (2.5cm,3.4cm) {56};

    \draw (3cm,0cm) rectangle (4cm,3cm) node at (3.5cm,3.4cm) {60};

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

    \draw[fill=myblue] (3cm,0cm) rectangle (4cm,3cm)
    node at (3.5cm,3.4cm) {60};

    \draw (4cm,0cm) rectangle (5cm,3cm) node at (4.5cm,3.4cm) {86};

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

    \draw[fill=myblue] (4cm,0cm) rectangle (5cm,3cm)
    node at (4.5cm,3.4cm) {86};

    \draw (5cm,0cm) rectangle (6cm,3cm) node at (5.5cm,3.4cm) {74};

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

    \draw[fill=myblue] (5cm,0cm) rectangle (6cm,3cm)
    node at (5.5cm,3.4cm) {74};

    \draw (6cm,0cm) rectangle (7cm,3cm) node at (6.5cm,3.4cm) {62};

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

    \draw[fill=myblue] (6cm,0cm) rectangle (7cm,3cm)
    node at (6.5cm,3.4cm) {62};

    \draw (7cm,0cm) rectangle (8cm,3cm) node at (7.5cm,3.4cm) {48};

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

    \draw[fill=myblue] (7cm,0cm) rectangle (8cm,3cm)
    node at (7.5cm,3.4cm) {48};
    \end{tikzpicture}
\end{document}
  • Just FYI, you don't need to add \definecolor{myblue}{HTML}{92dcec} every time; you just need to use it once, at the beginning, and then it will be defined throughout your document. – Arun Debray Dec 16 '15 at 5:16
2

Maybe this can help you. You can avoid writing the same stuff several times.

I use \the\numexpr\x*10 for calculating the labels and then each bar according with your design.

\documentclass[border=5mm]{standalone}

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

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


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

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

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

\foreach \y [count=\x from 1] in {14, 56, 60, 36, 74, 62, 48} 
    {\draw[fill=myblue] (\x,0) rectangle (\x + 1, \y / 10);
    \node at (\x+.5,\y / 10 + .4) {\y};};

\end{tikzpicture}

\end{document}

This is the output:

histogram

  • A big big thanks to you.......i find something new hints from this . – snehal Dec 16 '15 at 6:15
1

Another solution :

 \foreach \x / \y in {
   1/10,
   2/20,
   3/30,
   4/40,
   5/50,
   6/60,
   7/70,
   8/80,
   9/90,
   10/100,
   11/110,
   12/120,
   13/130,
   14/140
 }
 \draw[gray!50, text=black] (0 cm,\x cm) -- (14 cm,\x cm) 
 node at (-0.5 cm,\x cm) {\y};

This solution allows you to put whatever you want in this y-axis label.

0

You can just add a 0 to the text at each node on your y-axis:

\foreach \x in {1,...,14}
\draw[gray!50, text=black] (0 cm,\x cm) -- (14 cm,\x cm) 
node at (-0.5 cm,\x cm) {\x 0}; % <-- here

You can also replace the {\x 0} with a calculation {\the\numexpr\x * 10}, as in this answer, i.e.

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

This has the advantage that you can replace 10 with any other number and get them numbered by twos or fives or whatever you want.

Either way, here's what the output looks like.

enter image description here

  • I wanna ask one more question related to previous one. – snehal Dec 16 '15 at 8:07
  • How can i keep a blank of 1cm between each bar in the above MWE. – snehal Dec 16 '15 at 8:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.