2

i've successfully created my first 'bar chart' today:

enter image description here

by the use of the code given below:

\documentclass[tikz,border=1pt]{standalone} 
\usepackage[ngerman]{babel}                         
\usepackage[ansinew]{inputenc}  
\usepackage[T1]{fontenc}                            
\usepackage{graphicx} 
\usepackage{pgfplots}
\usepackage{amsmath} 
\usepackage{units}
\usetikzlibrary{matrix}

\pgfplotsset{compat=1.10, 
        saeule-style/.style={
          title={\textbf{Bar Chart}},
        },
        height=6cm,
        width=20cm,
        grid=major,
        grid style={
          solid,
          ultra thin,
          gray
        },
        xmin=1925,
        xmax=1975,
        xtick={1925,1930,...,1975},
        x tick label style={
            rotate=90
        },
        x tick style={
          color=black,
          thin
        },
        ymin=0,
        ymax=10,
        y tick style={
          color=black,
          thin
        },
        xlabel={\textbf{Year}},
      ylabel={\textbf{Population}},
      legend columns=2,     
        legend style={
            font=\scriptsize,
            legend pos=north west,
            draw=none,
            /tikz/column 2/.style={
        column sep=10pt,
      }
        },
      ybar=5pt,
      nodes near coords,
      /pgf/number format/.cd,
      use comma,
      set thousands separator={},
} 


\begin{document}     

 \begin{tikzpicture} 
    \begin{axis}[saeule-style]
        \addplot[ybar, bar width=0.2cm, fill=blue, draw=black] coordinates {(1930,5) (1940,3) (1950,4) (1960,5) (1970,7)};
            \addplot[ybar, bar width=0.2cm, fill=red, draw=black] coordinates {(1930,3) (1940,4)    (1950,4) (1960,4) (1970,6)};

            \legend{Far,Near}
    \end{axis} 
 \end{tikzpicture} 

\end{document} 

As you can see, I added those x- and ytics. As I expected, the ytics were drawn in the diagram. Unfortunately, the xtics don't. It seems that something gave them a kind of "outer"-option.

Does anyone of you know how I can place them also in the diagram?

Thank you in advance, eniem


EDIT: Hmm ... now tried to use the 'xtick align=inside'-option, but it doesn't work. Does anybody know why?

\documentclass[tikz,border=1pt]{standalone}     
\usepackage[ngerman]{babel}                     
\usepackage[ansinew]{inputenc}                  
\usepackage[T1]{fontenc}                            
\usepackage{graphicx} 
\usepackage{pgfplots}
\usepackage{amsmath} 
\usepackage{units}

\pgfplotsset{compat=newest, 
        saeule-style/.style={
          title={\textbf{Bar Chart}},
        },
        height=6cm,
        width=20cm,
        grid=major,
        grid style={
          solid,
          ultra thin,
          gray
        },
        xmin=1925,
        xmax=1975,
        xtick={1925,1930,...,1975},
        xtick align=inside,
        xticklabel style={
            rotate=90
        },
        xtick style={
          color=black,
          thin
        },
        ymin=0,
        ymax=10,
        ytick align=inside,
        y tick style={
          color=black,
          thin
        },
        xlabel={\textbf{Year}},
      ylabel={\textbf{Population}},
      legend columns=2,     
        legend style={
            font=\scriptsize,
            legend pos=north west,
            draw=none,
            /tikz/column 2/.style={
        column sep=10pt,
      }
        },
      ybar=5pt,
      /pgf/number format/.cd,
      use comma,
      set thousands separator={},
} 


\begin{document} 

 \begin{tikzpicture} 
    \begin{axis}[saeule-style]
        \addplot[ybar, bar width=6pt, fill=blue, draw=black] coordinates {(1930,5) (1940,3) (1950,4) (1960,5) (1970,7)};
        \addlegendentry{Far}
            \addplot[ybar, bar width=6pt, fill=red, draw=black] coordinates {(1930,3) (1940,4)  (1950,4) (1960,4) (1970,6)};
            \addlegendentry{Near}
        \end{axis} 
 \end{tikzpicture} 

\end{document} 

Thank you in advance! eniem

eniem
  • 189

1 Answers1

2

Quoting the manual for ybar;

It changes the legend, draws ticks outside of the axis lines and draws multiple \addplot arguments adjacent to each other;....

So it expected. You can override this via small adjustments though, xtick align=inside will do it automatically for you as @Jake reminded in the comment.

\documentclass[border=1pt]{standalone} 
\usepackage{pgfplots}
\pgfplotsset{compat=1.10, 
        xtick={1925,1930,...,1975},
        x tick label style={rotate=90,xshift=-4mm},
        ybar=5pt,
        xtick style={/pgfplots/major tick length=-2mm,draw=black,thick}
} 


\begin{document}     

 \begin{tikzpicture} 
    \begin{axis}
        \addplot[ybar, bar width=0.2cm, fill=blue, draw=black] coordinates {(1930,5) (1940,3) (1950,4) (1960,5) (1970,7)};
            \addplot[ybar, bar width=0.2cm, fill=red, draw=black] coordinates {(1930,3) (1940,4)    (1950,4) (1960,4) (1970,6)};
    \end{axis} 
 \end{tikzpicture} 
\end{document}

enter image description here

percusse
  • 157,807
  • Or you could set xtick align=inside, of course =) – Jake Aug 18 '14 at 16:41
  • 1
    @Jake Pfff,...are you reading the manual secretly? :P – percusse Aug 18 '14 at 16:57
  • @Jake thanks for your answer. - I tried out to use xtick align=inside like I added in my first question above. Unfortunately it doesn't work. And I don't know why. Maybe you could help me ... – eniem Aug 19 '14 at 09:29
  • 1
    @eniem Did you try putting it after ybar=5pt? – percusse Aug 19 '14 at 09:31
  • 2
    @eniem: As percusse said, you need to put xtick align=inside after ybar=5pt, since the ybar option calls (among other things) xtick align=outside, which overwrites the previous xtick align option. – Jake Aug 19 '14 at 09:48