我想在图表中添加一个条形图,但不添加任何值。不幸的是,当我跳过在坐标中添加带有标签 x 的正确值时,它(带有标签 x 的条形图)会从图表中消失。
\begin{tikzpicture}
\begin{axis}[
width=\textwidth,
ybar,
enlargelimits=0.10,
legend style={at={(0.5,-0.2)},
anchor=north,legend columns=-1},
symbolic x coords={A,B,C},
xtick=data,
nodes near coords,
nodes near coords align={vertical},
x tick label style={rotate=45,anchor=east},
]
\addplot coordinates {(B,5) (C,7)};
\end{axis}
\end{tikzpicture}
在此示例中,条形图 A(和标签 A)不会出现在图表上。有人知道如何将此 x 标签放到图表上吗?
答案1
以下是使用的建议
symbolic x coords={A,B,C},
xtick={A,B,C},
xmin=A,
ymin=0,
enlarge y limits={0.1,upper},
代码:
\documentclass[margin=5mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
width=\textwidth,
ybar,
enlargelimits=0.10,
legend style={at={(0.5,-0.2)},
anchor=north,legend columns=-1},
symbolic x coords={A,B,C},
xtick={A,B,C},
xmin=A,
ymin=0,
enlarge y limits={0.1,upper},
nodes near coords,
nodes near coords align={vertical},
x tick label style={rotate=45,anchor=east},
]
\addplot coordinates {(B,5) (C,7)};
\end{axis}
\end{tikzpicture}
\end{document}
答案2
另一个测试 bar 值并决定是否将节点放在 coord 附近。以防你因为某些不为人知的原因而需要它。测试应该更简单,但不知何故ifthenelse
在这种情况下没有超载。输出与上面的相同
\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.10}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
width=\textwidth,
ybar,ymin=0,enlarge y limits=false,
enlarge x limits=0.10,
legend style={at={(0.5,-0.2)},
anchor=north,legend columns=-1},
symbolic x coords={A,B,C},
xtick=data,
nodes near coords*={%
\pgfmathprintnumberto[fixed,assume math mode]{\pgfplotspointmeta}{\temp}%
\ifnum\temp=0\else\pgfmathprintnumber{\pgfplotspointmeta}\fi%
},
nodes near coords align={vertical},
x tick label style={rotate=45,anchor=east},
]
\addplot coordinates {(A,0) (B,5) (C,7)};
\end{axis}
\end{tikzpicture}
\end{document}