解釋

解釋

所以,之後@Symbol1 的出色解決方案對於我的最後一個問題,我有一個關於同一主題的新問題。使用提議的代碼,它改變了 的行為\pgfplotsplothandlerquiver@vis@pathpoint meta似乎不再起作用了...

mapped color這對和都有影響\pgfplotspointmetatransformed

\documentclass[border=9,tikz]{standalone} 
\usepackage{pgfplots}\pgfplotsset{compat=newest}
\usetikzlibrary{arrows.meta}
\begin{document}

\makeatletter
\def\pgfplotsplothandlerquiver@vis@path#1{%
    % remember (x,y) in a robust way
    #1%
    \pgfmathsetmacro\pgfplots@quiver@x{\pgf@x}%
    \pgfmathsetmacro\pgfplots@quiver@y{\pgf@y}%
    % calculate (u,v) in relative coordinate
    \pgfplotsaxisvisphasetransformcoordinate\pgfplots@quiver@u\pgfplots@quiver@v\pgfplots@quiver@w%
    \pgfplotsqpointxy{\pgfplots@quiver@u}{\pgfplots@quiver@v}%
    \pgfmathsetmacro\pgfplots@quiver@u{\pgf@x-\pgfplots@quiver@x}%
    \pgfmathsetmacro\pgfplots@quiver@v{\pgf@y-\pgfplots@quiver@y}%
    % move to (x,y) and start drawing
    {%
        \pgftransformshift{\pgfpoint{\pgfplots@quiver@x}{\pgfplots@quiver@y}}%
        \pgfpathmoveto{\pgfpointorigin}%
        \pgfpathlineto{\pgfpoint\pgfplots@quiver@u\pgfplots@quiver@v}%
    }%
}%

  \begin{tikzpicture}
    \begin{axis}[axis equal]

      \addplot[
               point meta=x,
               quiver={u=x,v=y,
                 after arrow/.code={
                   \relax{% always protect the shift
                     \pgftransformshift{\pgfpoint{\pgfplots@quiver@x}{\pgfplots@quiver@y}}%
                     \node[below right]{\tiny\color{mapped color}\pgfplotspointmetatransformed};
                   }
                 }
               },
               ->,
               samples=10,domain=-1:1
              ] {x*x-1};


      \addplot[
               point meta=x,
               quiver={u=x,v=y,
                      every arrow/.append style={%
                        -{Latex[scale length={max(0.1,\pgfplotspointmetatransformed/1000)}]},mapped color
                      },
               },
               ->,
               samples=10,domain=-1:1
              ] {x*x};
   \end{axis}
  \end{tikzpicture}
\end{document}

對於未更改的版本,縮放和著色有效,而對於更改的版本,這兩件事都不起作用。

在此輸入影像描述

為了完成,原始pgfplots程式碼被替換(我在這裡看不到任何有關元值的資訊?!?)

\def\pgfplotsplothandlerquiver@vis@path#1{%
    \pgfpathmoveto{#1}%
    \pgfplotsaxisvisphasetransformcoordinate\pgfplots@quiver@u\pgfplots@quiver@v\pgfplots@quiver@w
    \pgfpathlineto{%
        \pgfplotsifcurplotthreedim{%
            \pgfplotsqpointxyz\pgfplots@quiver@u\pgfplots@quiver@v\pgfplots@quiver@w
        }{%
            \pgfplotsqpointxy\pgfplots@quiver@u\pgfplots@quiver@v
        }%
    }%
}%

答案1

以下內容對我有用。

\documentclass[border=9,tikz]{standalone} 
\usepackage{pgfplots}\pgfplotsset{compat=newest}
\usetikzlibrary{arrows.meta}
\begin{document}

\makeatletter
\def\pgfplotsplothandlerquiver@vis@path#1{%
    % remember (x,y) in a robust way
    #1%
    \pgfmathsetmacro\pgfplots@quiver@x{\pgf@x}\global\let\pgfplots@quiver@x\pgfplots@quiver@x%
    \pgfmathsetmacro\pgfplots@quiver@y{\pgf@y}\global\let\pgfplots@quiver@y\pgfplots@quiver@y%
    % calculate (u,v) in relative coordinate
    \pgfplotsaxisvisphasetransformcoordinate\pgfplots@quiver@u\pgfplots@quiver@v\pgfplots@quiver@w%
    \pgfplotsqpointxy{\pgfplots@quiver@u}{\pgfplots@quiver@v}%
    \pgfmathsetmacro\pgfplots@quiver@u{\pgf@x-\pgfplots@quiver@x}%
    \pgfmathsetmacro\pgfplots@quiver@v{\pgf@y-\pgfplots@quiver@y}%
    % move to (x,y) and start drawing
    {%
        \pgftransformshift{\pgfpoint{\pgfplots@quiver@x}{\pgfplots@quiver@y}}%
        \pgfpathmoveto{\pgfpointorigin}%
        \pgfpathlineto{\pgfpoint\pgfplots@quiver@u\pgfplots@quiver@v}%
    }%
}%

    \begin{tikzpicture}
        \begin{axis}[axis equal]
            \addplot[
                point meta=x,
                quiver={
                    u=x,v=y,
                    every arrow/.append style={%
                        -{Latex[scale length={max(0.1,\pgfplotspointmetatransformed/1000)}]},mapped color
                    },
                    after arrow/.code={
                        \relax{% always protect the shift
                            \pgftransformshift{\pgfpoint{\pgfplots@quiver@x}{\pgfplots@quiver@y}}%
                            \node[below right]{\tiny\color{mapped color}\pgfplotspointmetatransformed};
                        }
                    }
                },
                ->,samples=10,domain=-1:1
            ]{x*x-1};
            \addplot[
                point meta=x,
                quiver={
                    u=x,v=y,
                    every arrow/.append style={%
                        -{Latex[scale length={max(0.1,\pgfplotspointmetatransformed/1000)}]},mapped color
                    },
                },
                ->,samples=10,domain=-1:1
            ]{x*x};
        \end{axis}
    \end{tikzpicture}
\end{document}

解釋

這段程式碼和上一個問題中的程式碼的主要區別在於

\pgfmathsetmacro\pgfplots@quiver@x{\pgf@x}
\pgfmathsetmacro\pgfplots@quiver@y{\pgf@y}

被製成

\pgfmathsetmacro\pgfplots@quiver@x{\pgf@x}\global\let\pgfplots@quiver@x\pgfplots@quiver@x%
\pgfmathsetmacro\pgfplots@quiver@y{\pgf@y}\global\let\pgfplots@quiver@y\pgfplots@quiver@y%

這表明錯誤是由額外分組引入的。但為什麼會有額外的分組呢?嗯,因為當 pgfplots 遇到

\draw[/pgfplots/quiver/every arrow]...

\draw按照慣例,的選項僅影響 this \draw。因此鈦kZ 會將其放入\draw一個群組中。由於\pgfplotsplothandlerquiver@vis@path因此被放在一個群組中,因此您無法\pgfplots@quiver@x從外部看到(特別是在 中after arrow)。


順便說一句#1\pgfplotsplothandlerquiver@vis@path粗略地說,

\global\pgf@x=\pgf@x
\global\pgf@x=\pgf@y

而上面的分析也說明了這樣做的必要性。

相關內容