0

I am beginner in using pgfplots package, I am trying to edit this example but seriously it was hard for me to understand what is the purpose of each parameter in the code like how can I change the color with a certain bar in HTML or any other format. How can I control the number and the value of each column in the chart? how can I control the exact position of the legend? Please if you have any resources could explain this in easy way for beginners suggest it to me.

\documentclass{article}

\usepackage{pgfplots}
\pgfplotsset{width=7cm,compat=newest}

\begin{document}


    \begin{tikzpicture} \begin{axis}
  [ x tick label style={ /pgf/number format/1000 sep=},
ylabel=Population,
enlargelimits=0.05,
legend style={at={(0.5,-0.15)},
anchor=north,legend columns=-1},
ybar interval=0.7, ] 
\addplot
coordinates {(1930,50e6) (1940,33e6) (1950,40e6) (1960,50e6) (1970,70e6)};
\addplot
coordinates {(1930,38e6) (1940,42e6) (1950,43e6) (1960,45e6) (1970,65e6)};
\addplot
coordinates {(1930,15e6) (1940,12e6) (1950,13e6) (1960,25e6) (1970,35e6)};
\legend{Far,Near,Here} 
\end{axis} 
\end{tikzpicture}

\end{document}

Thank you

User505
  • 287

1 Answers1

1

I hope to answer some of your questions. The part "How can I control the number and the value of each column in the chart?" I did not understand. The legend position in defined in a coordinate system, in which the coordinates of the plot range from 0 to 1. Hence, at={(0.5,0.95) together with anchor=north moves the legend to the middle top position. I also added a way to control the colors, stealing the color definitions from an older answer.

\documentclass{article}

\usepackage{pgfplots}
\pgfplotsset{width=7cm,compat=newest}

% from https://tex.stackexchange.com/a/37660/121799
\definecolor{RYB1}{RGB}{141, 211, 199}
\definecolor{RYB2}{RGB}{255, 255, 179}
\definecolor{RYB3}{RGB}{190, 186, 218}
\definecolor{RYB4}{RGB}{251, 128, 114}
\definecolor{RYB5}{RGB}{128, 177, 211}
\definecolor{RYB6}{RGB}{253, 180, 98}
\definecolor{RYB7}{RGB}{179, 222, 105}


\begin{document}


    \begin{tikzpicture} \begin{axis}
  [ x tick label style={ /pgf/number format/1000 sep=},
ylabel=Population,
enlargelimits=0.05,
legend style={at={(0.5,0.95)}, %<-changed
anchor=north,legend columns=-1},
ybar interval=0.7, ] 
\addplot[fill=RYB1]
coordinates {(1930,50e6) (1940,33e6) (1950,40e6) (1960,50e6) (1970,70e6)};
\addplot[fill=RYB2]
coordinates {(1930,38e6) (1940,42e6) (1950,43e6) (1960,45e6) (1970,65e6)};
\addplot[fill=RYB3]
coordinates {(1930,15e6) (1940,12e6) (1950,13e6) (1960,25e6) (1970,35e6)};
\legend{Far,Near,Here} 
\end{axis} 
\end{tikzpicture}
  • Thank you @marmot for your answer. Do you know why there is a number (·10^7) at the top of the chart? and how can I control its size? – User505 Apr 05 '18 at 20:45
  • @User505 10^7 is there because your y-coordinates are of the order 10^7. E.g. 50e6 is 5\cdot 10^7. –  Apr 05 '18 at 20:47
  • So is there a way to remove it?and how can I change the size of the chart frame? – User505 Apr 05 '18 at 21:10
  • @User505 Remove it while keeping the coordinates? The size can be changed e.g. by adjusting the width in \pgfplotsset{width=7cm}. –  Apr 05 '18 at 21:31