如何在页面上指定 tikzpicture 的位置?

如何在页面上指定 tikzpicture 的位置?

我遇到了两个连续的条形图边缘不对齐的问题。 有办法解决这个问题吗?

即有没有办法精确指定页面左边距和 tikzpicture 之间的空间大小? 对齐不良

\documentclass[a4paper]{article}
\usepackage{pgfplots}
\begin{document}

\center
\begin{tikzpicture}
\begin{axis}[
    width=11cm, height=7.5cm,
    /pgf/number format/.cd,
    use comma,
scaled ticks= false,
title style={align=center},
title    = {\textbf{Flowers}},
ybar = 0.9cm,
ymin = 0,
nodes near coords,
xtick=data,
bar width = 1.5cm,
symbolic x coords = {Flowers},
legend style={at={(0.5,-0.03)},anchor=north},
xmajorticks=false
]
\addplot coordinates {(Flowers,123)};
\addplot coordinates {(Flowers,32)};
\legend{dandelion, roses}
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}
\begin{axis}[
    width=11cm, height=7.5cm,
    /pgf/number format/.cd,
    use comma,
scaled ticks= false,
title style={align=center},
title    = {\textbf{Even More Flowers}},
ybar = 0.9cm,
ymin = 0,
nodes near coords,
xtick=data,
bar width = 1.5cm,
symbolic x coords = {Flowers},
legend style={at={(0.5,-0.03)},anchor=north},
xmajorticks=false
]
\addplot coordinates {(Flowers,12553)};
\addplot coordinates {(Flowers,3442)};
\legend{orchids, lilies}
\end{axis}
\end{tikzpicture}

\end{document}

答案1

如果我正确理解了所提供的草图,那么您正在寻找类似这样的东西:

在此处输入图片描述

使用对齐图表的边框很简单\begin{tabular}{r} ... image 1\\ ... image 2 \end{tabular}。由于图像具有相同的大小(不考虑ytick标签,图表的左边框和右边框都是对齐的。

\documentclass[a4paper]{article}
\usepackage{pgfplots}
\begin{document}
    \begin{center}
\begin{tabular}{r}
\begin{tikzpicture}
\begin{axis}[
    width=11cm, height=7.5cm,
    /pgf/number format/.cd,
    use comma,
scaled ticks= false,
title style={align=center},
title    = {\textbf{Flowers}},
ybar = 0.9cm,
ymin = 0,
nodes near coords,
xtick=data,
bar width = 1.5cm,
symbolic x coords = {Flowers},
legend style={at={(0.5,-0.03)},anchor=north},
xmajorticks=false
]
\addplot coordinates {(Flowers,123)};
\addplot coordinates {(Flowers,32)};
\legend{dandelion, roses}
\end{axis}
\end{tikzpicture}     \\
%
\begin{tikzpicture}
\begin{axis}[
    width=11cm, height=7.5cm,
    /pgf/number format/.cd,
    use comma,
scaled ticks= false,
title style={align=center},
title    = {\textbf{Even More Flowers}},
ybar = 0.9cm,
ymin = 0,
nodes near coords,
xtick=data,
bar width = 1.5cm,
symbolic x coords = {Flowers},
legend style={at={(0.5,-0.03)},anchor=north},
xmajorticks=false
]
\addplot coordinates {(Flowers,12553)};
\addplot coordinates {(Flowers,3442)};
\legend{orchids, lilies}
\end{axis}
\end{tikzpicture}
\end{tabular}
    \end{center}
\end{document}

附录: 考虑pgfplots软件包作者的评论博士克里斯蒂安·费尔桑格下面,使用trim axis left选项可以获得相同的结果tikzpicture(有关详细信息,请参阅pgfplots 手册,第 379 -- 380 页):

\documentclass[a4paper]{article}
\usepackage{pgfplots}

\begin{document}
    \begin{center}
\begin{tikzpicture}[trim axis left] % <----- 
\begin{axis}[
    width=11cm, height=7.5cm,
    /pgf/number format/.cd,
    use comma,
scaled ticks= false,
title style={align=center},
title    = {\textbf{Flowers}},
ybar = 0.9cm,
ymin = 0,
nodes near coords,
xtick=data,
bar width = 1.5cm,
symbolic x coords = {Flowers},
legend style={at={(0.5,-0.03)},anchor=north},
xmajorticks=false,
trim axis left
]
\addplot coordinates {(Flowers,123)};
\addplot coordinates {(Flowers,32)};
\legend{dandelion, roses}
\end{axis}
\end{tikzpicture}

\begin{tikzpicture}[trim axis left] % <----- 
\begin{axis}[
    width=11cm, height=7.5cm,
    /pgf/number format/.cd,
    use comma,
scaled ticks= false,
title style={align=center},
title    = {\textbf{Even More Flowers}},
ybar = 0.9cm,
ymin = 0,
nodes near coords,
xtick=data,
bar width = 1.5cm,
symbolic x coords = {Flowers},
legend style={at={(0.5,-0.03)},anchor=north},
xmajorticks=false,
]
\addplot coordinates {(Flowers,12553)};
\addplot coordinates {(Flowers,3442)};
\legend{orchids, lilies}
\end{axis}
\end{tikzpicture}
    \end{center}
\end{document}

相关内容