0

Does anyone know how I can bring the groups of bars closer together? When I comment out the enlarge x limits={abs=0.1} the graph is the correct size, but bars too far apart, and when I include it, the bars come closer together but the graph is too small/not everything fits in the graph.

\begin{figure}
   \centering
\makeatletter
\pgfplotsset{
    calculate offset/.code={
        \pgfkeys{/pgf/fpu=true,/pgf/fpu/output format=fixed}
        \pgfmathsetmacro\testmacro{(\pgfplotspointmeta *10^\pgfplots@data@scale@trafo@EXPONENT@y)*\pgfplots@y@veclength)}
        \pgfkeys{/pgf/fpu=false}
    },
    every node near coord/.style={
        /pgfplots/calculate offset,
        yshift=-\testmacro
    }
}
% gating 1, 2, 3
\pgfplotstableread{
% Q S U
1 1219 937 2547
2 2576 3578 2387
3 1424 462 158
}\dataset
\begin{tikzpicture}
\begin{axis}[ybar,
        bar width=20pt,
        enlarge x limits={abs=0.1}    
        width=14cm,
        height=6cm,
        ymin=0,
        ymax=4000,        
        ylabel={frequency},
        xtick=data,
        xticklabels = {
            Gating1,
            Gating2,
            Gating3,
        },
    %    enlarge x limits={abs=1}    
        xlabel=Stimuli Condition,
        xlabel style={yshift=-10ex},
        xticklabel style={yshift=-10ex},
        major x tick style = {opacity=0},
        minor x tick num = 1,
        minor tick length=1ex,
        every node near coord/.append style={
                anchor=east,
                rotate=90
        }
        ]

\addplot[draw=black,fill=blue!20, nodes near coords=Question] table[x index=0,y index=1] \dataset;
\addplot[draw=black,fill=red!20, nodes near coords=Statement] table[x index=0,y index=2] \dataset;
\addplot[draw=black,fill=green!20, nodes near coords=Uncertain] table[x index=0,y index=3] \dataset;

\end{axis}
\end{tikzpicture}
\caption{Frequency and Proportion of Responses by Utterance Type and Gating Condition}\label{graph Frequency and Proportion of Responses by Utterance Type and Gating Condition}

\end{figure}
1
  • Your code is not minimal working example (MWE)!
    – Black Mild
    Feb 7, 2020 at 18:42

1 Answer 1

1

Just change

enlarge x limits={abs=0.1} 

to e.g.

enlarge x limits={abs=0.7},

where I also added a comma to make the code run through.

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

\begin{document}
\begin{figure}
   \centering
\makeatletter
\pgfplotsset{
    calculate offset/.code={
        \pgfkeys{/pgf/fpu=true,/pgf/fpu/output format=fixed}
        \pgfmathsetmacro\testmacro{(\pgfplotspointmeta *10^\pgfplots@data@scale@trafo@EXPONENT@y)*\pgfplots@y@veclength)}
        \pgfkeys{/pgf/fpu=false}
    },
    every node near coord/.style={
        /pgfplots/calculate offset,
        yshift=-\testmacro
    }
}
% gating 1, 2, 3
\pgfplotstableread{
% Q S U
1 1219 937 2547
2 2576 3578 2387
3 1424 462 158
}\dataset
\begin{tikzpicture}
\begin{axis}[ybar,
        bar width=20pt,
        enlarge x limits={abs=0.7}, 
        width=12cm,
        height=6cm,
        ymin=0,
        ymax=4000,        
        ylabel={frequency},
        xtick=data,
        xticklabels = {
            Gating1,
            Gating2,
            Gating3,
        },
    %    enlarge x limits={abs=1}    
        xlabel=Stimuli Condition,
        xlabel style={yshift=-10ex},
        xticklabel style={yshift=-10ex},
        major x tick style = {opacity=0},
        minor x tick num = 1,
        minor tick length=1ex,
        every node near coord/.append style={
                anchor=east,
                rotate=90
        }
        ]

\addplot[draw=black,fill=blue!20, nodes near coords=Question] table[x index=0,y index=1] \dataset;
\addplot[draw=black,fill=red!20, nodes near coords=Statement] table[x index=0,y index=2] \dataset;
\addplot[draw=black,fill=green!20, nodes near coords=Uncertain] table[x index=0,y index=3] \dataset;

\end{axis}
\end{tikzpicture}
\caption{Frequency and Proportion of Responses by Utterance Type and Gating Condition}\label{graph Frequency and Proportion of Responses by Utterance Type and Gating Condition}

\end{figure}
\end{document}

\documentclass[11pt]{article} 
\usepackage{fullpage, lipsum, tikz}
\usepackage{eso-pic}
\AddToShipoutPictureBG{%
\begin{tikzpicture}[remember picture, overlay]
\node[opacity=.1, inner sep=0pt]
    at(current page.center){\includegraphics[width=14cm,
    angle=45]{example-image-duck}};
\end{tikzpicture}%
}
\begin{document}
\lipsum[1-20] 
\end{document}

\documentclass{standalone}

\usepackage{pgfplots}
\pgfplotsset{no markers}
\pgfplotsset{but I really want a mark/.style={/pgfplots/every axis plot
post/.append style={mark=#1}}}
\begin{document}

\begin{tikzpicture}
  \begin{axis}
    \addplot coordinates { (1,1) (2,2) (3,2) };
  \end{axis}
\end{tikzpicture}

\begin{tikzpicture}
  \begin{axis}[but I really want a mark={*}]% works
    \addplot coordinates { (1,1) (2,2) (3,2) };
  \end{axis}
\end{tikzpicture}
\end{document}

\begin{tikzpicture}
  \begin{axis}[mark options={*}]% does not work
    \addplot[mark=*] coordinates { (1,1) (2,2) (3,2) };% does not work, but strangely changes line color to black?
  \end{axis}
\end{tikzpicture}

\end{document}

enter image description here

BTW, the above is working example, i.e. it can be compiled as is. Since I do not know your preamble, I had to make the width a bit smaller to fit into the standard article class. This is one of the many reasons why you may always want to provide a complete example that others can copy and compile.

1
  • thankyou, that's perfect. the comma made all the difference. Thankyou for your help
    – Helen
    Feb 9, 2020 at 16:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .