我正在制作两个数字pgf图包,其中一个在顶部,另一个在底部。它们之间有空白。我该如何删除该空白?此外,我如何才能使所有内容都像 一样宽\textwidth
?
\documentclass{article}
\usepackage{lmodern}
\usepackage{pgfplots}
\pgfplotsset{compat=1.8}
\usepackage{tikz}
\usepackage[showframe]{geometry}
\begin{document}
\begin{figure}
\centering
\pgfplotsset{
scale only axis,
enlargelimits=false,
width=0.4\textwidth,
height=2cm,
xmin=0.5,
xmax=16.5,
xticklabel=\empty,
xtick={1,2,...,16},
leftinnery/.style={
every y tick label/.append style=green!75!black,
ytick style=green!75!black,
ylabel=a,
ylabel style=green!75!black,
axis y line*=left,
y axis line style=green!75!black,
},
rightinnery/.style={
every y tick label/.append style=blue,
ytick style=blue,
ylabel=b,
ylabel style=blue,
axis y line*=right,
y axis line style=blue,
},
leftoutery/.style={
every y tick label/.append style={red,xshift=-1.7cm},
ytick style={red,xshift=-1.7cm},
ylabel=c,
ylabel style=red,
axis y line*=left,
y axis line style={red,xshift=-1.7cm},
},
rightoutery/.style={
every y tick label/.append style={brown,xshift=1.4cm},
ytick style={brown,xshift=1.4cm},
ylabel=d,
ylabel style=brown,
axis y line*=right,
y axis line style={brown,xshift=1.4cm},
},
}
\begin{tikzpicture}
\begin{axis}[leftinnery]
\addplot[green!75!black, mark=o, draw] table[x index=0]{
1 10
};
\node[anchor=north west] at (rel axis cs:0,1) {东湖};
\end{axis}
\begin{axis}[rightinnery]
\addplot[blue, mark=x, draw] table[x index=0]{
1 2
};
\end{axis}
\begin{axis}[leftoutery]
\addplot[red, mark=+, draw] table[x index=0]{
1 1000
};
\end{axis}
\begin{axis}[rightoutery]
\addplot[brown, mark=Mercedes star, draw] table[x index=0]{
1 3
};
\end{axis}
\end{tikzpicture}\\
\begin{tikzpicture}[yshift=-\lineskip]
\begin{axis}[leftinnery]
\addplot[green!75!black, mark=o, draw] table[x index=0]{
1 400
};
\node[anchor=north west] at (rel axis cs:0,1) {东湖};
\end{axis}
\begin{axis}[rightinnery]
\addplot[blue, mark=x, draw] table[x index=0]{
1 2
};
\end{axis}
\begin{axis}[leftoutery]
\addplot[red, mark=+, draw] table[x index=0]{
1 3
};
\end{axis}
\begin{axis}[rightoutery]
\addplot[brown, mark=Mercedes star, draw] table[x index=0]{
1 40
};
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}
这将产生以下内容:
似乎图形左侧有一点空白,所以图形稍微向右移动了一点。我怎样才能删除这个空白?如何对齐所有标签和 y 轴?我想这很难。使用群体图可以这样做,但是如何绘制多个 y 轴?
答案1
诀窍是使用scope
而不是新的tikzpicture
。我使用 对齐了 y 标签ylabel absolute
,同时将所有标签放在彼此的顶部。
\documentclass{article}
\usepackage{lmodern}
\usepackage{pgfplots}
\pgfplotsset{compat=1.8}
\usepackage{tikz}
\usepackage[showframe]{geometry}
\begin{document}
\begin{figure}
\centering
\pgfplotsset{
scale only axis,
enlargelimits=false,
width=0.4\textwidth,
height=2cm,
xmin=0.5,
xmax=16.5,
xticklabel=\empty,
xtick={1,2,...,16},
ylabel absolute,
leftinnery/.style={
every y tick label/.append style=green!75!black,
ytick style=green!75!black,
ylabel=a,
ylabel style=green!75!black,
axis y line*=left,
y axis line style=green!75!black,
},
rightinnery/.style={
every y tick label/.append style=blue,
ytick style=blue,
ylabel=b,
ylabel style={blue, yshift=\dimexpr -0.4\textwidth-2.1cm},% 2.1cm by trial and error
axis y line*=right,
y axis line style=blue,
},
leftoutery/.style={
every y tick label/.append style={red,xshift=-1.7cm},
ytick style={red,xshift=-1.7cm},
ylabel=c,
ylabel style={red,yshift=1.7cm},
ylabel shift=-1.7cm,
axis y line*=left,
y axis line style={red,xshift=-1.7cm},
},
rightoutery/.style={
every y tick label/.append style={brown,xshift=1.4cm},
ytick style={brown,xshift=1.4cm},
ylabel=d,
ylabel style={brown, yshift=\dimexpr -0.4\textwidth-2.1cm-1.4cm},
axis y line*=right,
y axis line style={brown,xshift=1.4cm},
},
}
\begin{tikzpicture}
\begin{scope}
\begin{axis}[leftinnery]
\addplot[green!75!black, mark=o, draw] table[x index=0]{
1 10
};
\node[anchor=north west] at (rel axis cs:0,1) {东湖};
\end{axis}
\begin{axis}[rightinnery]
\addplot[blue, mark=x, draw] table[x index=0]{
1 2
};
\end{axis}
\begin{axis}[leftoutery]
\addplot[red, mark=+, draw] table[x index=0]{
1 1000
};
\end{axis}
\begin{axis}[rightoutery]
\addplot[brown, mark=Mercedes star, draw] table[x index=0]{
1 3
};
\end{axis}
\end{scope}
\begin{scope}[yshift=-2cm]
\begin{axis}[leftinnery]
\addplot[green!75!black, mark=o, draw] table[x index=0]{
1 400
};
\node[anchor=north west] at (rel axis cs:0,1) {东湖};
\end{axis}
\begin{axis}[rightinnery]
\addplot[blue, mark=x, draw] table[x index=0]{
1 2
};
\end{axis}
\begin{axis}[leftoutery]
\addplot[red, mark=+, draw] table[x index=0]{
1 3
};
\end{axis}
\begin{axis}[rightoutery]
\addplot[brown, mark=Mercedes star, draw] table[x index=0]{
1 40
};
\end{axis}
\end{scope}
\end{tikzpicture}
\end{figure}
\end{document}