使用/不使用 Gnuplot 时 Pgfplot 标签位置一致

使用/不使用 Gnuplot 时 Pgfplot 标签位置一致

我喜欢将节点放置在图的末尾,并且该节点相对于图上的最后一个点出现。当在中指定域时增加顺序\DomainMin:\DomainMax,标签会准确地放置在我想要的位置 - 无论有无gnuplot(参见图 1 和图 2):

在此处输入图片描述

但是,当在减少order DomainMax:\DomainMin,如果我使用 PGF 计算坐标,我会得到我想要的标签(见图 3),但当我使用时,它却在错误的位置gnuplot(见图 4)。我怀疑这是因为gnuplot忽略了指定域的开始和结束的顺序,并按 x 的递增顺序计算坐标。

在此处输入图片描述

我试图pos=0认为它会将标签放在我想要的位置,但事实并非如此——它看起来相当低,但不知道为什么。

在此处输入图片描述

那么,有没有办法可以将gnuplots标签放置在pgfplots与我按降序指定域时放置标签相同的位置?

以下是生成上述 5 个图表的代码:

\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}

\newcommand*{\XAxisMin}{-1.0}
\newcommand*{\XAxisMax}{3.0}
\newcommand*{\YAxisMin}{-2.0}
\newcommand*{\YAxisMax}{10}

\newcommand*{\DomainMinF}{\XAxisMin}
\newcommand*{\DomainMaxF}{2.2}
\newcommand*{\PlotLabel}{$y=x^3$}%


\pgfkeys{/pgfplots/Axis Style/.style={
    clip=false,% so that we can see labels placed outside
    xmin=\XAxisMin, xmax=\XAxisMax,
    ymin=\YAxisMin, ymax=\YAxisMax, 
    width=6.5cm
}}

% Gnuplot options here  have no effect if not using GnuPlot
\pgfkeys{/pgfplots/Plot Style/.style={
    translate gnuplot=true,% can use ‘^’ instead of ‘**’
    id=foo,
    mark=none,%
    domain=\DomainMinF:\DomainMaxF,%
    samples=50,%
    ultra thick,
}}

\newcommand*{\AddLabel}[1]{\node [align = center] at (axis cs: 0.4,5) {#1};}%

\begin{document}
With increasing domain=$\DomainMinF:\DomainMaxF$, we get label at the end of the graph as desired:

\begin{tikzpicture}
\begin{axis}[Axis Style]
    \addplot [Plot Style, blue, domain=\DomainMinF:\DomainMaxF]
        {(x)^3} node [right] {\PlotLabel};%
    \AddLabel{1. without \\ Gnuplot}
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}
\begin{axis}[Axis Style]
    \addplot [Plot Style, red, domain=\DomainMinF:\DomainMaxF]
        gnuplot% Use Gnuplot for Graph 2
        {(x)^3} node [right] {\PlotLabel};%
    \AddLabel{2. with \\ Gnuplot}
\end{axis}
\end{tikzpicture}

\bigskip
But, with decreasing domain=$\DomainMaxF:\DomainMinF$, we get the label in the wrong spot with Gnuplot:

\begin{tikzpicture}
\begin{axis}[Axis Style]
    \addplot [Plot Style, blue, domain=\DomainMaxF:\DomainMinF]
        {(x)^3} node [right] {\PlotLabel};%
    \AddLabel{3. without \\ Gnuplot}
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}
\begin{axis}[Axis Style]
    \addplot [Plot Style, red, domain=\DomainMaxF:\DomainMinF]
        gnuplot% Use Gnuplot for Graph 4
        {(x)^3} node [right] {\PlotLabel};% adding pos=0.0 does not work
    \AddLabel{4. with \\ Gnuplot}
\end{axis}
\end{tikzpicture}

\begin{tikzpicture}
\begin{axis}[Axis Style]
    \addplot [Plot Style, red, domain=\DomainMaxF:\DomainMinF]
        gnuplot% Use Gnuplot for Graph 5
        {(x)^3} node [right, pos=0.0] {\PlotLabel};
    \AddLabel{5. with \\ Gnuplot (pos=0.0)}
\end{axis}
\end{tikzpicture}
\end{document}

答案1

最简单的做法可能是升级到 PGFplots 的当前版本 (>=1.5.1),它允许使用 将节点放置在图上[pos=...]

或者,你可以使用plotlabel我在回答中使用的样式在 pgfplots 中标记图,无需手动输入坐标。它允许您沿路径以指定距离放置节点。语法是plotlabel{<pos>}{<label text>}。这使用 markings装饰,pgfnode而不是 TikZ node,后者不太灵活。

第三,这是linelabel我在回答在 pgfplots 中标记图,无需手动输入坐标。其语法为linelabel={<relative horizontal position along axis>}{<label code>},其中<label code>将传递给label(参见第 194 页的手册),因此你可以使用类似如下的方法[node options]<placement>:<label text>

\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\usetikzlibrary{intersections}
\usetikzlibrary{decorations.markings}
\makeatletter
\tikzset{
    nomorepostaction/.code={\let\tikz@postactions\pgfutil@empty},
    plotlabel/.style 2 args={
        every path/.append style={
            postaction={
                nomorepostaction,
                decorate,
                decoration={
                    markings,
                    mark=at position #1 with {
                        \pgftransformresetnontranslations
                        \pgfnode{rectangle}{west}{#2}{}{}
                    }
                }
            }
        }
    }
}
\makeatother


\newcommand*{\XAxisMin}{-1.0}
\newcommand*{\XAxisMax}{3.0}
\newcommand*{\YAxisMin}{-3.0}
\newcommand*{\YAxisMax}{10}

\newcommand*{\DomainMinF}{\XAxisMin}
\newcommand*{\DomainMaxF}{2.2}
\newcommand*{\PlotLabel}{$y=x^3$}%


\pgfkeys{/pgfplots/Axis Style/.style={
    clip=false,% so that we can see labels placed outside
    xmin=\XAxisMin, xmax=\XAxisMax,
    ymin=\YAxisMin, ymax=\YAxisMax, 
    width=6.5cm
}}

% Gnuplot options here  have no effect if not using GnuPlot
\pgfkeys{/pgfplots/Plot Style/.style={
    translate gnuplot=true,% can use ‘^’ instead of ‘**’
    id=foo,
    mark=none,%
    domain=\DomainMinF:\DomainMaxF,%
    samples=50,%
    ultra thick,
}}

\pgfkeys{/pgfplots/linelabel/.style 2 args={name path global=labelpath,execute at end plot={
\path [name path global = labelpositionline]
(rel axis cs:#1,0) --
(rel axis cs:#1,1);
\draw [name intersections={of=labelpath and labelpositionline}] (intersection-1)  node [label={#2}] {};},
}}

\newcommand*{\AddLabel}[1]{\node [align = center] at (axis cs: 0.4,5) {#1};}%


\begin{document}
\begin{tikzpicture}
\begin{axis}[Axis Style]
    \addplot [Plot Style, blue, domain=\DomainMinF:\DomainMaxF,plotlabel={0}{\PlotLabel}]
        {(x)^3};
\AddLabel{1. without \\ Gnuplot}
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}
\begin{axis}[Axis Style]
    \addplot [Plot Style, red, domain=\DomainMinF:\DomainMaxF,plotlabel={0}{\PlotLabel}]
        gnuplot {(x)^3};
\AddLabel{2. with \\ Gnuplot}
\end{axis}
\end{tikzpicture}

\begin{tikzpicture}
\begin{axis}[Axis Style]
    \addplot [Plot Style, blue, domain=\DomainMinF:\DomainMaxF,linelabel={0}{[blue]right:\PlotLabel}]
        {(x)^3};
\AddLabel{1. without \\ Gnuplot}
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}
\begin{axis}[Axis Style]
    \addplot [Plot Style, red, domain=\DomainMinF:\DomainMaxF,linelabel={0}{[red]right:\PlotLabel}]
        gnuplot {(x)^3};
\AddLabel{2. with \\ Gnuplot}
\end{axis}
\end{tikzpicture}
\end{document}

相关内容