在 pgfplots 中填充更改图例字体

在 pgfplots 中填充更改图例字体

我正在处理一个文档,其中有几张图并排放置。其中一些使用“填充”,而另一些则不使用。现在的问题是,根据是否使用此功能,图例的字体大小会有所不同。此外,图的厚度也会受到影响。作为一种解决方法,我现在在每个图中都使用一个空的“填充”(第三个 tikzpicture),但这感觉太像作弊了,我很想知道这里到底发生了什么。

以下是 MWE:

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.13}
\usepgfplotslibrary{fillbetween}

\begin{document}

\begin{tikzpicture}[scale=0.55]
\begin{axis}[
    ticks=none, axis x line* = bottom, 
    samples= 160, domain = -3:3,    
    xmin = -3, xmax = 3,    
    ymin = 0, ymax = 3,
    legend style={at={(axis cs:2.9,2.9)},anchor=north east}]
    \addplot[name path=f, black, thick, mark=none, ] {max(2.8-2*x^2,0)};
    \addplot[name path=g, black, thick, mark=none, dashed, ] {max(1-0.3*x^2,0)};
    \addplot[name path=line, gray, no markers, line width=0pt] {0};
    \addplot[color=gray, fill opacity=0.25] fill between[of = f and line, split];
    \legend{$f$,$q$}
\end{axis}
\end{tikzpicture}\hspace{5em}
\begin{tikzpicture}[scale=0.55]
\begin{axis}[
    ticks=none, axis x line* = bottom, 
    samples= 160, domain = -3:3,    
    xmin = -3, xmax = 3,    
    ymin = 0, ymax = 3,
    legend style={at={(axis cs:2.9,2.9)},anchor=north east}]
    \addplot[name path=f, black, thick, mark=none, ] {max(2.8-2*x^2,0)};
    \addplot[name path=g, black, thick, mark=none, dashed, ] {max(1-0.3*x^2,0)};
    \addplot[name path=line, gray, no markers, line width=0pt] {0};
    \legend{$f$,$q$}
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}[scale=0.55]
\begin{axis}[
    ticks=none, axis x line* = bottom, 
    samples= 160, domain = -3:3,    
    xmin = -3, xmax = 3,    
    ymin = 0, ymax = 3,
    legend style={at={(axis cs:2.9,2.9)},anchor=north east}]
    \addplot[name path=f, black, thick, mark=none, ] {max(2.8-2*x^2,0)};
    \addplot[name path=g, black, thick, mark=none, dashed, ] {max(1-0.3*x^2,0)};
    \addplot[name path=line, gray, no markers, line width=0pt] {0};
    \addplot[color=gray, fill opacity=0.25] fill between[of = line and line, split];
    \legend{$f$,$q$}
\end{axis}
\end{tikzpicture}

\end{document}

编辑:感谢 Zarko 的回答,我现在认为这个问题是pgfplots 中 fillbetween 的缩放问题。出于某种原因,调用 fillbetween 会导致缩放突然影响图例。但我还没弄清楚为什么。

答案1

观察到的问题的原因是 TikZ 图片的缩放。如果省略,图例中的字体将不会改变。这让我想到fillbetween库的使用不允许缩放,即使缩放按预期工作:它不应该改变字体大小(据我了解它的工作原理,也许我错了)。

我在你的 MWE 中做了一些改变:省略缩放,引入图像宽度,并且它可以按预期工作:

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.13}
\usepgfplotslibrary{fillbetween}

\usepackage{showframe}

\begin{document}
\noindent%
    \begin{tikzpicture}
\begin{axis}[
    width=0.45\textwidth,
    ticks=none, axis x line* = bottom,
    samples= 160, domain = -3:3,
    xmin = -3, xmax = 3,
    ymin = 0, ymax = 3,
    legend style={at={(0.97,0.99)}, anchor=north east},
                    ]
    \addplot[name path=f, black, thick, mark=none, ] {max(2.8-2*x^2,0)};
    \addplot[name path=g, black, thick, mark=none, dashed, ] {max(1-0.3*x^2,0)};
    \addplot[name path=line, gray, no markers, line width=0pt] {0};
    \addplot[color=gray, fill opacity=0.25] fill between[of = f and line, split];
    \legend{$f$,$q$}
\end{axis}
\end{tikzpicture}\hfill
\begin{tikzpicture}
\begin{axis}[
    width=0.45\textwidth,
    ticks=none, axis x line* = bottom,
    samples= 160, domain = -3:3,
    xmin = -3, xmax = 3,
    ymin = 0, ymax = 3,
    legend style={at={(0.97,0.99)}, anchor=north east},
                    ]
    \addplot[name path=f, black, thick, mark=none, ] {max(2.8-2*x^2,0)};
    \addplot[name path=g, black, thick, mark=none, dashed, ] {max(1-0.3*x^2,0)};
    \addplot[name path=line, gray, no markers, line width=0pt] {0};
    \legend{$f$,$q$}
\end{axis}
\end{tikzpicture}\hfill
\begin{tikzpicture}
\begin{axis}[
    width=0.45\textwidth,
    ticks=none, axis x line* = bottom,
    samples= 160, domain = -3:3,
    xmin = -3, xmax = 3,
    ymin = 0, ymax = 3,
    legend style={at={(0.97,0.99)}, anchor=north east},
                    ]
    \addplot[name path=f, black, thick, mark=none, ] {max(2.8-2*x^2,0)};
    \addplot[name path=g, black, thick, mark=none, dashed, ] {max(1-0.3*x^2,0)};
    \addplot[name path=line, gray, no markers, line width=0pt] {0};
    \addplot[color=gray, fill opacity=0.25] fill between[of = f and line, split];
    \legend{$f$,$q$}
\end{axis}
    \end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容