我有以下 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}
结果是:
大多数调整我都能够找到方法。但有 3 个调整让我很纠结:
- 如何删除 0 以下的 y 轴空间?尽管我声明了,但它仍然从 -0.5 开始
ymin = 0.0
。 - 如何删除红色条上的所有数据标签?我只想调出 2019 年第三季度系列的数据(黑色条)。
答案1
- 这
enlargelimits=0.15
是在数据下方和数据左侧添加额外空间。通过设置enlargelimits=false
然后enlarge x limits=0.15
您可以修复轴问题。 - 正在
nodes near coords
为两个数据系列创建标签。在下面的解决方案中,我已将其从 的选项中删除axis
,并将其添加到 的选项中\addplot
。
最终的代码如下:
\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}
对我来说,编译如下。请注意,这看起来不太像您的屏幕截图,我怀疑是因为您的屏幕截图不是 MWE 的屏幕截图,而是来自原始文件的屏幕截图,其中包含一些其他设置。
答案2
编辑:
@rbrgnall 答案(+1)的一个小变化,使用表格(代码中用 标记了差异% <---%
):
\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}