0

I created a bar chart, but it does not look good:

enter image description here

Problems:

  1. the values above the bars does not look good. I want to have it on top of the bars and with slightly smaller font size;
  2. The lay-out looks very cramped. I want to control the font size of the bar charts if possible;
  3. I want to have a larger spacing between each groups, i.e., more spacing between 1 and 2, 2 and 3 etc;
  4. I want to have the legend (Measured & Model) on top of each other, rather than side by side;
  5. Group 9 does not appear on the bar chart, but I have it in my code;
  6. I do not want spacing between the bars (i.e. no spacing between blue and red).

This is what I used:

    \usepackage{pgfplots}
    \pgfplotsset{compat=newest}
        \begin{adjustbox}{width=\textwidth, height= 75mm}
        \begin{tikzpicture}
        \begin{axis}[
        x tick label style={
        /pgf/number format/1000 sep=},
        ylabel=$H_{m0}$,
        xlabel= {Position},
        enlargelimits=0.05,
        legend style={at={(0,1)},
          anchor=north,legend columns=-1},
            nodes near coords,
            every node near coord/.append style={font=\tiny},
           nodes near coords align={vertical},
           ybar interval=0.8,
        ]
\addplot [gray!20!gray,fill=gray!80!gray]
    coordinates {(01,0.150) (02,0.166)
         (03,0.174) (04,0.186) (05,0.187) (06,0.152) (07,0.218) (08,0.187) (09,0.156)};
\addplot  [black!20!black,fill=black!80!black]
    coordinates {(01,0.153) (02,0.162) 
         (03,0.178) (04,0.177) (05,0.222) (06,0.233) (07,0.323) (08,0.301) (09,0.233)};
        \legend{Measured, Model}
        \end{axis}
        \end{tikzpicture}
        \end{adjustbox} 
2

Use ybar=0pt instead ybar interval. Adjust the width of the bars to your needs. Set width and height for the axis environment. Remove legend columns=-1.

enter image description here

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.13}
\begin{document}
\begin{center}
  \begin{tikzpicture}
    \begin{axis}[
        width=\textwidth,height=75mm,% <- added
        x tick label style={/pgf/number format/1000 sep=},
        ylabel=$H_{m0}$,
        xlabel= {Position},
        %enlargelimits=0.05,% <- commented, default is .1
        legend style={
          at={(0,1)},
          anchor=north west,% <- changed
          %legend columns=-1% <- commented
        },
        nodes near coords,
        every node near coord/.append style={font=\tiny},
        %nodes near coords align={vertical},% <- commented, default
        ybar=0pt,%<- changed
        bar width=13pt% <- added
      ]
      \addplot
        coordinates {(01,0.150) (02,0.166)
          (03,0.174) (04,0.186) (05,0.187) (06,0.152) (07,0.218) (08,0.187) (09,0.156)};
      \addplot
        coordinates {(01,0.153) (02,0.162) 
          (03,0.178) (04,0.177) (05,0.222) (06,0.233) (07,0.323) (08,0.301) (09,0.233)};
      \legend{Measured, Model}
    \end{axis}
  \end{tikzpicture}
\end{center}
\end{document}

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.