2

I need to make my histograms look like these two:

s

I would like to keep my percentages on the bars, though. This is my actual layout:

enter image description here

I now need to get rid of those little lines above the boxes and redirect them towards inside, as in the picture I want to look like:

enter image description here

CODE:

\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}
2
  • 1
    You only need to remove axis x line=bottom, and axis y line=left, or is there more that you want?
    – user121799
    Commented Jun 1, 2019 at 14:51
  • Thank you @marmot. I have updated the post. I need to get rid of those little lines above the box
    – user189304
    Commented Jun 1, 2019 at 14:56

1 Answer 1

1

I mainly removed axis x line=bottom and axis y line=left and added xtick align=inside.

\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,
    width=0.8\textwidth,height=1.2\textwidth,
    ybar, 
    xlabel={Rating},
    ylabel={Percentage},
    label style={font=\sffamily},
    ymin=0,
    ytick=\empty,
    xtick=data,
    enlarge x limits=0.2,
    xticklabel style={anchor=base,yshift=-\baselineskip},
    nodes near coords={\pgfmathprintnumber\pgfplotspointmeta\%},
    nodes near coords style={font=\scriptsize},
    xtick align=inside
    }

\begin{subfigure}{0.46\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}\hfill
\begin{subfigure}{0.46\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

6
  • Does this actually look like a histogram or more like a bar chart? Maybe because of the term "Percentage" on the y-axis, it can be considered a histogram, since it is measuring frequencies. What do you think?
    – user189304
    Commented Jun 1, 2019 at 17:37
  • 1
    @Delan I do not now. I am even not sure to which extent these terms are mutually exclusive.
    – user121799
    Commented Jun 1, 2019 at 17:43
  • How can you reduce the space between bars?
    – user189304
    Commented Jun 1, 2019 at 18:22
  • 1
    @Delan \begin{axis}[bar width=18pt] instead of \begin{axis}[bar width=22pt], say.
    – user121799
    Commented Jun 1, 2019 at 18:24
  • 1
    @Delan OK, sorry, I misread. You want to decrease the space between the bars? Then use e.g. x=\linewidth/7 instead of x=\linewidth/6 (you need to make x smaller).
    – user121799
    Commented Jun 1, 2019 at 18:30

You must log in to answer this question.