在图表栏中打印坐标附近的节点时显示 0

在图表栏中打印坐标附近的节点时显示 0

问题描述:

我希望所有值(包括 0)都显示在图表栏上方。由于我是 Latex 新手,我假设在坐标附近的节点中设置了某些内容,导致无法显示 0。使用我当前的 MWE,我得到了以下输出: 在此处输入图片描述

梅威瑟:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{pgfplots}
\pgfplotsset{width=10cm,compat=newest}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
 
\usetikzlibrary{patterns}


\begin{document}
\pgfplotstableread[col sep=comma,header=false]{
1,0,19,0,13
2,0,12,2,9
3,10,0,8,2
4,22,1,15,1
}\data

\pgfplotsset{
    percentage plot/.style={
        point meta=explicit,
    every node near coord/.append style={
        align=left,
        font=\footnotesize,
    }, 
    nodes near coords={
        \pgfmathtruncatemacro\iszero{\originalvalue==0}
         \ifnum\iszero=0
         \pgfmathprintnumber[fixed zerofill,precision=0]
        {\originalvalue}\\
        \pgfmathprintnumber{\pgfplotspointmeta}$\%$
        \fi}, 
    nodes near coords style={font=\tiny},     
    nodes near coords align=vertical,
    ymin=0,
    ymax=23,
    enlarge y limits={upper,value=0.1}, 
    visualization depends on={y \as \originalvalue}
    },
}

\begin{tikzpicture}
\begin{axis}[
    axis on top,
    width=14cm,
    ybar,
    percentage plot,bar width=12pt,
    enlarge x limits=0.25,
    symbolic x coords={1,2,3,4},
    xtick=data,
    legend style={
            at={(0.5,-0.2)},
        anchor=north,
            column sep=1ex}, 
]
\addplot [style={teal,fill=teal}] coordinates {(1,0)[0] (2,0)[0] (3,10)[31] (4,22)[69]};
\addplot [style={blue,fill=blue}] coordinates {(1,19)[59] (2,12)[38] (3,0)[0] (4,1)[3]};
\addplot [style={violet,fill=violet}] coordinates {(1,0)[0] (2,2)[8] (3,8)[32] (4,15)[60]};
\addplot [style={cyan,fill=cyan}] coordinates {(1,13)[52] (2,9)[36] (3,2)[8] (4,1)[4]};

\end{axis}
\end{tikzpicture}


\end{document}

期望结果 我希望在没有显示条形的地方写上 0 和 0%。

答案1

像这样?

在此处输入图片描述

这个想法就是删除if测试和fi测试结束。

\documentclass[border=2pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{width=10cm,compat=newest}

\begin{document}
\pgfplotstableread[col sep=comma,header=false]{
1,0,19,0,13
2,0,12,2,9
3,10,0,8,2
4,22,1,15,1
}\data

\pgfplotsset{
    percentage plot/.style={
        point meta=explicit,
    every node near coord/.append style={
        align=left,
        font=\footnotesize,
    }, 
    nodes near coords={
%         \ifnum\iszero=0  % <-- changed here
         \pgfmathprintnumber[fixed zerofill,precision=0]
        {\originalvalue}\\
        \pgfmathprintnumber{\pgfplotspointmeta}$\%$
%        \fi  % <-- changed here
}, 
    nodes near coords style={font=\tiny},     
    nodes near coords align=vertical,
    ymin=0,
    ymax=23,
    enlarge y limits={upper,value=0.1}, 
    visualization depends on={y \as \originalvalue}
    },
}

\begin{tikzpicture}
\begin{axis}[
    axis on top,
    width=14cm,
    ybar,
    percentage plot,bar width=12pt,
    enlarge x limits=0.25,
    symbolic x coords={1,2,3,4},
    xtick=data,
    legend style={
            at={(0.5,-0.2)},
        anchor=north,
            column sep=1ex}, 
]
\addplot [style={teal,fill=teal}] coordinates {(1,0)[0] (2,0)[0] (3,10)[31] (4,22)[69]};
\addplot [style={blue,fill=blue}] coordinates {(1,19)[59] (2,12)[38] (3,0)[0] (4,1)[3]};
\addplot [style={violet,fill=violet}] coordinates {(1,0)[0] (2,2)[8] (3,8)[32] (4,15)[60]};
\addplot [style={cyan,fill=cyan}] coordinates {(1,13)[52] (2,9)[36] (3,2)[8] (4,1)[4]};

\end{axis}
\end{tikzpicture}
\end{document}

相关内容