条形图绘制:线条混淆和重叠

条形图绘制:线条混淆和重叠

我正在尝试遵循这个例子](https://tex.stackexchange.com/a/157028/122628) 来绘制基本的条形图,但线条却混杂在一起。我认为这是因为两列之间存在很大差异。

在此处输入图片描述

\documentclass[border=0.2cm]{standalone}
 
\usepackage{pgfplots} 
\usetikzlibrary{patterns}
 
\begin{document}
 
 \pgfplotstableread[row sep=\\,col sep=&]{
    interval & dynamic & static \\
0  & 0  & 0  \\
1  & 0  & 0  \\
2  & 0  & 0  \\
3  & 0  & 0  \\
4  & 0  & 0  \\
5  & 0  & 0  \\
6  & 0  & 0  \\
7  & 0  & 0  \\
8  & 0  & 0  \\
9  & 0  & 0  \\
10  & 0  & 0  \\
11  & 0  & 0  \\
12  & 0  & 0  \\
13  & 1  & 0  \\
14  & 46  & 6  \\
15  & 66  & 9  \\
16  & 157  & 18  \\
17  & 649  & 79  \\
    }\mydata
 
\begin{tikzpicture}
    \begin{axis}[
            ybar,
            legend style={at={(0.5,1)},
                anchor=north,legend columns=-1},
            symbolic x coords={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17}
            xtick=data,
            nodes near coords,
            nodes near coords align={vertical},
            ylabel={\%},
        ]
        \addplot table[x=interval,y=dynamic]{\mydata};
        \addplot table[x=interval,y=static]{\mydata};
        \legend{Dynamic,Static}
    \end{axis}
\end{tikzpicture}
 
\end{document}

答案1

只需对代码进行最少的更改(参见代码中相应的注释)

\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
        \pgfplotstableread[row sep=\\,col sep=&]{
            interval & dynamic & static \\
            0  & 0  & 0  \\
            1  & 0  & 0  \\
            2  & 0  & 0  \\
            3  & 0  & 0  \\
            4  & 0  & 0  \\
            5  & 0  & 0  \\
            6  & 0  & 0  \\
            7  & 0  & 0  \\
            8  & 0  & 0  \\
            9  & 0  & 0  \\
            10  & 0  & 0  \\
            11  & 0  & 0  \\
            12  & 0  & 0  \\
            13  & 1  & 0  \\
            14  & 46  & 6  \\
            15  & 66  & 9  \\
            16  & 157  & 18  \\
            17  & 649  & 79  \\
        }\mydata
    \begin{axis}[
        width=10cm,
        height=7cm,
        ybar,
        % to make the individual bars independent of the `width' of the
        % surrounding axis give a `bar width' in axis units
        /pgf/bar width=1,
        legend style={at={(0.5,0.98)},
            anchor=north,legend columns=-1},
%        symbolic x coords={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17},   % <-- the trailing comma was missing
        xtick=data,
        nodes near coords,
%        nodes near coords align={vertical},    % not needed
        nodes near coords style={
        ylabel={\%},
    ]
        \addplot table[x=interval,y=dynamic]{\mydata};
        \addplot table[x=interval,y=static]{\mydata};
        \legend{Dynamic,Static}
    \end{axis}
\end{tikzpicture}
\end{document}

我明白了

该图显示了上述代码的结果

相关内容