3

I copy the code from Guide to draw charts (basic, pie, bar) from data and customize (just change the data value).

The bar width is too thin so the percentage overlap on each other. img1 So, I increase the bar width img2 Then 2 bar on the side of chart go outside (almost half)

How to fix that ?

Here is full source code that cause the problem (picture 2)

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric, arrows}
\usetikzlibrary{matrix}
\usetikzlibrary{patterns}
\usepackage{pgfplots}
\title{draw_chart}
\author{Thien Thai}
\date{August 2017}

\begin{document}

\pgfplotstableread[row sep=\\,col sep=&]{
    interval & carT & carD & carR \\
    0--2     & 51.92  & 50.84  &  51.7 \\
    2--5     & 51 & 50.53  & 50  \\
    5--10    & 51.53 & 51.51 & 52.46 \\
    }\mydata

\begin{tikzpicture}
    \begin{axis}[
            ybar,
            bar width=0.7cm,
            width=\textwidth,
            height=.5\textwidth,
            legend style={at={(0.5,1)},
                anchor=north,legend columns=-1},
            symbolic x coords={0--2,2--5,5--10},
            xtick=data,
            nodes near coords,
            nodes near coords align={vertical},
            ymin=49.5,ymax=53,
            ylabel={},
        ]
        \addplot table[x=interval,y=carT]{\mydata};
        \addplot table[x=interval,y=carD]{\mydata};
        \addplot table[x=interval,y=carR]{\mydata};
        \legend{Trips, Distance, Energy}
    \end{axis}
\end{tikzpicture}

\end{document}
Torbjørn T.
  • 206,688
  • Look for enlarge x limits. Try e.g. enlarge x limits={abs=0.5}. – Torbjørn T. Aug 30 '17 at 14:48
  • @TorbjørnT. may I have full source code. I am newbie. – Haha TTpro Aug 30 '17 at 15:43
  • Just add what I wrote there to the axis options, e.g. right after ylabel={}, – Torbjørn T. Aug 30 '17 at 16:13
  • 1
    I would suggest bar width=.8cm and enlarge x limits={abs=2*\pgfplotbarwidth}. Additional remark: set compat for pgfplots: \usepackage{pgfplots} directly followed by \pgfplotsset{compat=1.15} (1.15 is the current version) or \pgfplotsset{compat=newest}. – esdd Aug 30 '17 at 16:27

1 Answers1

4

I would suggest bar width=.8cm and enlarge x limits={abs=2*\pgfplotbarwidth}.

Additional remark: set compat for pgfplots: \usepackage{pgfplots} directly followed by \pgfplotsset{compat=1.15} (1.15 is the current version) or \pgfplotsset{compat=newest}.

enter image description here

Code:

\documentclass{article}
\usepackage[utf8]{inputenc}

\usepackage{pgfplots}% loads also tikz
\pgfplotsset{compat=1.15}%<- added

\usetikzlibrary{shapes.geometric, arrows}
\usetikzlibrary{matrix}
\usetikzlibrary{patterns}

\title{draw_chart}
\author{Thien Thai}
\date{August 2017}

\begin{document}
\pgfplotstableread[row sep=\\,col sep=&]{
    interval & carT & carD & carR \\
    0--2     & 51.92  & 50.84  &  51.7 \\
    2--5     & 51 & 50.53  & 50  \\
    5--10    & 51.53 & 51.51 & 52.46 \\
    }\mydata

\begin{tikzpicture}
    \begin{axis}[
            ybar,
            bar width=0.8cm,%<- changed
            width=\textwidth,
            height=.5\textwidth,
            legend style={at={(0.5,1)},
                anchor=north,legend columns=-1},
            symbolic x coords={0--2,2--5,5--10},
            xtick=data,
            nodes near coords,
            nodes near coords align={vertical},
            ymin=49.5,ymax=53,
            ylabel={},
            enlarge x limits={abs=2*\pgfplotbarwidth}
        ]
        \addplot table[x=interval,y=carT]{\mydata};
        \addplot table[x=interval,y=carD]{\mydata};
        \addplot table[x=interval,y=carR]{\mydata};
        \legend{Trips, Distance, Energy}
    \end{axis}
\end{tikzpicture}
\end{document}

Maybe you want to add

nodes near coords style={/pgf/number format/.cd,fixed,fixed zerofill,precision=2},

to the axis options to get

enter image description here

Or with precision=1:

enter image description here

esdd
  • 85,675