条形图值重叠,切片非常小以显示这些值

条形图值重叠,切片非常小以显示这些值

在此处输入图片描述

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

\begin{document}
\caption{Existing Scenario:
Mode Wise Percentage Share}
\centering
\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,...,200},    
    ylabel=label,
% coordinates
    xtick distance=1,
    symbolic x coords={KAR-MMK, MMK-MCK, MCK-TJB}, yticklabel=\pgfmathprintnumber{\tick}\,$\%$,
    ymin=0, ymax=200,  
    ybar stacked,
    axis on top,                        % <---
% nodes 
    nodes near coords,
    every node near coord/.append style={font=\footnotesize,rotate=90}, % <--- 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}[ylabel=Percentage Share,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,69) (MMK-MCK,28) (MCK-TJB,28)};
\addplot [fill=none,mark=Black,postaction={pattern=dots}]
coordinates {(KAR-MMK,98) (MMK-MCK,98) (MCK-TJB,43)};
\end{axis}

\begin{axis}[ylabel=Percentage Share,
             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,052) (MMK-MCK,28) (MCK-TJB,02)};
\addplot[fill=none,mark=Black,postaction={pattern=dots}] coordinates {(KAR-MMK,30) (MMK-MCK,2) (MCK-TJB,02)};
\end{axis}

\begin{axis}[ylabel=Percentage Share,
             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)};
\legend {,HSD,MS},
\addplot [fill=none,mark=Black,postaction={pattern=dots}]
coordinates {(KAR-MMK,39) (MMK-MCK,48) (MCK-TJB,47)};
   
        ]
\end{axis}


\end{tikzpicture}
\end{document}

答案1

要将百分号添加到标签,您可以使用nodes near coords={\pgfmathprintnumber\pgfplotspointmeta\,\%}。请注意,如果您将其添加到轴,它还将应用于您添加的图的标签,以添加“道路”、“铁路”和“管道”的标签。因此,您需要将这些标签的选项重置为默认的nodes near coords={\pgfmathprintnumber\pgfplotspointmeta}

为了对齐框尺寸太小的标签,您可以使用选项coordinate style/.condition并使用条件表达式仅选择相关标签,然后可以为其指定特定样式。不过,我不确定第三组中第二个栏的解决方案是否最佳。

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usetikzlibrary{patterns}

\begin{document}
\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,...,200},    
    ylabel=label,
% coordinates
    xtick distance=1,
    symbolic x coords={KAR-MMK, MMK-MCK, MCK-TJB}, yticklabel=\pgfmathprintnumber{\tick}\,$\%$,
    ymin=0, 
    ymax=200,  
    ybar stacked,
    axis on top,   
% nodes 
    nodes near coords={\pgfmathprintnumber\pgfplotspointmeta\,\%},
    every node near coord/.append style={
        font=\scriptsize,
        rotate=90
    },
    stacked ignores zero=false,
    nnc_ticklabels/.style={
        point meta=explicit symbolic,
        nodes near coords={\pgfmathprintnumber\pgfplotspointmeta},
        every node near coord/.style={
            font=\scriptsize, 
            below,
            rotate=40,
            anchor=north east, 
            inner sep=1pt,
        }
    },
    coordinate style/.condition={\coordindex == 1 && y > 28 && y <= 30}{
        right
    },
    coordinate style/.condition={\coordindex == 2 && y > 0 && y <= 2}{
        right
    },
    coordinate style/.condition={\coordindex == 2 && y > 2 && y <= 4}{
        right, yshift=.5cm
    }
}
%%%% diagram body
\begin{axis}[ylabel=Percentage Share,
    bar width=8pt, 
    bar shift=-15pt]
\addplot [xshift=-15pt, nnc_ticklabels] coordinates 
    {(KAR-MMK,0) [Road]
     (MMK-MCK,0) [Road]
     (MCK-TJB,0) [Road]
    };
\addplot [fill=none, mark=Black, postaction={pattern=north east lines, pattern color=lightgray}] 
coordinates {(KAR-MMK,69) (MMK-MCK,28) (MCK-TJB,28)};
\addplot [fill=none, mark=Black, postaction={pattern=dots}]
coordinates {(KAR-MMK,98) (MMK-MCK,98) (MCK-TJB,43)};
\end{axis}

\begin{axis}[ylabel=Percentage Share,
             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, pattern color=lightgray}] coordinates {(KAR-MMK,52) (MMK-MCK,28) (MCK-TJB,2)};
\addplot [fill=none, mark=Black, postaction={pattern=dots}] coordinates {(KAR-MMK,30) (MMK-MCK,2) (MCK-TJB,2)};
\end{axis}

\begin{axis}[ylabel=Percentage Share,
             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, pattern color=lightgray}]  
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{document}

在此处输入图片描述

相关内容