水平条形图:使用多个 \addplots 时,条形之间的距离会变得混乱

水平条形图:使用多个 \addplots 时,条形之间的距离会变得混乱

我遇到了一个我不明白的问题。我试图使用绘制一个非常简单的条形图pgfplots。当用相同的颜色绘制所有条形图时,它确实有效:

\documentclass[border={0.2cm, 0.2cm, 0.2cm, 0.2cm}]{standalone}
\usepackage[T1]{fontenc}
\usepackage{fontawesome5}
%\usepackage{figure}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{xcolor}

\begin{document}
\pgfplotsset{compat=newest}
\pgfplotsset{frequency_bar/.style={
    xbar,
    width=.95\textwidth,
    %height=2cm,
    axis y line*= none, axis x line*= bottom,
    xmajorgrids = true,
    xmin=0,xmax=100,
    ytick = {0,1,2,3,4}, 
    yticklabels = {Every year,Every half year,Every month,Every week, Every day},
    yticklabel style={font=\footnotesize, text width=1.2cm, align=center},
    tick align = outside, xtick pos = left,
    bar width=5mm, y=10mm,
    xlabel = {Percentage of operators (\%)},
    xlabel style = {font=\small},
    enlarge y limits={abs=0.5},    
    nodes near coords=\pgfmathprintnumber{\pgfplotspointmeta}\%,
    every node near coord/.append style={font=\scriptsize, color=black},
}}

\begin{tikzpicture}
    \begin{axis}[frequency_bar] 
        % 1 gbps
        \addplot[black!80, fill=blue!30!white, mark=none] coordinates {(15,4) (17,3) (46,2) (78,1) (95,0)};
    \end{axis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

但是,当我尝试给每个条形图上色,并使用多个条形图进行拆分时,\addplot条形图变得混乱不堪,我不确定如何修复它。

\documentclass[border={0.2cm, 0.2cm, 0.2cm, 0.2cm}]{standalone}
\usepackage[T1]{fontenc}
\usepackage{fontawesome5}
%\usepackage{figure}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{xcolor}

\begin{document}
\pgfplotsset{compat=newest}
\pgfplotsset{frequency_bar/.style={
    xbar,
    width=.95\textwidth,
    %height=2cm,
    axis y line*= none, axis x line*= bottom,
    xmajorgrids = true,
    xmin=0,xmax=100,
    ytick = {0,1,2,3,4}, 
    yticklabels = {Every year,Every half year,Every month,Every week, Every day},
    yticklabel style={font=\footnotesize, text width=1.2cm, align=center},
    tick align = outside, xtick pos = left,
    bar width=5mm, y=10mm,
    xlabel = {Percentage of operators (\%)},
    xlabel style = {font=\small},
    enlarge y limits={abs=0.5},    
    nodes near coords=\pgfmathprintnumber{\pgfplotspointmeta}\%,
    every node near coord/.append style={font=\scriptsize, color=black},
}}

\begin{tikzpicture}
    \begin{axis}[frequency_bar] 
        \addplot[black!80, fill=blue!30!white, mark=none] coordinates {(95,0)};
        \addplot[black!80, fill=red!30!white, mark=none] coordinates {(78,1)};
        \addplot[black!80, fill=green!30!white, mark=none] coordinates {(46,2)};
        \addplot[black!80, fill=yellow!30!white, mark=none] coordinates {(17,3)};
        \addplot[black!80, fill=purple!30!white, mark=none] coordinates {(15,4)};
    \end{axis}
\end{tikzpicture}

\end{document}

在此处输入图片描述

我尝试调整高度,enlarge y limits但没能修复它。有人有想法吗?

相关内容