我需要使我的直方图看起来像这两个:
不过,我想保留条形图上的百分比。这是我的实际布局:
我现在需要去掉盒子上方的那些小线条,并将它们重定向到内部,就像我想要的图片那样:
代码:
\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}
答案1
我主要删除了axis x line=bottom
和axis y line=left
并添加了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}