3

I'm trying to create a simple bar graph with four data sets. But, when I typeset, it produces a plot but the bars seem to overlay each other, which I want to eliminate. How do I fix this? I tried editing the x-axis or the enlarge x limits, but didn't do anything. Please help.

\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}

The text is typed below:

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

enter image description here

  • Could you provide the data file as text, or use \addplot coordinates instead of \addplot table? A screenshot is not very friendly for those wanting to test your example. – Torbjørn T. Jul 23 '19 at 21:41
  • @TorbjørnT. I have added the text please let me know and thank you. – Dave124 Jul 23 '19 at 21:49
2

I'll quote myself first:

The space you have available for each group of bars (each symbolic coord) basically depends on 1) on the number of groups, 2) the number of bars in each group (the number of \addplots), 3) the width of the bars (set by bar width), 4) the space between the bars in the group (set by the argument passed to ybar, e.g. ybar=3pt), 5) the total width of the plot (set by the width key) and 6) the distance from the edge of the axis to the bars (for your code set by enlarge x limits).

For your case, you can for example do something like bar width=0.1cm, or ybar=0, bar width=0.15cm. The latter will look like this:

enter image description here

You will also want enlarge x limits={abs=0.5},, not enlarge x limits=0, otherwise some bars are hidden, outside the axis.

% 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}
  • Thank you. That looks much better! – Dave124 Jul 23 '19 at 22:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.