无法将条形值添加到条形图中,仅显示 xtick 标签

无法将条形值添加到条形图中,仅显示 xtick 标签
\documentclass{article}
\usepackage{pgfplots, pgfplotstable}
\pgfplotsset{compat=1.17}
\usetikzlibrary{patterns}

\begin{document}
    \begin{figure}
    \caption{Test Caption}
    \centering
\begin{tikzpicture}
\pgfplotsset{
    width=8cm,  height=6cm,
    enlarge x limits=0.25,              % <---
    xtick style = {draw=none},          % <---
    x tick label style={yshift=-2em},   % <---
    ytick={0,20,...,100},    
% coordinates
    xtick distance=1,
    symbolic x coords={KAR-MMK, MMK-MCK, MCK-TJB},
    ymin=0, ymax=100,                   
    ybar stacked,
    axis on top,                        % <---
% nodes 
    nodes near coords,
    every node near coord/.style={font=\footnotesize,
                                  below, rotate=40, anchor=east}, % <---
     point meta=explicit symbolic,       % <===
    stacked ignores zero=false        
    % <===
            }
%%%% diagram body
\begin{axis}[bar width=8pt, 
             bar shift=-15pt]
\addplot [xshift=-15pt] coordinates
    {(KAR-MMK,0) [Road]
     (MMK-MCK,0) [Road]
     (MCK-TJB,0) [Road]
    };
\addplot [fill=none,mark=Black,postaction={pattern=north east lines}] 
coordinates {(KAR-MMK,52) (MMK-MCK,28) (MCK-TJB,28)};
% \addplot[fill=none,mark=Black,postaction={pattern=horizontal lines}]
coordinates {(KAR-MMK,0) (MMK-MCK,0) (MCK-TJB,0)};
\addplot [fill=none,mark=Black,postaction={pattern=dots}]
coordinates {(KAR-MMK,30) (MMK-MCK,53) (MCK-TJB,43)};
\end{axis}

\begin{axis}[ytick=\empty, xtick=\empty,
             bar width=8pt,
             bar shift=-5pt]
\addplot  [xshift=-5pt] coordinates
    {(KAR-MMK,0) [Railway]
     (MMK-MCK,0) [Railway]
     (MCK-TJB,0) [Railway]
    };
\addplot [fill=none,mark=Black,postaction={pattern=north east lines}]   coordinates {(KAR-MMK,52) (MMK-MCK,28) (MCK-TJB,38)};
% \addplot [fill=none,mark=Black,postaction={pattern=horizontal lines}] coordinates {(KAR-MMK,0) (MMK-MCK,00) (MCK-TJB,0)};
\addplot[fill=none,mark=Black,postaction={pattern=dots}] coordinates {(KAR-MMK,30) (MMK-MCK,53) (MCK-TJB,43)};
\end{axis}

\begin{axis}[ytick=\empty, xtick=\empty,
             bar width=8pt,
             bar shift= 5pt]
\addplot [xshift=5pt] coordinates
    {(KAR-MMK,0) [Pipeline]
     (MMK-MCK,0) [Pipeline]
     (MCK-TJB,0) [Pipeline]
    };
\addplot [fill=none,mark=Black,postaction={pattern=north east lines}]  
coordinates {(KAR-MMK,42) (MMK-MCK,25) (MCK-TJB,33)};
% \addplot [fill=none,mark=Black,postaction={pattern=horizontal lines}]   
coordinates {(KAR-MMK,0) (MMK-MCK,0) (MCK-TJB,0)};
\addplot [fill=none,mark=Black,postaction={pattern=dots}]
coordinates {(KAR-MMK,39) (MMK-MCK,48) (MCK-TJB,47)};
   \legend{,HSD,MS}
\end{axis}


\end{tikzpicture}
    \end{figure}
\end{document}

答案1

主要问题是你使用的是point meta=explicit symbolic,这意味着放置在中的文本nodes near coords是坐标流中指定的文本,meta

(x,y) [meta]

默认使用 y 值,因此您explicit symbolic只需要用于制作刻度标签的图表。

此外,您every node near coord样式中的旋转等是为了制作刻度标签,这可能并不适合在条形图中放置节点。

在下面的代码中,我定义了一种新样式nnc_ticklabels,并在选项中将其用于定义刻度标签的三个\addplots。我还修改了 的默认样式nodes near coords

在此处输入图片描述

\documentclass{article}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.17}
\usetikzlibrary{patterns}

\begin{document}
\begin{figure}
\centering
\caption{Test Caption}
\begin{tikzpicture}
\pgfplotsset{
    width=8cm,  height=6cm,
    enlarge x limits=0.25,              % <---
    xtick style = {draw=none},          % <---
    x tick label style={yshift=-2.2em},   % <---
    ytick={0,20,...,100},    
% coordinates
    xtick distance=1,
    symbolic x coords={KAR-MMK, MMK-MCK, MCK-TJB},
    ymin=0, ymax=100,                   
    ybar stacked,
    axis on top,                        % <---
% nodes 
    nodes near coords,
    every node near coord/.append style={font=\footnotesize}, % <--- modified
    stacked ignores zero=false,
    nnc_ticklabels/.style={ % <-- new style
       point meta=explicit symbolic,
       every node near coord/.style={
          font=\footnotesize, below,
          rotate=40,
          anchor=north east, % <- changed from east
          inner sep=1pt % <- added
          }
       }
    % <===
            }
%%%% diagram body
\begin{axis}[bar width=8pt, 
             bar shift=-15pt]
\addplot [xshift=-15pt, nnc_ticklabels] coordinates % <-- added the new style for all the "ticklabel" plots
    {(KAR-MMK,0) [Road]
     (MMK-MCK,0) [Road]
     (MCK-TJB,0) [Road]
    };

\addplot [fill=none,mark=Black,postaction={pattern=north east lines}] 
coordinates {(KAR-MMK,52) (MMK-MCK,28) (MCK-TJB,28)};
\addplot [fill=none,mark=Black,postaction={pattern=dots}]
coordinates {(KAR-MMK,30) (MMK-MCK,53) (MCK-TJB,43)};
\end{axis}

\begin{axis}[ytick=\empty, xtick=\empty,
             bar width=8pt,
             bar shift=-5pt]
\addplot  [xshift=-5pt, nnc_ticklabels] coordinates
    {(KAR-MMK,0) [Railway]
     (MMK-MCK,0) [Railway]
     (MCK-TJB,0) [Railway]
    };
\addplot [fill=none,mark=Black,postaction={pattern=north east lines}]   coordinates {(KAR-MMK,52) (MMK-MCK,28) (MCK-TJB,38)};
\addplot[fill=none,mark=Black,postaction={pattern=dots}] coordinates {(KAR-MMK,30) (MMK-MCK,53) (MCK-TJB,43)};
\end{axis}

\begin{axis}[ytick=\empty, xtick=\empty,
             bar width=8pt,
             bar shift=5pt]
\addplot [xshift=5pt, nnc_ticklabels] coordinates
    {(KAR-MMK,0) [Pipeline]
     (MMK-MCK,0) [Pipeline]
     (MCK-TJB,0) [Pipeline]
    };
\addplot [fill=none,mark=Black,postaction={pattern=north east lines}]  
coordinates {(KAR-MMK,42) (MMK-MCK,25) (MCK-TJB,33)};
\addplot [fill=none,mark=Black,postaction={pattern=dots}]
coordinates {(KAR-MMK,39) (MMK-MCK,48) (MCK-TJB,47)};
   \legend{,HSD,MS}
\end{axis}


\end{tikzpicture}
\end{figure}
\end{document}

相关内容