3

I am trying to put these two histograms side by side but I cannot do it. Could you give me a hand?

enter image description here

mwe:

\documentclass[11pt]{book}
\usepackage[T1]{fontenc}
\usepackage{pgfplots}
\usepackage{adjustbox}

\usepackage{tikz}
\usetikzlibrary{positioning}
\usepackage{caption}
\usepackage{subcaption}
\captionsetup[subfigure]{font=footnotesize}
\usepackage{graphicx}

\pgfplotsset{
    compat=newest,
    xlabel near ticks,
    ylabel near ticks
}

\begin{document}

\begin{figure}


        \centering

    \begin{minipage}{0.5\textwidth}

    \begin{adjustbox}{width=5cm, height=5cm, keepaspectratio}
    \begin{tikzpicture}[font=\small]
    \begin{axis}[
    ybar,
    bar width=20pt,
    xlabel={Rating},
    ylabel={Percentage},
    ymin=0,
    ytick=\empty,
    xtick=data,
    axis x line=bottom,
    axis y line=left,
    enlarge x limits=0.2,
    xticklabel style={anchor=base,yshift=-\baselineskip},
    nodes near coords={\pgfmathprintnumber\pgfplotspointmeta\%}
    ]
    \addplot[blue,fill=blue!10] coordinates {
        (1, 6.110)
        (2, 11.370)
        (3 ,    27.145)
        (4  ,34.174)
        (5  ,21.201)
    };
    \end{axis}
    \end{tikzpicture}
    \end{adjustbox}
\caption{Two}
\end{minipage}

\begin{minipage}{0.5\textwidth}

    \begin{adjustbox}{width=5cm, height=5cm, keepaspectratio}
    \begin{tikzpicture}[font=\small]
    \begin{axis}[
    ybar,
    bar width=20pt,
    xlabel={Rating},
    ylabel={Percentage},
    ymin=0,
    ytick=\empty,
    xtick=data,
    axis x line=bottom,
    axis y line=left,
    enlarge x limits=0.2,
    %symbolic x coords={excellent,good,average,bad,awful},
    xticklabel style={anchor=base,yshift=-\baselineskip},
    nodes near coords={\pgfmathprintnumber\pgfplotspointmeta\%}
    ]
    \addplot[fill=orange] coordinates {
        (1, 5.616226)
        (2, 10.753453)
        (3 ,    27.145)
        (4  ,34.889808)
        (5  ,   22.626271)
    };
    \end{axis}
    \end{tikzpicture}
        \end{adjustbox}
    \caption{One}


\end{minipage}
\caption{blablabla}

\end{figure}
\end{document}
  • your captions are unusual. Or is third caption superfluous or are the first two actually sub-captions? – Zarko May 31 at 13:53
  • I just put whatever, they are nonsense. – Delan May 31 at 14:53
5

Just remove the blank line between the two minipage environments and, as Erik says in the comments, put a % after the first \end{minipage}:

enter image description here

Here's the full code:

\documentclass[11pt]{book}
\usepackage[T1]{fontenc}
\usepackage{pgfplots}
\usepackage{adjustbox}

\usepackage{tikz}
\usetikzlibrary{positioning}
\usepackage{caption}
\usepackage{subcaption}
\captionsetup[subfigure]{font=footnotesize}
\usepackage{graphicx}

\pgfplotsset{
    compat=newest,
    xlabel near ticks,
    ylabel near ticks
}

\begin{document}

\begin{figure}


        \centering

    \begin{minipage}{0.5\textwidth}

    \begin{adjustbox}{width=5cm, height=5cm, keepaspectratio}
    \begin{tikzpicture}[font=\small]
    \begin{axis}[
    ybar,
    bar width=20pt,
    xlabel={Rating},
    ylabel={Percentage},
    ymin=0,
    ytick=\empty,
    xtick=data,
    axis x line=bottom,
    axis y line=left,
    enlarge x limits=0.2,
    xticklabel style={anchor=base,yshift=-\baselineskip},
    nodes near coords={\pgfmathprintnumber\pgfplotspointmeta\%}
    ]
    \addplot[blue,fill=blue!10] coordinates {
        (1, 6.110)
        (2, 11.370)
        (3 ,    27.145)
        (4  ,34.174)
        (5  ,21.201)
    };
    \end{axis}
    \end{tikzpicture}
    \end{adjustbox}
\caption{Two}
\end{minipage}%
\begin{minipage}{0.5\textwidth}

    \begin{adjustbox}{width=5cm, height=5cm, keepaspectratio}
    \begin{tikzpicture}[font=\small]
    \begin{axis}[
    ybar,
    bar width=20pt,
    xlabel={Rating},
    ylabel={Percentage},
    ymin=0,
    ytick=\empty,
    xtick=data,
    axis x line=bottom,
    axis y line=left,
    enlarge x limits=0.2,
    %symbolic x coords={excellent,good,average,bad,awful},
    xticklabel style={anchor=base,yshift=-\baselineskip},
    nodes near coords={\pgfmathprintnumber\pgfplotspointmeta\%}
    ]
    \addplot[fill=orange] coordinates {
        (1, 5.616226)
        (2, 10.753453)
        (3 ,    27.145)
        (4  ,34.889808)
        (5  ,   22.626271)
    };
    \end{axis}
    \end{tikzpicture}
        \end{adjustbox}
    \caption{One}


\end{minipage}
\caption{blablabla}

\end{figure}
\end{document}
  • 1
    Or keep 0.5\textwidth and use \end{minipage}% – erik May 31 at 14:09
  • @erik Ha! Thanks. Definitely better! Updated. – Andrew May 31 at 14:17
3

Off-topic (since your main problem is solved by other answer).

I suspect, that you like to have two sub figures. For this I would use only subfigure environment, i.e. without minipage and especial without adjustbox.

\documentclass[11pt]{book}
\usepackage[T1]{fontenc}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\usetikzlibrary{positioning}

\usepackage{caption}
\usepackage{subcaption}
\captionsetup[subfigure]{font=footnotesize}

\begin{document}
    \begin{figure}
    \centering
\pgfplotsset{x=\linewidth/6,
    ybar, 
    xlabel={Rating},
    ylabel={Percentage},
    ymin=0,
    ytick=\empty,
    xtick=data,
    axis x line=bottom,
    axis y line=left,
    enlarge x limits=0.2,
    xticklabel style={anchor=base,yshift=-\baselineskip},
    nodes near coords={\pgfmathprintnumber\pgfplotspointmeta\%},
    nodes near coords style={font=\scriptsize},
    }

\begin{subfigure}{0.49\linewidth}
     \begin{tikzpicture}
    \begin{axis}[bar width=22pt]
    \addplot[blue,fill=blue!10] coordinates {
        (1, 6.110)
        (2, 11.370)
        (3, 27.145)
        (4, 34.174)
        (5, 21.201)
    };
    \end{axis}
    \end{tikzpicture}
    \caption{Two}
\end{subfigure}
\begin{subfigure}{0.49\textwidth}
     \begin{tikzpicture}
    \begin{axis}[bar width=22pt]
    \addplot[fill=orange] coordinates {
        (1, 5.616226)
        (2, 10.753453)
        (3, 27.145)
        (4, 34.889808)
        (5, 22.626271)
    };
    \end{axis}
    \end{tikzpicture}
    \caption{One}
\end{subfigure}
\caption{blablabla}
    \end{figure}
\end{document}

enter image description here

  • 1
    IMHO this should, at best, be a comment on Andrew's nice answer, not a separate answer. – marmot May 31 at 15:02
  • Mmm, good point! Haha, you were/are right. Thank you! – Delan May 31 at 15:02
  • +1 @marmot My answer is really only a comment on the original post ! :) – Andrew May 31 at 22:13
1
\documentclass[border=5mm]{standalone}
\usepackage{filecontents}
\usepackage{pgfplots, pgfplotstable}
\usepgfplotslibrary{statistics}

\begin{filecontents}{data.csv}
dist
1
2
2.5
2
1
3.5
3
1
3
2
1
1
0.5
1
1.5
1
\end{filecontents}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
    ybar,
    ymin=0
]
\addplot +[
    hist={
        bins=7,
        data min=0.5,
        data max=4
    }   
] table [y index=0] {data.csv};
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

  • Your answer only includes one tikzpicture. Could you please tell how this answers to original question that asked about placing two tikzpictures side-by-side? – leandriis Jul 6 at 12:55

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.