更新 1

更新 1

我想要用来pgfplots绘制groupplot谁的宽度(8 厘米)、字体系列(Arial)和字体大小(轴标签为 10pt,刻度标签为 8pt)是特定的。

我尝试过tikzscale让图形的尺寸正确。对于我使用的字体fontspec,虽然我可以正确设置图形尺寸和字体系列,但字体大小总是有点偏差(10pt 和 8pt 分别为 9.79586506pt 和 7.83669019pt)。

主要.tex文件:

\documentclass[10pt]{standalone} % set the normalsize to 10 pt
\usepackage{pgfplots}
\pgfplotsset{compat=newest} 
\usepgfplotslibrary{groupplots}
\usepackage{tikzscale}

\usepackage{fontspec}
\setmainfont{Arial} % set the font family to Arial

\pgfplotsset{
             every axis label = {font=\normalsize}, % set the axis label font to 10pt
             tick label style={/pgf/number format/assume math mode=true, font=\footnotesize} % set the tick label font to 8pt
}

\begin{document}

\includegraphics[width = 8cm]{a.tikz}

\end{document}

文件.tikz

\begin{tikzpicture}
    \begin{groupplot}[
        group style={group size=2 by 2,
            horizontal sep = 1 cm, 
            vertical sep = 1 cm}, 
        xlabel = X label,
        ylabel = Y label,
        width = 3 cm, % this is supposed to be total width (8cm) divided by number of columns (2) minus the sum of all the horizontal separations (1cm), i.e. 3 cm
        height = 3 cm, % same as above, but for the vertical
        ]
        \nextgroupplot%[xticklabels = {1,}]
        \addplot coordinates {(0,0) (1,1) (2,2)};
        \nextgroupplot
        \addplot coordinates {(0,2) (1,1) (2,0)};
        \nextgroupplot
        \addplot coordinates {(0,2) (1,1) (2,1)};
        \nextgroupplot
        \addplot coordinates {(0,2) (1,1) (1,0)};      
    \end{groupplot}
\end{tikzpicture}

结果

注1:我怀疑问题的一部分在于groupplot。我跟着这些说明。但我认为我们需要考虑最左边轴左侧的内容——即 y 刻度标签和 y 标签。也许这样结果就正确了?

笔记2:实际上,两种字体大小相差的倍数相同。因此,8pt 和 10pt 实际上是a*8pt 和a*10pt,其中a是某个数字。我设法通过使用 1.0245 的倍数来调整结果:

\setmainfont[Scale = 1.0245]{Arial}
\defaultfontfeatures{ Scale = MatchUppercase }

虽然这种方法“有效”,但它非常繁琐,而且只适用于这个特定的例子。如果我改变图形或其大小,这种方法就会失效。

更新 1

按照 Stefan Pinnow 的解决方案,当 x 轴的刻度标签突出到最右侧窗格框的最右侧之外时,就会出现额外的复杂性。当坐标包含大量数字时,很容易发生这种情况。例如:

\documentclass[10pt]{standalone}
\usepackage{fontspec}
    \setmainfont{Arial} % set the font family to Arial
\usepackage{pgfplots}
    \usepgfplotslibrary{groupplots}
    \pgfplotsset{
        % use this `compat` level or higher to get this result
        compat=1.3,
        every axis label={font=\normalsize}, % set the axis label font to 10pt
        tick label style={
            /pgf/number format/assume math mode=true,
            font=\footnotesize,     % set the tick label font to 8pt
        },
    }
\begin{document}
\begin{tikzpicture}
        % define a length to set the (y) label distance
        \pgfmathsetlengthmacro{\LabelDistance}{30pt}
    \begin{groupplot}[
        group style={
            group size=2 by 2,
            horizontal sep=1cm,
            vertical sep=1cm,
        },
        % added so the ylabels are aligned properly and to define a distance
        % to the axis line, which can be used to calculate the `width`
        every axis y label/.style={
            at={(ticklabel* cs:0.5,\LabelDistance)},
            rotate=90,
            anchor=near ticklabel opposite,
        },
        % use `width` to the axis only, i.e. without any labels
        scale only axis,
        % calculate the with of the single plots depending on the known/defined
        % values for the total width of the groupplot, the distance of the
        % ylabels (`\LabelDistance`) and `horizontal sep` of the plots
        width={(8cm - \LabelDistance - \pgfkeysvalueof{/pgfplots/group/horizontal sep})/2},
        % give the same value as the `width` (not sure is this is required)
        height={\pgfkeysvalueof{/pgfplots/width}},
        xlabel=X label,
        ylabel=Y label,
    ]
    \nextgroupplot
        \addplot coordinates {(0,0) (1,1) (2,2)};
    \nextgroupplot
        \addplot coordinates {(0,2) (1,1) (2,1)};
    \nextgroupplot
        \addplot coordinates {(0,2) (1,1) (2,1)};
    \nextgroupplot
        \addplot coordinates {(0,2) (1,1) (1000,0)};
    \end{groupplot}
\end{tikzpicture}
\end{document}

这样一来,pdf 文件就变得更宽了,宽度为 8.214 厘米(而不是 8 厘米):

更宽的 pdf

答案1

新版本:

新1: 您需要测量标签的距离。

然后你可以计算出正确的\TotalWidth

% My Values =========================
\pgfmathsetlengthmacro\horizontalsep{15pt}
\pgfmathsetlengthmacro\verticalsep{30pt}
\pgfmathsetlengthmacro\mywidth{200pt}
\pgfmathsetlengthmacro\myheight{120pt}

在此处输入图片描述

新2:测量方法现在是一种样式
Measure/.style={...},
您可以任意添加它。
\nextgroupplot[...., Measure] % should be the last key
您应该使用\nextgroupplot具有最大刻度和标签的样式。

如果不设置 a ylabel(或xlabel),则会出现无法找到棕色 (黄色) 点的坐标的问题。因此,如果未设置标签,我们应该设置一个默认值
xlabel={\phantom{.}}, ylabel={\phantom{.}}
,这将产生约 2pt 的间隙 (由于)。inner sep

在此处输入图片描述

\def\ShowHelps{0} % 1 "yes" 0 "no"

在此处输入图片描述

\documentclass[a4paper]{article}
\usepackage[margin=1cm, showframe=false]{geometry}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepgfplotslibrary{groupplots}
\pgfkeys{/tikz/savevalue/.code 2 args={\global\edef#1{#2}}}
%\usetikzlibrary{backgrounds}
\pgfdeclarelayer{background}
\pgfdeclarelayer{foreground}
\pgfsetlayers{background,main,foreground}

% My Values =========================
\pgfmathsetlengthmacro\horizontalsep{15pt}
\pgfmathsetlengthmacro\verticalsep{30pt}
\pgfmathsetlengthmacro\mywidth{200pt}
\pgfmathsetlengthmacro\myheight{120pt}

\def\ShowHelps{1}  % 1 "yes"    0 "no"

\renewcommand*\familydefault{\sfdefault}  
% ===============================

\ifnum\ShowHelps=1 
\def\ptsize{1.5pt}
\tikzset{Help/.style={draw=#1,  
nodes={scale=0.6, fill=yellow, draw=#1, text=black, font=\tiny, midway, below, align=left, inner sep=0.5pt} },
Help/.default=red, 
HelpDraw/.style={draw},
}
\else
\def\ptsize{0pt}
\tikzset{Help/.style={opacity=0, overlay},
HelpDraw/.style={},
}\fi

\pgfplotsset{
NeededStylesForMeasure/.style={
xlabel={\phantom{.}},  ylabel={\phantom{.}}, % "default"
every axis label/.append style={
inner sep=1pt % abs. value, regular default = 0.333em
},     },
}

\pgfplotsset{
Measure/.style={%%%
every axis label/.append style={HelpDraw},
every tick label/.append style={HelpDraw},
before end axis/.code={
\fill[red] (yticklabel* cs:0.5) coordinate (X2) circle(\ptsize); 
\fill[orange] (xticklabel* cs:0.5) coordinate (Y2) circle(\ptsize); 
}, 
every axis y label/.append style={  
path picture={
\begin{pgfonlayer}{foreground}
\fill[brown] (path picture bounding box.west) coordinate(X1) circle(\ptsize);
\end{pgfonlayer}    
\path let \p1=($(X2)-(X1)$) in 
\pgfextra{  \pgfmathsetlengthmacro{\ylabeldistance}{veclen(\x1,\y1)}   }
[savevalue={\yLabelDistance}{\ylabeldistance}];          
},
},
every axis x label/.append style={  
path picture={
\begin{pgfonlayer}{foreground}
\fill[yellow!88!red] (path picture bounding box.south) coordinate(Y1) circle(\ptsize);
\end{pgfonlayer}    
\path let \p1=($(Y2)-(Y1)$) in 
\pgfextra{  \pgfmathsetlengthmacro{\xlabeldistance}{veclen(\x1,\y1)}   }
[savevalue={\xLabelDistance}{\xlabeldistance}];          
},
},
},%%%
}

% For wrong picture
\pgfmathsetlengthmacro\HorizontalSep{\horizontalsep}
\pgfmathsetlengthmacro\VerticalSep{\verticalsep}

\pgfmathsetlengthmacro\AxisWidth{\mywidth}
\pgfmathsetlengthmacro\AxisHeight{\myheight}

\newcommand\mygroupplot{%%%%%%%%%%%%%%%%
\noindent\begin{tikzpicture}
\begin{groupplot}[group style={
group size={2 by 2},
horizontal sep=\HorizontalSep, vertical sep=\VerticalSep,
}, 
scale only axis, %1/2
width=\AxisWidth, height=\AxisHeight, %2/2
NeededStylesForMeasure,
clip=false, 
]
\nextgroupplot[title={1}, ylabel=yLabel1, xlabel=x, 
y label style={font=\Huge,},  % Test
%ylabel shift=5pt, % Test 2
Measure
]
\addplot coordinates {(0,-500) (1,1) (2,2)};

\nextgroupplot[title={2}, ylabel=yLabel2, xlabel=x]
\addplot coordinates {(0,2) (1,1) (2,1)};
\nextgroupplot[title={3}]
\addplot coordinates {(0,2) (1,1) (1,0)};
\nextgroupplot[title={4}]
\addplot[]{3*x};
\end{groupplot}

% ==========================================
% The Helping Stuff ===============================
% x- and  y Label Distances
\draw[Help] (X1) -- (X2) node[Help]{\yLabelDistance \\ yLabelDistance};   
\draw[Help] (Y1) -- (Y2) node[Help, right]{\xLabelDistance \\ xLabelDistance};     

% Width
\pgfmathsetlengthmacro{\width}{\AxisWidth+\yLabelDistance}
\node[savevalue={\Width}{\width}]{};
\draw[Help]  (group c1r1.north east) -- +(-\Width,0) node[Help]{\Width \\ Width};
\draw[Help]  (group c1r1.south east) -- +(-\AxisWidth,0) node[Help, above]{\AxisWidth \\ AxisWidth};

% Height
\pgfmathsetlengthmacro{\height}{\AxisHeight+\xLabelDistance}
\node[savevalue={\Height}{\height}]{};
\draw[Help=blue, |-, transform canvas={yshift=-\xLabelDistance},
]  (group c1r1.south west) -- +(0,\Height) node[Help, right]{\Height \\ Height};
\draw[Help]  (group c1r1.south east) -- +(0,\AxisHeight) node[Help, left]{\AxisHeight \\ AxisHeight};

% groupplot boxes
\foreach \r in {1,2}{\foreach \c in {1,2}{
\draw[Help=pink, dashed]  ([xshift=-\yLabelDistance, yshift=-\xLabelDistance]group  c\c r\r.south west) rectangle +(\Width, \Height);
\draw[Help=green, dashed] (group c\c r\r.south east) rectangle +(-\AxisWidth,-\xLabelDistance);
}}

% complete plot box 
\draw[Help=purple, dashed]  ([xshift=-\yLabelDistance, yshift=-\xLabelDistance]group c1r2.south west) rectangle +(2*\Width+\horizontalsep, 2*\Height+\verticalsep);

% Horizontal Sep
\draw[Help, purple]  (group c1r1.south east) -- +(\HorizontalSep,0) node[Help, below, near end]{\HorizontalSep \\ HorizontalSep};
\draw[Help, green, thin]  ([yshift=4mm]group c1r1.south east)  --  +(\horizontalsep,0) node[Help, draw=green, above=3pt,near end]{\horizontalsep \\ horizontalsep};

% Vertical Sep
\draw[Help=purple]  (group c1r1.south east) -- +(0,-\VerticalSep) node[Help, right]{\VerticalSep \\ VerticalSep};
\draw[Help, green, thin]  ([yshift=-\xLabelDistance, xshift=-4mm]group c1r1.south east)  --  +(0,-\verticalsep) node[Help, draw=green, left=3pt]{\verticalsep \\ verticalsep};

% Total Width
\pgfmathsetlengthmacro{\TotalWidth}{2*\Width+\horizontalsep}
\draw[Help=purple, thick]  ([xshift=-\yLabelDistance, yshift=-\xLabelDistance]group c1r2.south west) -- +(\TotalWidth, 0) node[Help=purple, above, scale=2]{ 
TotalWidth (\TotalWidth) = 2$\times$Width (2$\times$\Width) + horizontalsep (\horizontalsep)
};

% Total Height
\pgfmathsetlengthmacro{\TotalHeight}{2*\Height+1*\verticalsep}
\draw[Help=blue, very thick]  ([yshift=-\xLabelDistance]group c2r2.south east) -- +(0,\TotalHeight) node[Help=blue, above, rotate=90, scale=2]{
TotalHeight (\TotalHeight) =  2$\times$Height (2$\times$\Height) + horizontalsep (\verticalsep)
};
% ==========================================
% ==========================================
\end{tikzpicture}
}%%%%%%%%%%%%%%%%
\begin{document}
\section{Wrong}
\mygroupplot % wrong

\newsavebox{\mybox}
\sbox{\mybox}{\mygroupplot}
%\noindent\usebox{\mybox} % still wrong

\section{Correct}
\pgfmathsetlengthmacro\AxisWidth{\mywidth-\yLabelDistance}
\pgfmathsetlengthmacro\AxisHeight{\myheight-\xLabelDistance}
\pgfmathsetlengthmacro\HorizontalSep{\horizontalsep+\yLabelDistance}
\pgfmathsetlengthmacro\VerticalSep{\verticalsep+\xLabelDistance}
\sbox{\mybox}{\mygroupplot}
\noindent\usebox{\mybox} %now correct
\end{document}

答案2

我不建议使用该tikzscale包。这里有一种实现你想要的方法。有关详细信息,请查看代码中的注释。

% used PGFPlots v1.16
\documentclass[border=5pt,10pt]{standalone}
\usepackage{fontspec}
    \setmainfont{Arial} % set the font family to Arial
\usepackage{pgfplots}
    \usepgfplotslibrary{groupplots}
    \pgfplotsset{
        % use this `compat` level or higher to get this result
        compat=1.3,
        every axis label={font=\normalsize}, % set the axis label font to 10pt
        tick label style={
            /pgf/number format/assume math mode=true,
            font=\footnotesize,     % set the tick label font to 8pt
        },
    }
\begin{document}
\begin{tikzpicture}
        % define a length to set the (y) label distance
        \pgfmathsetlengthmacro{\LabelDistance}{30pt}
    \begin{groupplot}[
        group style={
            group size=2 by 2,
            horizontal sep=1cm,
            vertical sep=1cm,
        },
        % added so the ylabels are aligned properly and to define a distance
        % to the axis line, which can be used to calculate the `width`
        every axis y label/.style={
            at={(ticklabel* cs:0.5,\LabelDistance)},
            rotate=90,
            anchor=near ticklabel opposite,
        },
        % use `width` to the axis only, i.e. without any labels
        scale only axis,
        % calculate the with of the single plots depending on the known/defined
        % values for the total width of the groupplot, the distance of the
        % ylabels (`\LabelDistance`) and `horizontal sep` of the plots
        width={(8cm - \LabelDistance - \pgfkeysvalueof{/pgfplots/group/horizontal sep})/2},
        % give the same value as the `width` (not sure is this is required)
        height={\pgfkeysvalueof{/pgfplots/width}},
        xlabel=X label,
        ylabel=Y label,
    ]
    \nextgroupplot
        \addplot coordinates {(0,0) (1,1) (2,2)};
    \nextgroupplot
        \addplot coordinates {(0,2) (1,1) (2,1)};
    \nextgroupplot
        \addplot coordinates {(0,2) (1,1) (2,1)};
    \nextgroupplot
        \addplot coordinates {(0,2) (1,1) (1,0)};
    \end{groupplot}

    % -------------------------------------------------------------------------
    % for debugging purposes only
    % -------------------------------------------------------------------------
    \draw [red] (group c1r2.south east) -- +(1cm,0);

    \draw [blue,thick]  (current bounding box.south west |- 0,1) --
                       +(\LabelDistance,0pt);

    \draw [green]       (current bounding box.south west) rectangle
                        (current bounding box.north east);
    \draw [red,dashed]  (current bounding box.south west) rectangle +(8,8);
    % -------------------------------------------------------------------------
\end{tikzpicture}
\end{document}

该图显示了上述代码的结果

相关内容