1

I have the following MWE:

\documentclass{standalone}

\usepackage{tikz}
\usepackage{pgfplots}

\pgfplotsset{compat=1.11,
    /pgfplots/ybar legend/.style={
        /pgfplots/legend image code/.code={%
            \draw[##1,/tikz/.cd,yshift=-0.25em]
            (0cm,0cm) rectangle (0.8em,0.8em);},
    },
}

\begin{document}


\begin{tikzpicture}
\scriptsize
\begin{axis}[
ybar, bar width = 0.2cm,
axis lines=left, 
enlargelimits = 0.15,
% y and x axis ticks
y tick label style={/pgf/number format/.cd, fixed, fixed zerofill,precision=1,/tikz/.cd},
ymin = 0.0,
xtick=data,
x tick label style={rotate = 90},
symbolic x coords={US, Euro, UK, PR China, Singapore, Indonesia, Malaysia, Korea, Taiwan, Philippines},
% legends and labels
legend style={at={(0.5, 0.9)},anchor=north},
ylabel={Annual change (\%)},
y label style={at={(axis description cs:0.15,1)},rotate=-90,anchor=south},
% data labels
nodes near coords,
nodes near coords style={/pgf/number format/.cd, fixed, fixed zerofill,precision=1,/tikz/.cd},
]
\addplot[red, fill = red] coordinates {(US,2.0) (Euro,1.2) (UK,1.2) (PR China,6.1) (Singapore,0.2) (Indonesia,5.1) (Malaysia,4.7) (Korea, 2.1) (Taiwan, 1.7) (Philippines,5.6)};
\addplot[green!20!black, fill = green!20!black] coordinates {(US,1.9) (Euro,1.1) (UK,1.0) (PR China,6.0) (Singapore,0.1) (Indonesia,5.0) (Malaysia,4.4) (Korea,2.0) (Taiwan,2.9) (Philippines,5.9)};
%\addplot[red, fill = red] coordinates {(US,2.0) (Euro,1.2) (UK,1.2) (China,6.1)};
%\addplot[green!20!black, fill = green!20!black] coordinates {(US,1.9) (Euro,1.1) (UK,1.0) (China,6.0)};
\legend{\phantom{a}2Q19, \phantom{a}3Q19}
\end{axis}
\end{tikzpicture}

\end{document}

which produces this:

enter image description here

Most adjustments, I've been able to find out how to do. There are 3 which I'm struggling with:

  1. How do I remove the y-axis space below 0? It's starting at -0.5 despite me declaring ymin = 0.0.
  2. How do I remove all the data labels on the red bars? I only want to call out the data for the 3Q19 series (the black bars).
5
  1. The enlargelimits=0.15 is adding extra space below the data and to the left of the data. By setting enlargelimits=false and then enlarge x limits=0.15 you can fix the axis issue.
  2. The nodes near coords is creating the label for the two data series. In the solution below, I've removed this from the options for the axis, and added it to the options for the \addplot.

The resulting code is this:

\documentclass{standalone}

\usepackage{tikz}
\usepackage{pgfplots}

\pgfplotsset{compat=1.11,
    /pgfplots/ybar legend/.style={
        /pgfplots/legend image code/.code={%
            \draw[##1,/tikz/.cd,yshift=-0.25em]
            (0cm,0cm) rectangle (0.8em,0.8em);},
    },
}

\begin{document}


\begin{tikzpicture}
\scriptsize
\begin{axis}[
ybar, bar width = 0.2cm,
axis lines=left, 
enlargelimits=false,
enlarge x limits=0.15,
% y and x axis ticks
y tick label style={/pgf/number format/.cd, fixed, fixed zerofill,precision=1,/tikz/.cd},
ymin = 0.0,
xtick=data,
x tick label style={rotate = 90},
symbolic x coords={US, Euro, UK, PR China, Singapore, Indonesia, Malaysia, Korea, Taiwan, Philippines},
% legends and labels
legend style={at={(0.5, 0.9)},anchor=north},
ylabel={Annual change (\%)},
y label style={at={(axis description cs:0.15,1)},rotate=-90,anchor=south},
nodes near coords style={/pgf/number format/.cd, fixed, fixed zerofill,precision=1,/tikz/.cd},
]
\addplot[red, fill = red] coordinates {(US,2.0) (Euro,1.2) (UK,1.2) (PR China,6.1) (Singapore,0.2) (Indonesia,5.1) (Malaysia,4.7) (Korea, 2.1) (Taiwan, 1.7) (Philippines,5.6)};
\addplot[green!20!black, fill = green!20!black, nodes near coords] coordinates {(US,1.9) (Euro,1.1) (UK,1.0) (PR China,6.0) (Singapore,0.1) (Indonesia,5.0) (Malaysia,4.4) (Korea,2.0) (Taiwan,2.9) (Philippines,5.9)};
%\addplot[red, fill = red] coordinates {(US,2.0) (Euro,1.2) (UK,1.2) (China,6.1)};
%\addplot[green!20!black, fill = green!20!black] coordinates {(US,1.9) (Euro,1.1) (UK,1.0) (China,6.0)};
\legend{\phantom{a}2Q19, \phantom{a}3Q19}
\end{axis}
\end{tikzpicture}

\end{document}

For me this compiles as follows. Note that this does not look quite like your screenshot, I suspect because your screenshot was not a screenshot of the MWE, but a screenshot from your original file which has some other settings in it.

Bar chart screenshot

5

Edit: A small variation of @rbrgnall answer (+1), with use of table (differences are marked in code by % <---%):

\documentclass[margin=3mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}       % <---
\usepackage{pgfplotstable}      % <---
\usepackage{siunitx}            % <---

\pgfplotstableread[col sep=comma]%  % <--- 
{
state,          2Q19,   3Q19
US,             2.0,    1.9
Euro,           1.2,    1.1
UK,             1.2,    1.0
PR China,       6.1,    6.0
Singapore,      0.2,    0.1
Indonesia,      5.1,    5.0
Malaysia,       4.7,    4.4
Korea,          2.1,    2.0
Taiwan,         1.7,    2.9
Philippines,    5.6,    5.9
}\mydata

\begin{document}
    \begin{tikzpicture}
\begin{axis}[x=9mm,             % <---
ybar,
bar width = 3.4mm,              % <--
axis lines=left,
enlarge x limits=0.1,           % <---
enlarge y limits={.2, upper},   % <---
% y and x axis ticks
y tick label style={/pgf/number format/.cd, fixed, fixed zerofill, precision=1,
                    /tikz/.cd, font=\small},
ymin = 0,
xtick=data,
x tick label style={rotate = 90},
symbolic x coords={US, Euro, UK, PR China, Singapore, 
                   Indonesia, Malaysia, Korea, Taiwan, Philippines},
% legends and labels
legend style={legend columns=-1,                                        % <---
              /tikz/every even column/.append style={column sep=1em}},  % <---
%y label style={at={(0.15,1)},rotate=-90,anchor=south},  % I prefer label along y axes
nodes near coords style={/pgf/number format/.cd, fixed, fixed zerofill, precision=1,
                         /tikz/.cd, font=\scriptsize},
legend style={legend columns=-1},
ylabel={Annual change (\si{\percent})},
]
\addplot[red, fill = red]
    table [y=2Q19]  \mydata;    % <---
\addplot[green!20!black, fill = green!20!black, nodes near coords]
    table [y=3Q19]  \mydata;    % <---
\legend{~2Q19, ~3Q19}
\end{axis}
    \end{tikzpicture}
\end{document}

enter image description here

3
  • Good job. The data is indeed more readable this way. Maybe one could use the small style or something similar, instead of \scriptsize. – frougon Feb 11 '20 at 13:29
  • @frougon, thank you! Regardin fonts size, I agree with you, I will correct OP settings :-) (asap) – Zarko Feb 11 '20 at 14:41
  • Also, you can use row sep=\\ and avoid all the commas... but at some point, it's just a matter of user preference, so I won't add more “also“s. :-) (note: even if tempting, I wouldn't advise row sep=newline because it can cause a lot of hair pulling when the table is read in a macro, as done here). – frougon Feb 11 '20 at 14:44

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.