pgfplots:在 pgfplotsextra 中使用 foreach 创建的路径名

pgfplots:在 pgfplotsextra 中使用 foreach 创建的路径名

这是在 TikZ foreach 循环中命名路径

我想绘制一系列图,每个图的名称都由 生成(使用name path global) ,并在每个图与参考线的交点处foreach放置。名称也是在循环中生成的。名称是从 生成的,参考线称为:coordinatecoordinates\ksdlistRefLine

\documentclass{standalone}

\usepackage{tikz,pgfplots}

\usetikzlibrary{intersections}%,backgrounds,calc}

\def\ksdList{5e-2,3e-2,2e-2,1e-2,4e-3,1e-3,4e-4,1e-4,1e-5}

\begin{document}
  \begin{tikzpicture}
    \begin{loglogaxis}[
      xmin=2300,xmax=1e8,ymin=0.008,ymax=0.1
      ]
      \path[draw,name path global=RefLine] (rel axis cs:1,0) -- (rel axis cs:1,1);
      \foreach \ksd in \ksdList {
        \edef\plotopt{name path global=plot-\ksd}
        \expandafter\addplot\expandafter[\plotopt,raw gnuplot] gnuplot {
          set xrange [2300:1e8];
          set yrange [0.008:0.1];
          set logscale xy;
          set samples 100;
          f(R,L) = -2*log10(2.51/(R*sqrt(L))+\ksd/3.71)-1/sqrt(L);
          set cont base;
          set cntrparam levels discrete 0;
          unset surface;
          splot f(x,y) smooth unique;
        };
        \edef\intersectionsopt{name intersections={of plot-\ksd and RefLine}}
        % -----------
        % This fails:
        \expandafter\pgfplotsextra{\expandafter\path\expandafter[\intersectionsopt] (intersection-1) coordinate (end-\ksd);};
      }
      % -----------------------------------------------
      % This is how I can place the coordinate manually
      \pgfplotsextra{\path[name intersections={of=plot-5e-2 and RefLine}] (intersection-1) coordinate (end-5e-2);}
    \end{loglogaxis}
    % ---------------------------
    % And I can now place a label
    \path (end-5e-2) node[right] {0.05};
  \end{tikzpicture}
\end{document}

创建交点的选项在\intersectionsopt,但它们包含一对括号,我认为这是编译期间出现错误的原因。

答案1

您无需费心使用 s \edef,只需使用 即可\pgfplotsinvokeforeach{<list>}{<code, where the current list element is available as #1>}直接执行循环代码(而不是像 那样累积命令并同时执行它们\foreach)。

\documentclass{standalone}

\usepackage{pgfplots}

\usetikzlibrary{intersections}

\begin{document}
  \begin{tikzpicture}
    \begin{loglogaxis}[clip=false,
      xmin=2300,xmax=1e8,ymin=0.008,ymax=0.1
      ]
      \path[draw,name path global=RefLine] (rel axis cs:1,0) -- (rel axis cs:1,1);
      \pgfplotsinvokeforeach{5e-2,3e-2,2e-2,1e-2,4e-3,1e-3,4e-4,1e-4,1e-5}{
       \addplot[name path global=plot-#1,raw gnuplot] gnuplot {
          set xrange [2300:1e8];
          set yrange [0.008:0.1];
          set logscale xy;
          set samples 100;
          f(R,L) = -2*log10(2.51/(R*sqrt(L))+#1/3.71)-1/sqrt(L);
          set cont base;
          set cntrparam levels discrete 0;
          unset surface;
          splot f(x,y) smooth unique;
        };
        \pgfplotsextra{\path[name intersections={of=plot-#1 and RefLine}] (intersection-1) node [right] {#1};};
      }
    \end{loglogaxis}
  \end{tikzpicture}
\end{document}

如果您需要将列表作为宏,则可以使用\edefplot 命令。

\documentclass{standalone}

\usepackage{pgfplots}

\usetikzlibrary{intersections}

\def\ksdList{{5e-2,3e-2,2e-2,1e-2,4e-3,1e-3,4e-4,1e-4,1e-5}}

\begin{document}
  \begin{tikzpicture}
    \begin{loglogaxis}[clip=false,
      xmin=2300,xmax=1e8,ymin=0.008,ymax=0.1
      ]
      \path[draw,name path global=RefLine] (rel axis cs:1,0) -- (rel axis cs:1,1);
      \edef\doplot{
      \noexpand\pgfplotsinvokeforeach{\ksdList}{
       \noexpand\addplot[name path global=plot-\noexpand##1,raw gnuplot] gnuplot {
          set xrange [2300:1e8];
          set yrange [0.008:0.1];
          set logscale xy;
          set samples 100;
          f(R,L) = -2*log10(2.51/(R*sqrt(L))+\noexpand##1/3.71)-1/sqrt(L);
          set cont base;
          set cntrparam levels discrete 0;
          unset surface;
          splot f(x,y) smooth unique;
        };
        \noexpand\pgfplotsextra{\noexpand\path[name intersections={of=plot-\noexpand##1 and RefLine}] (intersection-1) node [right] {\noexpand##1};};
      }}
      \doplot
    \end{loglogaxis}
  \end{tikzpicture}
\end{document}

相关内容