拆分条形图列

拆分条形图列

我正在尝试创建一个包含四个数据集的简单条形图。但是,当我排版时,它会产生一个图,但条形似乎相互重叠,我想消除这种情况。我该如何解决这个问题?我尝试编辑 x 轴或放大 x 限制,但没有任何效果。请帮忙。

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.13}


\begin{document}
\newread\file
    \openin\file=frequency.txt
\begin{tikzpicture}
\begin{axis}[
    ymin=0,
        ymajorgrids = true,
        ymax=0.6,
    ybar,
    bar width=.2cm,
            width=\textwidth,
            height=.5\textwidth,
            legend style={at={(0.5,1)},
            anchor=north,legend columns=-1},
           nodes near coords align={vertical},
        xtick={1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
         enlarge x limits=0,
    ylabel={\textbf{Frequency}},
    xlabel={\textbf{Radius Ratio}}]

    \addplot table[x=x,y=DOxide] {frequency.txt};
    \addplot table[x=x,y=DOxalate] {frequency.txt};
    \addplot table[x=x,y=ROxide] {frequency.txt};
    \addplot table[x=x,y=ROxalate] {frequency.txt};
    \legend{D-Oxide, D-Oxalate, R-Oxide, R-Oxalate}
\end{axis}
\end{tikzpicture}

\end{document}

文本输入如下:

x   DOxide      DOxalate    ROxide      ROxalate
1   0           0.018735363 0           0.042253521
2   0           0.046838407 0           0.052816901
3   0           0.100702576 0           0.077464789
4   0           0.045667447 0           0.028169014
5   0           0.003512881 0           0.003521127
6   0           0.014051522 0           0.01056338
7   0.003205128 0.005854801 0.006756757 0.007042254
8   0.003205128 0.015222482 0.030405405 0.014084507
9   0.009615385 0.026932084 0.064189189 0.042253521
10  0.121794872 0.050351288 0.091216216 0.038732394
11  0.493589744 0.216627635 0.358108108 0.257042254
12  0.291666667 0.244730679 0.283783784 0.25
13  0.044871795 0.163934426 0.131756757 0.126760563
14  0.019230769 0.037470726 0.030405405 0.038732394
15  0.012820513 0.009367681 0.003378378 0.01056338

在此处输入图片描述

答案1

我引用一下第一的:

每组条形图(每个符号坐标)可用的空间基本上取决于 1)组数,2)每组中条形图的数量(的数量\addplots),3)条形图的宽度(由 设置bar width),4)组中条形图之间的空间(由传递给 的参数设置ybar,例如ybar=3pt),5)图的总宽度(由键设置width)和 6)从轴边缘到条形图的距离(对于您的代码由 设置enlarge x limits)。

对于您的情况,您可以执行类似bar width=0.1cm或 的操作ybar=0, bar width=0.15cm。后者将如下所示:

在此处输入图片描述

您还需要enlarge x limits={abs=0.5},,而不是enlarge x limits=0,否则一些条形图将被隐藏在轴之外。

% the filecontents environment here writes its content to the filename given
% you already have the file of course, so you won't need it for your own use
% it is just to make the example self-contained
\begin{filecontents*}{frequency.txt}
x   DOxide      DOxalate    ROxide      ROxalate
1   0           0.018735363 0           0.042253521
2   0           0.046838407 0           0.052816901
3   0           0.100702576 0           0.077464789
4   0           0.045667447 0           0.028169014
5   0           0.003512881 0           0.003521127
6   0           0.014051522 0           0.01056338
7   0.003205128 0.005854801 0.006756757 0.007042254
8   0.003205128 0.015222482 0.030405405 0.014084507
9   0.009615385 0.026932084 0.064189189 0.042253521
10  0.121794872 0.050351288 0.091216216 0.038732394
11  0.493589744 0.216627635 0.358108108 0.257042254
12  0.291666667 0.244730679 0.283783784 0.25
13  0.044871795 0.163934426 0.131756757 0.126760563
14  0.019230769 0.037470726 0.030405405 0.038732394
15  0.012820513 0.009367681 0.003378378 0.01056338
\end{filecontents*}
\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.13}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
    ymin=0,
    ymax=0.6,
    ymajorgrids = true,
    ybar=0,
    bar width=.15cm,
    width=\textwidth,
    height=.5\textwidth,
    legend style={at={(0.5,1)},
    anchor=north,legend columns=-1},
    nodes near coords align={vertical},
    xtick={1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
    enlarge x limits={abs=0.5},
    ylabel={\textbf{Frequency}},
    xlabel={\textbf{Radius Ratio}}]

    \addplot table[x=x,y=DOxide] {frequency.txt};
    \addplot table[x=x,y=DOxalate] {frequency.txt};
    \addplot table[x=x,y=ROxide] {frequency.txt};
    \addplot table[x=x,y=ROxalate] {frequency.txt};
    \legend{D-Oxide, D-Oxalate, R-Oxide, R-Oxalate}
\end{axis}
\end{tikzpicture}

\end{document}

相关内容