我正在摸索着使用 pgfplots。一页上有 3 个图。我有几个问题:
- 图中的条形在不同程度上浮动在轴上方。如何将条形固定到轴上?
2.我希望上部条形图和下部条形图之间有更多间距,我该如何实现?我试过 \vspace,但没有效果。
请参阅我的 MWE
\documentclass{article}
\usepackage{booktabs, caption}
\usepackage{tabularx}
\usepackage[online]{threeparttable}
\usepackage{siunitx}
\usepackage{graphics}
\usepackage{adjustbox}
\usepackage{pgfplots}
\pgfplotsset{compat=1.9}
\usepackage{amsmath}
\begin{document}
\begin{tikzpicture} [baseline]
\begin{axis}[
x=1.5cm,
bar width=.4cm,
%enlarge x limits=1,
xticklabels={0,1,2,$\geq3$},
title= \textbf{\# of cats per week for whom I was directly repsonsible},
title style={align=center,text width=5.2cm},
enlargelimits=.15,
ylabel=\textbf{Frequency},
xlabel=\textbf{\# of cats},
xtick={0,...,3}, % To make sure the tick labels match the data points
xticklabel style={text height=2ex},
nodes near coords,
nodes near coords align={vertical}] % Make all letters the same height so they align properly
\addplot[ybar] coordinates {
(0, 4)
(1, 9)
(2, 26)
(3, 33)
};
\end{axis}
\end{tikzpicture}
\hspace{1cm}
\begin{tikzpicture}[baseline]
\begin{axis}[
xshift=4em,
x=1.5cm,
bar width=.4cm,
%enlarge x limits=1,
xticklabels={0,1,2,$\geq3$},
title= \textbf{Avg \# of cats from whom I obtained a complete physical exam and presented the results to the Vet was:
},
title style={align=center,text width=7cm},
enlargelimits=.15,
ylabel=\textbf{Frequency},
xlabel=\textbf{\# of cats},
xtick={0,...,3}, % To make sure the tick labels match the data points
xticklabel style={text height=2ex},
nodes near coords,
nodes near coords align={vertical}] % Make all letters the same height so they align properly
\addplot[ybar] coordinates {
(0, 3)
(1, 17)
(2, 20)
(3, 31)
};
\end{axis}
\end{tikzpicture}
\vspace{5pt}
\begin{tikzpicture}[baseline]
\begin{axis}[
xshift=4em,
x=1.5cm,
bar width=.4cm,
%enlarge x limits=1,
xticklabels={0,1,2,$\geq3$},
title= \textbf{Avg \# of procedures (e.g., lumbar punctures) that I was allowed to perform was:},
title style={align=center,text width=7cm},
enlargelimits=.15,
ylabel=\textbf{Frequency},
xlabel=\textbf{\# of procedures},
xtick={0,...,3}, % To make sure the tick labels match the data points
xticklabel style={text height=2ex},
nodes near coords,
nodes near coords align={vertical}] % Make all letters the same height so they align properly
\addplot[ybar] coordinates {
(0, 53)
(1, 8)
(2, 7)
(3, 3)
};
\end{axis}
\end{tikzpicture}
\end{document}
答案1
如图所示https://tex.stackexchange.com/a/208681/32374,您需要更改指定轴的放大方式。而不是enlargelimits=0.15
,而是使用
enlarge x limits=.15,
enlarge y limits={0.15, upper},
然后,我还必须指定ymin=0
。我还编辑了您的轴以使用样式ybar
而不是图,因为这会设置刻度线等,因此它们(恕我直言)看起来会更好一些。
对于第二个问题,您只需通过插入一些额外的空白行将第二张图移到新段落即可。然后您的\vspace
代码就会起作用。请参见下面的修复示例。请注意,我在此示例中“最小化”了您的序言。
最后,最新版本的pgfplots
截至撰写本文时1.11
- 您不使用它的原因是什么?
\documentclass{article}
\usepackage[margin=1in]{geometry}
\pagestyle{empty}
\usepackage{pgfplots}
\pgfplotsset{compat=1.9}
\begin{document}
\begin{tikzpicture}[baseline]
\begin{axis}[
x=1.5cm,
bar width=.4cm,
%enlarge x limits=1,
xticklabels={0,1,2,$\geq3$},
title= \textbf{\# of cats per week for whom I was directly repsonsible},
title style={align=center,text width=5.2cm},
% Change these lines
enlarge x limits=.15,
enlarge y limits={0.15, upper},
ymin=0,
ybar,
%Change the above lines
ylabel=\textbf{Frequency},
xlabel=\textbf{\# of cats},
xtick={0,...,3}, % To make sure the tick labels match the data points
xticklabel style={text height=2ex},
nodes near coords,
nodes near coords align={vertical} % Make all letters the same height so they align properly
]
\addplot[black, fill=none] coordinates {
(0, 4)
(1, 9)
(2, 26)
(3, 33)
};
\end{axis}
\end{tikzpicture}
%
\hspace{1cm}
%
\begin{tikzpicture}[baseline]
\begin{axis}[
xshift=4em,
x=1.5cm,
bar width=.4cm,
%enlarge x limits=1,
xticklabels={0,1,2,$\geq3$},
title= \textbf{Avg \# of cats from whom I obtained a complete physical exam and presented the results to the Vet was:
},
title style={align=center,text width=7cm},
% Change these lines
enlarge x limits=.15,
enlarge y limits={0.15, upper},
ymin=0,
ybar,
%Change the above lines
ylabel=\textbf{Frequency},
xlabel=\textbf{\# of cats},
xtick={0,...,3}, % To make sure the tick labels match the data points
xticklabel style={text height=2ex},
nodes near coords,
nodes near coords align={vertical}
] % Make all letters the same height so they align properly
\addplot[black, fill=none] coordinates {
(0, 3)
(1, 17)
(2, 20)
(3, 31)
};
\end{axis}
\end{tikzpicture}
\vspace{1in}
\begin{tikzpicture}[baseline]
\begin{axis}[
xshift=4em,
x=1.5cm,
bar width=.4cm,
%enlarge x limits=1,
xticklabels={0,1,2,$\geq3$},
title= \textbf{Avg \# of procedures (e.g., lumbar punctures) that I was allowed to perform was:},
title style={align=center,text width=7cm},
% Change these lines
enlarge x limits=.15,
enlarge y limits={0.15, upper},
ymin=0,
ybar,
%Change the above lines
ylabel=\textbf{Frequency},
xlabel=\textbf{\# of procedures},
xtick={0,...,3}, % To make sure the tick labels match the data points
xticklabel style={text height=2ex},
nodes near coords,
nodes near coords align={vertical} % Make all letters the same height so they align properly
]
\addplot[black, fill=none] coordinates {
(0, 53)
(1, 8)
(2, 7)
(3, 3)
};
\end{axis}
\end{tikzpicture}
\end{document}