1

I produce the following graph:

enter image description here

With this code:

\documentclass{standalone}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usepgfplotslibrary{dateplot}
\pgfplotsset{compat=1.8}

%colors
\usepackage{color} % colors
\usepackage{xcolor} 
\definecolor{colourone}{RGB}{22,131,198}
\definecolor{colourtwo}{RGB}{202,211,43}

\begin{document}

\pgfplotstableread[col sep=comma]{
    country,        3Q19,   4Q19
    PR China,       -0.5,    1.9
    C.\ Taipei,     -1.0,    1.3
    Hong Kong SAR,  -6,    -2.5
    Malaysia,       -3,    -3.2
    Indonesia,      -7,    -3.8
    Thailand,       -0.5,    -4.5
    Singapore,      -12,    -4.6
    Korea,          -13,    -11.7
}\charttwo

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

\begin{tikzpicture}
\begin{axis}[
x=8.5mm,
ybar,
bar width=2mm,
axis lines=left,
axis x line shift=-14,
ylabel shift = 1pt,
xlabel shift = -10,
enlarge x limits=0.1,
enlarge y limits={.2, upper},
%
% y ticks style and label
ylabel={Annual change (\%)},
ymin=-14,
ytick distance = 2,
%y tick label style={/pgf/number format/.cd, fixed, fixed zerofill, precision=1, /tikz/.cd, font=\scriptsize},
%
% x axis ticks and style
xtick=data,
xticklabels from table={\charttwo}{country},  
table/x expr = \coordindex,                     
x tick label style = {rotate=90},
%
% legends and labels
legend style = {draw=none,
    legend columns=-1,
    at={(0.6,1)},
    anchor=north,
    /tikz/every even column/.append style={column sep=2em},
},
%
% nodes near coordinates
nodes near coords style = { /pgf/number format/.cd,
    fixed, fixed zerofill, precision=1,
    /tikz/.cd, font=\scriptsize, color=black,
    yshift=0,xshift=0.1cm,
},
]
%
% done with the axis, now the plots
\addplot [colourtwo, fill=colourtwo]
table [y=3Q19]  {\charttwo};
\addlegendentry{3Q19};
\addplot [colourone, fill=colourone,
nodes near coords]                        % <-------------------
table [y=4Q19]  {\charttwo};
\addlegendentry{4Q19};
\end{axis}
\end{tikzpicture}

\end{document}

I wanted to have the x-axis at y=0. My ymin = -14, so I achieved this with axis x line shift=-14.

However, this also shifts the x-axis labels up, as you can see. How do I put them back down (as if I didn't shift the axis up, i.e. shift them back down by 14 units)?

1 Answer 1

5

There is the option xticklabel shift, that you can pass to the axis-environment.


MWE:

\documentclass{standalone}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usepgfplotslibrary{dateplot}
\pgfplotsset{compat=1.8}

%colors
\usepackage{color} % colors
\usepackage{xcolor} 
\definecolor{colourone}{RGB}{22,131,198}
\definecolor{colourtwo}{RGB}{202,211,43}

\begin{document}

\pgfplotstableread[col sep=comma]{
    country,        3Q19,   4Q19
    PR China,       -0.5,    1.9
    C.\ Taipei,     -1.0,    1.3
    Hong Kong SAR,  -6,    -2.5
    Malaysia,       -3,    -3.2
    Indonesia,      -7,    -3.8
    Thailand,       -0.5,    -4.5
    Singapore,      -12,    -4.6
    Korea,          -13,    -11.7
}\charttwo

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

\begin{tikzpicture}
\begin{axis}[
x=8.5mm,
ybar,
bar width=2mm,
axis lines=left,
axis x line shift=-14,
ylabel shift={1pt},
xlabel shift={-10pt},
xticklabel shift={120pt},
enlarge x limits=0.1,
enlarge y limits={.2, upper},
%
% y ticks style and label
ylabel={Annual change (\%)},
ymin=-14,
ytick distance = 2,
%y tick label style={/pgf/number format/.cd, fixed, fixed zerofill, precision=1, /tikz/.cd, font=\scriptsize},
%
% x axis ticks and style
xtick=data,
xticklabels from table={\charttwo}{country},  
table/x expr = \coordindex,                     
x tick label style = {rotate=90},
%
% legends and labels
legend style = {draw=none,
    legend columns=-1,
    at={(0.6,1)},
    anchor=north,
    /tikz/every even column/.append style={column sep=2em},
},
%
% nodes near coordinates
nodes near coords style = { /pgf/number format/.cd,
    fixed, fixed zerofill, precision=1,
    /tikz/.cd, font=\scriptsize, color=black,
    yshift=0,xshift=0.1cm,
},
]
%
% done with the axis, now the plots
\addplot [colourtwo, fill=colourtwo]
table [y=3Q19]  {\charttwo};
\addlegendentry{3Q19};
\addplot [colourone, fill=colourone,
nodes near coords]                        % <-------------------
table [y=4Q19]  {\charttwo};
\addlegendentry{4Q19};
\end{axis}
\end{tikzpicture}

\end{document}

enter image description here

2
  • Thank you Tim! What's the difference between xlabel and xtick label?
    – Thev
    Mar 12, 2020 at 8:46
  • 1
    xlabel is the text that describes the axis (e.g. "speed in mph"), whereas xtick labels refers to the text on the ticks (like "1.9, 1.3, -2.5" in your plot)
    – Tim Hilt
    Mar 12, 2020 at 8:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .