我目前正在制作以下图表:
使用此代码:
\documentclass{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usepackage{pdfpages}
\pgfplotsset{compat=1.8}
\usepackage{color} % colors
\usepackage{xcolor} % more control over colors
\newcommand{\titlecolour}{blue!70!white}
% chart 1 data
\pgfplotstableread[col sep=comma]% % <---
{
country, 3Q19, 4Q19
US, 2.0, 1.9
Euro, 1.2, 1.1
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
}\chartone
\pgfplotsset{compat=1.11,
/pgfplots/ybar legend/.style={
/pgfplots/legend image code/.code={%
\draw[ ##1,/tikz/.cd,yshift=-0.25em]
(0cm,0cm) rectangle (0.6em,0.6em);},
},
}
\begin{document}
\begin{tikzpicture}
\small
\begin{axis}[
x=9mm,
ybar,
x = 0.85cm,
bar width = 2mm,
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=\scriptsize},
ymin = 0,
xtick=data,
x tick label style={rotate = 90},
symbolic x coords={US, Euro, PR China, Singapore,
Indonesia, Malaysia, Korea, Taiwan, Philippines},
%
% legends and labels
legend style={draw=none, legend columns=-1, at={(0.8, 1)},anchor=north east, %
/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, color = black},
legend style={legend columns=-1},
ylabel={Annual change (\%)},
]
\addplot[\titlecolour, fill = \titlecolour]
table [y=3Q19] \chartone; % <---
\addplot[yellow!80!black, fill = yellow!80!black, nodes near coords]
table [y=4Q19] \chartone; % <---
\legend{~3Q19, ~4Q19}
\end{axis}
\end{tikzpicture}
\end{document}
我想做两点调整:
- 稍微抬起一点
nodes near coords
,这样它们就不会太靠近杆了。 - 直接从表中设置图例标签(3Q19 和 4Q19)以及 x 轴标签的值。目前,它们是使用和命令
chartone
手动设置的,这并不理想,因为它为人为错误留下了空间。\legend
symbolic x coords
非常感谢任何帮助!
答案1
3 个点中的 2 个点可以相当轻松地完成。也就是说,将 添加到yshift=...
以nodes near coords style
移动节点,并将 替换symbolic coords
为xticklabels from table
和table/x expr=\coordindex
。
你能从表头获取图例条目,但这只有在您不对每个\addplot
命令执行特殊操作时才有意义,因此您可以循环遍历表列(使用\pgfplotsinvokeforeach
或类似方法)。作为示例,请查看我给出的答案这里。
% used PGFPlots v1.16
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.11,
/pgfplots/ybar legend/.style={
/pgfplots/legend image code/.code={%
\draw[ ##1,/tikz/.cd,yshift=-0.25em]
(0cm,0cm) rectangle (0.6em,0.6em);},
},
}
\colorlet{titlecolour}{blue!70!white} % <-- (adjusted)
\pgfplotstableread[col sep=comma]{
country, 3Q19, 4Q19
US, 2.0, 1.9
Euro, 1.2, 1.1
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
}\chartone
\begin{document}
\begin{tikzpicture}
\small
\begin{axis}[
x=9mm,
ybar,
x=0.85cm,
bar width=2mm,
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=\scriptsize,
},
ymin=0,
xtick=data,
xticklabels from table={\chartone}{country}, % <-- added
table/x expr=\coordindex, % <-- added
x tick label style={rotate=90},
%
% legends and labels
legend style={
draw=none,
legend columns=-1,
at={(0.8, 1)},
anchor=north east,
/tikz/every even column/.append style={
column sep=1em,
},
},
y label style={
at={(0.15,1)},
rotate=-90,
anchor=south,
},
nodes near coords style={
/pgf/number format/.cd,
fixed,
fixed zerofill,
precision=1,
/tikz/.cd,
font=\scriptsize,
color=black,
yshift=1ex, % <-- added
},
ylabel={Annual change (\%)},
]
\addplot [
titlecolour,
fill=titlecolour,
] table [y=3Q19] {\chartone};
\addlegendentry{3Q19};
\addplot [
yellow!80!black,
fill=yellow!80!black,
nodes near coords,
] table [y=4Q19] {\chartone};
\addlegendentry{4Q19};
\end{axis}
\end{tikzpicture}
\end{document}
答案2
渴望评论...
一些题外话(因为你的主要问题已经被 @Stefan Pinnow 解决了 回答: +1)我想知道,为什么你在 MWE 中:
- 定义版本使用了
pgfplots
两次,第一次compat=1.8
,比compat=1.11
(在序言中的图例样式定义中) - 定义两次图例样式,一次在序言中,一次在轴的选项中
- 为什么 y 轴标签放在图表顶部。图表标题通常放在那里,为了清晰起见,人们把轴标签放在轴上
- 另外,我认为将 y 刻度写为 2,0, 4.0, ... 没有任何优势,在我看来,2, 4, ... 就足够了
考虑到上述情况,MWE 可以是:
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.16}
\colorlet{titlecolour}{blue!70!white}
\pgfplotstableread[col sep=comma]{
country, 3Q19, 4Q19
US, 2.0, 1.9
Euro, 1.2, 1.1
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
}\chartone
\begin{document}
\begin{tikzpicture}
\begin{axis}[x=12mm,
ybar,
bar width=4.4mm,
axis lines=left,
enlarge x limits=0.1,
enlarge y limits={.2, upper},
% y ticks style and label
ylabel={Annual change (\%)},
ymin=0,
% x axis ticks and style
xtick=data,
xticklabels from table={\chartone}{country}, % <-------------------
table/x expr = \coordindex, % <-------------------
x tick label style = {rotate=90},
% legends and labels
legend style = {draw=none,
legend columns=-1,
at={(0.5,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.5ex,
},
]
\addplot [fill=titlecolour]
table [y=3Q19] {\chartone};
\addlegendentry{3Q19};
\addplot [fill=yellow!80!black,
nodes near coords] % <-------------------
table [y=4Q19] {\chartone};
\addlegendentry{4Q19};
\end{axis}
\end{tikzpicture}
\end{document}