这是对前两个问题的后续回答:
1:使用 pgfplots 和 tikzscale 的字体大小缩放和节点放置问题
2:将多列TikZ / pgfplots groupplot缩放到页面宽度
我想将多列组图缩放到一定宽度(通常为\textwidth
),同时保持字体大小和节点位置不变。从问题 1 中我了解到,使用时tikzscale
,不应为环境定义hight
和/或,以保持字体大小和节点位置不变。问题 2 的答案是,我应该使用来缩放到所需的整体宽度。但如果没有定义和,那么和的组合将产生错误: width
axis
tikzscale
groupplots
hight
width
groupplot
tikzscale
!包 pgfplots 错误:错误:绘图高度“-77.46211pt”太小。在保持标签大小不变的情况下无法实现此操作。抱歉,标签大小仅为近似值。您需要调整高度。
绘图将无法正确缩放。
我的问题的简短版本是:“如何将多列组图缩放到一定宽度,同时仍保持字体大小和节点位置?”
以下 MWE 说明了这个问题。
\documentclass[10pt,crop,varwidth=250pt]{standalone}
\usepackage[english]{babel}
\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}
\usepackage{tikzscale}
\pgfplotsset{compat=1.10}
\usepackage{filecontents}
\begin{document}
\begin{filecontents}{A.tikz}
\begin{tikzpicture}
\begin{groupplot}[
group style={
group size =2 by 1,
horizontal sep = 1cm
},
width=125pt,
height=125pt,
xmin=-1,xmax=1,ymin=-1,ymax=1,
xlabel=xlabel,
]
\nextgroupplot[
ylabel=ylabel,
]
\addplot coordinates{ (-0.9,-0.9) (0.9,0.9) };
\node[anchor=west] at (axis cs:-1,0) {01};
\coordinate (a) at (axis cs:-1,0);
\nextgroupplot
\addplot coordinates{ (-0.9,-0.9) (0.9,0.9) };
\end{groupplot}
\node[anchor=west,red] at (a) {02};
\end{tikzpicture}
\end{filecontents}
\begin{filecontents}{B.tikz}
\begin{tikzpicture}[transform shape]
\begin{groupplot}[
group style={
group size =2 by 1,
horizontal sep = 1cm
},
% width=125pt, %% this is now omitted
% height=125pt, %% this is now omitted
xmin=-1,xmax=1,ymin=-1,ymax=1,
xlabel=xlabel,
]
\nextgroupplot[
ylabel=ylabel,
]
\addplot coordinates{ (-0.9,-0.9) (0.9,0.9) };
\node[anchor=west] at (axis cs:-1,0) {01};
\coordinate (a) at (axis cs:-1,0);
\nextgroupplot
\addplot coordinates{ (-0.9,-0.9) (0.9,0.9) };
\end{groupplot}
\node[anchor=west,red] at (a) {02};
\end{tikzpicture}
\end{filecontents}
\input{A.tikz}
Manually scaling height and width of the plots requires a lot of tries until an ``acceptable'' result is reached. At least the font size and node placement are correct.
\includegraphics[width=250pt]{A.tikz}
Width and height are defined and tikzscale is used. The plots scale nicely to the desired width, but font size and node placement problems occur.
\input{B.tikz}
Without defined height and width and without tikzscale, the plots are too large to fit the desired textwidth. Font size and node placement work fine.
\includegraphics[width=250pt]{B.tikz}
No definition of width and height, but using tikzscale will produce an error. The plot is not scaled correctly but font size and node placement look fine.
\end{document}
答案1
免责声明:这不是一个完整的答案,因为它只涉及绘图宽度、标签字体大小等,而不涉及节点大小和位置。不幸的是,作为评论,它太长了。
groupplots
我发现通过补偿图之间的水平间距可以获得适当的字体大小。
您的\includegraphics
命令仍通过选项调用width=\textwidth
,但环境内每个图的宽度groupplots
必须设置为1/x*\textwidth-y
,其中x
是水平放置的组图数,y
是指定的水平分隔。
输出:
据我所知,tikzscale
-d 数字的宽度接近完美,并且字体没有扭曲。
但不幸的是,这也不能解决节点放置问题。我希望这是一个很好的起点,其他人可以为您提供帮助。我tikzscale
也经常使用,如果有一个强大的扩展解决方案就太好了groupplots
。
解决方案:
\documentclass{article}
\usepackage[showframe]{geometry}
\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}
\pgfplotsset{compat=1.10}
\usepackage{tikzscale}
\usepackage{filecontents}
\begin{filecontents}{notikzscale.tikz}
\begin{tikzpicture}
\begin{groupplot}[
group style={
group size=2 by 1,
horizontal sep=2cm,
},
scale only axis,
width=0.38*\textwidth,
height=3cm,
xlabel=$x$,
]
\nextgroupplot[ylabel={This}]
\addplot coordinates { (-0.9,-0.9) (0.9,0.9) };
\node[anchor=west] (a) at (axis cs:-1,0) {01};
\nextgroupplot[ylabel={That}]
\addplot coordinates { (-0.9,-0.9) (0.9,0.9) };
\end{groupplot}
\node[anchor=west,red] at (a) {0.2};
\end{tikzpicture}
\end{filecontents}
\begin{filecontents}{2by1.tikz}
\begin{tikzpicture}
\begin{groupplot}[
group style={
group size=2 by 1,
horizontal sep=2cm,
},
width=1/2*\textwidth-2cm,
height=3cm,
xlabel=$x$,
]
\nextgroupplot[ylabel={This}]
\addplot coordinates { (-0.9,-0.9) (0.9,0.9) };
\node[anchor=west] (a) at (axis cs:-1,0) {01};
\nextgroupplot[ylabel={That}]
\addplot coordinates { (-0.9,-0.9) (0.9,0.9) };
\end{groupplot}
\node[anchor=west,red] at (a) {0.2};
\end{tikzpicture}
\end{filecontents}
\begin{filecontents}{3by1.tikz}
\begin{tikzpicture}
\begin{groupplot}[
group style={
group size=3 by 1,
horizontal sep=1cm,
},
scale only axis,
width=1/3*\textwidth-1cm,
height=3cm,
xlabel=$x$,
]
\nextgroupplot[ylabel={This}]
\addplot coordinates { (-0.9,-0.9) (0.9,0.9) };
\node[anchor=west] (a) at (axis cs:-1,0) {01};
\nextgroupplot
\addplot coordinates { (-0.9,-0.9) (0.9,0.9) };
\nextgroupplot
\addplot coordinates { (-0.9,-0.9) (0.9,0.9) };
\end{groupplot}
\node[anchor=west,red] at (a) {0.2};
\end{tikzpicture}
\end{filecontents}
\begin{filecontents}{4by1.tikz}
\begin{tikzpicture}
\begin{groupplot}[
group style={
group size=4 by 1,
horizontal sep=1cm,
},
width=1/4*\textwidth-1cm,
height=3cm,
xlabel=$x$,
]
\nextgroupplot[ylabel={This}]
\addplot coordinates { (-0.9,-0.9) (0.9,0.9) };
\node[anchor=west] (a) at (axis cs:-1,0) {01};
\nextgroupplot
\addplot coordinates { (-0.9,-0.9) (0.9,0.9) };
\nextgroupplot
\addplot coordinates { (-0.9,-0.9) (0.9,0.9) };
\nextgroupplot
\addplot coordinates { (-0.9,-0.9) (0.9,0.9) };
\end{groupplot}
\node[anchor=west,red] at (a) {0.2};
\end{tikzpicture}
\end{filecontents}
\begin{document}
This does not use \texttt{tikzscale}. Manually-sized.
\begin{center}
\input{notikzscale.tikz}
\end{center}
\vspace{0.5cm}
The below use \texttt{tikzscale}.
\centering
\includegraphics[width=\textwidth]{2by1.tikz}
\vspace{0.5cm}
\includegraphics[width=\textwidth]{3by1.tikz}
\vspace{0.5cm}
\includegraphics[width=\textwidth]{4by1.tikz}
\end{document}