我制作了以下图表:
如果您查看表格\hchartone
,您会发现我创建了两个单独的系列(yoy
和yoy2
),以便能够用不同的颜色绘制最后一条柱状图。
然而,这导致 1Q20 的 x-tick 标签缺失。
我该如何解决?
生成上述内容的代码:
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.17}
%colors
\usepackage{color} % colors
\usepackage{xcolor}
\definecolor{c1}{HTML}{122084}
\definecolor{c6}{HTML}{ff8091}
\definecolor{c6d}{HTML}{db4d60}
\begin{document}
%\pagecolor{gray!20!white}
\pgfplotstableread[col sep=comma]{
date, yoy, yoy2
1Q19, 4.5,
2Q19, 4.9,
3Q19, 4.4,
4Q19, 3.6,
1Q20, , -0.3
}\hchartone
\begin{tikzpicture}
\small
\begin{axis}[
width = 6.5cm,
height = 6cm,
axis lines=left,
enlarge x limits={0.1},
enlarge y limits={0.1, upper},
%
% y ticks style and label
ymin = -1.1,
ylabel={Annual change (\%)},
ylabel shift = 0pt,
ytick distance = 1,
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={\hchartone}{date},
axis x line shift={\pgfkeysvalueof{/pgfplots/ymin}},
xticklabel shift={-\pgfkeysvalueof{/pgfplots/axis x line shift}},
table/x expr = \coordindex,
%
% nodes near coords
nodes near coords,
nodes near coords style = { /pgf/number format/.cd,
fixed, fixed zerofill, precision=1, /tikz/.cd, font=\scriptsize,
},
]
%
% done with the axis, now the plots
\addplot [c1, fill, ybar, nodes near coords, draw opacity = 0]
table [y=yoy] {\hchartone};
\addplot [c6, fill, ybar, nodes near coords style = {color = c6d}, draw opacity = 0]
table [y=yoy2] {\hchartone};
\end{axis}
\end{tikzpicture}
\end{document}
答案1
这是对情节处理程序的一个非常简单的破解,它使用未记录的键at end bar
,并允许您一次性为条形图着色。我不能保证这总是安全的,但至少在这里它似乎工作正常。
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.17}
%colors
\usepackage{color} % colors
\usepackage{xcolor}
\definecolor{c1}{HTML}{122084}
\definecolor{c6}{HTML}{ff8091}
\definecolor{c6d}{HTML}{db4d60}
\begin{document}
%\pagecolor{gray!20!white}
\pgfplotstableread[col sep=comma]{
date, yoy
1Q19, 4.5
2Q19, 4.9
3Q19, 4.4
4Q19, 3.6
1Q20, -0.3
}\hchartone
\begin{tikzpicture}
\small
\begin{axis}[
width = 6.5cm,
height = 6cm,
axis lines=left,
enlarge x limits={0.1},
enlarge y limits={0.1, upper},
%
% y ticks style and label
ymin = -1.1,
ylabel={Annual change (\%)},
ylabel shift = 0pt,
ytick distance = 1,
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={\hchartone}{date},
axis x line shift={\pgfkeysvalueof{/pgfplots/ymin}},
xticklabel shift={-\pgfkeysvalueof{/pgfplots/axis x line shift}},
table/x expr = \coordindex,
%
% nodes near coords
nodes near coords,
nodes near coords style = { /pgf/number format/.cd,
fixed, fixed zerofill, precision=1, /tikz/.cd, font=\scriptsize,
},
]
%
% done with the axis, now the plots
%
% hack the plot handler
\pgfkeys{/pgf/at end bar={\ifnum\coordindex=4
\pgfsetfillcolor{c6}
\else
\pgfsetfillcolor{c1}
\fi
\pgfusepath{stroke, fill}
}}
\addplot [c1, fill, ybar, nodes near coords, draw opacity = 0]
table [y=yoy] {\hchartone};
\end{axis}
\end{tikzpicture}
\end{document}
有很多方法可以概括这一点。例如,这个方法将所有负值的颜色与正值的颜色不同。
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.17}
%colors
\usepackage{color} % colors
\usepackage{xcolor}
\definecolor{c1}{HTML}{122084}
\definecolor{c6}{HTML}{ff8091}
\definecolor{c6d}{HTML}{db4d60}
\begin{document}
%\pagecolor{gray!20!white}
\pgfplotstableread[col sep=comma]{
date, yoy
1Q19, 4.5
2Q19, 4.9
3Q19, 4.4
4Q19, 3.6
1Q20, -0.3
}\hchartone
\begin{tikzpicture}
\small
\begin{axis}[
width = 6.5cm,
height = 6cm,
axis lines=left,
enlarge x limits={0.1},
enlarge y limits={0.1, upper},
%
% y ticks style and label
ymin = -1.1,
ylabel={Annual change (\%)},
ylabel shift = 0pt,
ytick distance = 1,
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={\hchartone}{date},
axis x line shift={\pgfkeysvalueof{/pgfplots/ymin}},
xticklabel shift={-\pgfkeysvalueof{/pgfplots/axis x line shift}},
table/x expr = \coordindex,
%
% nodes near coords
visualization depends on={int(sign(\thisrow{yoy}))\as\mysign},
nodes near coords,
nodes near coords style = {/utils/exec={\ifdim\mysign pt>0pt
\tikzset{text=c1}
\else
\tikzset{text=c6}
\fi},
/pgf/number format/.cd,
fixed, fixed zerofill, precision=1, /tikz/.cd, font=\scriptsize,
},
% nodes near coords style={anchor=\myanchor}
]
%
% done with the axis, now the plots
%
% hack the plot handler
\makeatletter
\pgfkeys{/pgf/at end bar={\ifdim\the\pgf@ya<0pt
\pgfsetfillcolor{c6}
\else
\pgfsetfillcolor{c1}
\fi
\pgfusepath{stroke, fill}
}}
\makeatother
\addplot [c1, fill, ybar, nodes near coords, draw opacity = 0]
table [y=yoy] {\hchartone};
\end{axis}
\end{tikzpicture}
\end{document}
答案2
一个非常 hacky 的解决方案是这样的:
- 在 1Q20 的列中添加一个 0 条目
yoy
- 这会导致显示缺失的标签,但现在会产生一个问题- 在1Q20 的nodes near coords
第一个系列中有一个多余的标签。0.0
- 为了修复上述问题,请用白色矩形手动覆盖它。(哈哈)
对于小数据系列来说,这没问题,但对于大数据系列来说,这站不住脚。不过,我可以得到这个:
使用此代码:
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.17}
%colors
\usepackage{color} % colors
\usepackage{xcolor}
\definecolor{c1}{HTML}{122084}
\definecolor{c6}{HTML}{ff8091}
\definecolor{c6d}{HTML}{db4d60}
\begin{document}
%\pagecolor{gray!20!white}
\pgfplotstableread[col sep=comma]{
date, yoy, yoy2
1Q19, 4.5,
2Q19, 4.9,
3Q19, 4.4,
4Q19, 3.6,
1Q20, 0, -0.3
}\hchartone
\begin{tikzpicture}
\small
\begin{axis}[
width = 6.5cm,
height = 6cm,
axis lines=left,
enlarge x limits={0.1},
enlarge y limits={0.1, upper},
%
% y ticks style and label
ymin = -1.1,
ylabel={Annual change (\%)},
ylabel shift = 0pt,
ytick distance = 1,
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={\hchartone}{date},
xtick distance = 1,
axis x line shift={\pgfkeysvalueof{/pgfplots/ymin}},
xticklabel shift={-\pgfkeysvalueof{/pgfplots/axis x line shift}},
table/x expr = \coordindex,
%
% nodes near coords
nodes near coords,
nodes near coords style = { /pgf/number format/.cd,
fixed, fixed zerofill, precision=1, /tikz/.cd, font=\scriptsize,
},
]
%
% done with the axis, now the plots
\addplot [c1, fill, ybar, nodes near coords, draw opacity = 0]
table [y=yoy] {\hchartone};
\addplot [c6, fill, ybar, nodes near coords style = {color = c6d}, draw opacity = 0]
table [y=yoy2] {\hchartone};
\end{axis}
\draw[white, fill] (4,0.8) rectangle (4.8,1.02);
\end{tikzpicture}
\end{document}