\pgfforeachungrouped 带有三个参数

\pgfforeachungrouped 带有三个参数

这是对上一个问题我的,稍微补充一下。我想循环遍历文件中的数据并以列表形式提供绘图样式。在旧问题中,我只给出了颜色和索引,而 Christian Hupfer 想出了一个可行的解决方案就像略有变化,厘米休斯。

现在,我想更精确地指定样式。例如,我希望能够像以下 MWE 中那样绘制虚线或虚线

\documentclass{article}
\usepackage{xcolor}
\usepackage{pgfplots}
\begin{document}
  \begin{tikzpicture}
    \begin{axis}
     \pgfplotsforeachungrouped \foridx/\forcol/\forstyle in {1/black/solid,2/red/dashed,3/green/dotted,4/blue/solid,5/orange/dotted}{
            \expandafter\addplot\expandafter[\forcol,\forstyle]{x^\foridx};
      }
    \end{axis}
  \end{tikzpicture}
\end{document}

如果我省略\forstyle列表中的第三个参数,我会得到 Christian Hupfer 的解决方案,该解决方案有效。像这样,它会产生许多错误:

! Undefined control sequence.
<argument> ...pgfplots@foreach@loc@TMPb \forstyle 

l.9       }

The control sequence at the end of the top line
of your error message was never \def'ed. If you have
misspelled it (e.g., `\hobx'), type `I' and the correct
spelling (e.g., `I\hbox'). Otherwise just continue,
and I'll forget about whatever was undefined.

Missing character: There is no 1 in font nullfont!
Missing character: There is no / in font nullfont!
Missing character: There is no b in font nullfont!
Missing character: There is no l in font nullfont!
Missing character: There is no a in font nullfont!
Missing character: There is no c in font nullfont!
Missing character: There is no k in font nullfont!
Missing character: There is no / in font nullfont!
Missing character: There is no s in font nullfont!
Missing character: There is no o in font nullfont!
Missing character: There is no l in font nullfont!
Missing character: There is no i in font nullfont!
Missing character: There is no d in font nullfont!
Missing character: There is no , in font nullfont!
Missing character: There is no 2 in font nullfont!
Missing character: There is no / in font nullfont!
Missing character: There is no r in font nullfont!
Missing character: There is no e in font nullfont!
Missing character: There is no d in font nullfont!
Missing character: There is no / in font nullfont!
Missing character: There is no d in font nullfont!
Missing character: There is no a in font nullfont!
Missing character: There is no s in font nullfont!
Missing character: There is no h in font nullfont!
Missing character: There is no e in font nullfont!
Missing character: There is no d in font nullfont!
Missing character: There is no , in font nullfont!
Missing character: There is no 3 in font nullfont!
Missing character: There is no / in font nullfont!
Missing character: There is no g in font nullfont!
Missing character: There is no r in font nullfont!
Missing character: There is no e in font nullfont!
Missing character: There is no e in font nullfont!
Missing character: There is no n in font nullfont!
Missing character: There is no / in font nullfont!
Missing character: There is no d in font nullfont!
Missing character: There is no o in font nullfont!
Missing character: There is no t in font nullfont!
Missing character: There is no t in font nullfont!
Missing character: There is no e in font nullfont!
Missing character: There is no d in font nullfont!
Missing character: There is no , in font nullfont!
Missing character: There is no 4 in font nullfont!
Missing character: There is no / in font nullfont!
Missing character: There is no b in font nullfont!
Missing character: There is no l in font nullfont!
Missing character: There is no u in font nullfont!
Missing character: There is no e in font nullfont!
Missing character: There is no / in font nullfont!
Missing character: There is no s in font nullfont!
Missing character: There is no o in font nullfont!
Missing character: There is no l in font nullfont!
Missing character: There is no i in font nullfont!
Missing character: There is no d in font nullfont!
Missing character: There is no , in font nullfont!
Missing character: There is no 5 in font nullfont!
Missing character: There is no / in font nullfont!
Missing character: There is no o in font nullfont!
Missing character: There is no r in font nullfont!
Missing character: There is no a in font nullfont!
Missing character: There is no n in font nullfont!
Missing character: There is no g in font nullfont!
Missing character: There is no e in font nullfont!
Missing character: There is no / in font nullfont!
Missing character: There is no d in font nullfont!
Missing character: There is no o in font nullfont!
Missing character: There is no t in font nullfont!
Missing character: There is no t in font nullfont!
Missing character: There is no e in font nullfont!
Missing character: There is no d in font nullfont!
! Undefined control sequence.
\pgfkeyscurrentkey ->\forstyle 

l.9       }

The control sequence at the end of the top line
of your error message was never \def'ed. If you have
misspelled it (e.g., `\hobx'), type `I' and the correct
spelling (e.g., `I\hbox'). Otherwise just continue,
and I'll forget about whatever was undefined.


! Package PGF Math Error: Could not parse input '' as a floating point number, 
sorry. The unreadable part was near ''. (in 'x^').

See the PGF Math package documentation for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.9       }

This error message was generated by an \errmessage
command, so I can't give any explicit help.
Pretend that you're Hercule Poirot: Examine all clues,
and deduce the truth by order and method.

NOTE: coordinate (2Y5.0e0],3Y0.0e0]) has been dropped because it is unbounded (
in y). (see also unbounded coords=jump). 

! Package PGF Math Error: Could not parse input '' as a floating point number, 
sorry. The unreadable part was near ''. (in 'x^').

See the PGF Math package documentation for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.9       }

(That was another \errmessage.)

NOTE: coordinate (2Y4.5833334e0],3Y0.0e0]) has been dropped because it is unbou
nded (in y). (see also unbounded coords=jump). 

! Package PGF Math Error: Could not parse input '' as a floating point number, 
sorry. The unreadable part was near ''. (in 'x^').

See the PGF Math package documentation for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.9       }

(That was another \errmessage.)

NOTE: coordinate (2Y4.1666669e0],3Y0.0e0]) has been dropped because it is unbou
nded (in y). (see also unbounded coords=jump). 

! Package PGF Math Error: Could not parse input '' as a floating point number, 
sorry. The unreadable part was near ''. (in 'x^').

See the PGF Math package documentation for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.9       }

(That was another \errmessage.)

NOTE: coordinate (2Y3.7500003e0],3Y0.0e0]) has been dropped because it is unbou
nded (in y). (see also unbounded coords=jump). 

! Package PGF Math Error: Could not parse input '' as a floating point number, 
sorry. The unreadable part was near ''. (in 'x^').

See the PGF Math package documentation for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.9       }

(That was another \errmessage.)

NOTE: coordinate (2Y3.3333337e0],3Y0.0e0]) has been dropped because it is unbou
nded (in y). (see also unbounded coords=jump). 

! Package PGF Math Error: Could not parse input '' as a floating point number, 
sorry. The unreadable part was near ''. (in 'x^').

See the PGF Math package documentation for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.9       }

(That was another \errmessage.)

NOTE: coordinate (2Y2.9166672e0],3Y0.0e0]) has been dropped because it is unbou
nded (in y). (see also unbounded coords=jump). 

! Package PGF Math Error: Could not parse input '' as a floating point number, 
sorry. The unreadable part was near ''. (in 'x^').

See the PGF Math package documentation for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.9       }

(That was another \errmessage.)

NOTE: coordinate (2Y2.5000006e0],3Y0.0e0]) has been dropped because it is unbou
nded (in y). (see also unbounded coords=jump). 

! Package PGF Math Error: Could not parse input '' as a floating point number, 
sorry. The unreadable part was near ''. (in 'x^').

See the PGF Math package documentation for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.9       }

(That was another \errmessage.)

NOTE: coordinate (2Y2.083334e0],3Y0.0e0]) has been dropped because it is unboun
ded (in y). (see also unbounded coords=jump). 

! Package PGF Math Error: Could not parse input '' as a floating point number, 
sorry. The unreadable part was near ''. (in 'x^').

See the PGF Math package documentation for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.9       }

(That was another \errmessage.)

NOTE: coordinate (2Y1.6666675e0],3Y0.0e0]) has been dropped because it is unbou
nded (in y). (see also unbounded coords=jump). 

! Package PGF Math Error: Could not parse input '' as a floating point number, 
sorry. The unreadable part was near ''. (in 'x^').

See the PGF Math package documentation for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.9       }

(That was another \errmessage.)

NOTE: coordinate (2Y1.2500009e0],3Y0.0e0]) has been dropped because it is unbou
nded (in y). (see also unbounded coords=jump). 

! Package PGF Math Error: Could not parse input '' as a floating point number, 
sorry. The unreadable part was near ''. (in 'x^').

See the PGF Math package documentation for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.9       }

(That was another \errmessage.)

NOTE: coordinate (2Y8.333344e-1],3Y0.0e0]) has been dropped because it is unbou
nded (in y). (see also unbounded coords=jump). 

! Package PGF Math Error: Could not parse input '' as a floating point number, 
sorry. The unreadable part was near ''. (in 'x^').

See the PGF Math package documentation for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.9       }

(That was another \errmessage.)

NOTE: coordinate (2Y4.166677e-1],3Y0.0e0]) has been dropped because it is unbou
nded (in y). (see also unbounded coords=jump). 

! Package PGF Math Error: Could not parse input '' as a floating point number, 
sorry. The unreadable part was near ''. (in 'x^').

See the PGF Math package documentation for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.9       }

(That was another \errmessage.)

NOTE: coordinate (2Y9.9e-7],3Y0.0e0]) has been dropped because it is unbounded 
(in y). (see also unbounded coords=jump). 

! Package PGF Math Error: Could not parse input '' as a floating point number, 
sorry. The unreadable part was near ''. (in 'x^').

See the PGF Math package documentation for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.9       }

(That was another \errmessage.)

NOTE: coordinate (1Y4.1666573e-1],3Y0.0e0]) has been dropped because it is unbo
unded (in y). (see also unbounded coords=jump). 

! Package PGF Math Error: Could not parse input '' as a floating point number, 
sorry. The unreadable part was near ''. (in 'x^').

See the PGF Math package documentation for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.9       }

(That was another \errmessage.)

NOTE: coordinate (1Y8.3333243e-1],3Y0.0e0]) has been dropped because it is unbo
unded (in y). (see also unbounded coords=jump). 

! Package PGF Math Error: Could not parse input '' as a floating point number, 
sorry. The unreadable part was near ''. (in 'x^').

See the PGF Math package documentation for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.9       }

(That was another \errmessage.)

NOTE: coordinate (1Y1.24999913e0],3Y0.0e0]) has been dropped because it is unbo
unded (in y). (see also unbounded coords=jump). 

! Package PGF Math Error: Could not parse input '' as a floating point number, 
sorry. The unreadable part was near ''. (in 'x^').

See the PGF Math package documentation for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.9       }

(That was another \errmessage.)

NOTE: coordinate (1Y1.6666656e0],3Y0.0e0]) has been dropped because it is unbou
nded (in y). (see also unbounded coords=jump). 

! Package PGF Math Error: Could not parse input '' as a floating point number, 
sorry. The unreadable part was near ''. (in 'x^').

See the PGF Math package documentation for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.9       }

(That was another \errmessage.)

NOTE: coordinate (1Y2.0833322e0],3Y0.0e0]) has been dropped because it is unbou
nded (in y). (see also unbounded coords=jump). 

! Package PGF Math Error: Could not parse input '' as a floating point number, 
sorry. The unreadable part was near ''. (in 'x^').

See the PGF Math package documentation for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.9       }

(That was another \errmessage.)

NOTE: coordinate (1Y2.4999988e0],3Y0.0e0]) has been dropped because it is unbou
nded (in y). (see also unbounded coords=jump). 

! Package PGF Math Error: Could not parse input '' as a floating point number, 
sorry. The unreadable part was near ''. (in 'x^').

See the PGF Math package documentation for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.9       }

(That was another \errmessage.)

NOTE: coordinate (1Y2.9166653e0],3Y0.0e0]) has been dropped because it is unbou
nded (in y). (see also unbounded coords=jump). 

! Package PGF Math Error: Could not parse input '' as a floating point number, 
sorry. The unreadable part was near ''. (in 'x^').

See the PGF Math package documentation for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.9       }

(That was another \errmessage.)

NOTE: coordinate (1Y3.3333319e0],3Y0.0e0]) has been dropped because it is unbou
nded (in y). (see also unbounded coords=jump). 

! Package PGF Math Error: Could not parse input '' as a floating point number, 
sorry. The unreadable part was near ''. (in 'x^').

See the PGF Math package documentation for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.9       }

(That was another \errmessage.)

NOTE: coordinate (1Y3.7499985e0],3Y0.0e0]) has been dropped because it is unbou
nded (in y). (see also unbounded coords=jump). 

! Package PGF Math Error: Could not parse input '' as a floating point number, 
sorry. The unreadable part was near ''. (in 'x^').

See the PGF Math package documentation for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.9       }

(That was another \errmessage.)

NOTE: coordinate (1Y4.166665e0],3Y0.0e0]) has been dropped because it is unboun
ded (in y). (see also unbounded coords=jump). 

! Package PGF Math Error: Could not parse input '' as a floating point number, 
sorry. The unreadable part was near ''. (in 'x^').

See the PGF Math package documentation for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.9       }

(That was another \errmessage.)

NOTE: coordinate (1Y4.5833316e0],3Y0.0e0]) has been dropped because it is unbou
nded (in y). (see also unbounded coords=jump). 

! Package PGF Math Error: Could not parse input '' as a floating point number, 
sorry. The unreadable part was near ''. (in 'x^').

See the PGF Math package documentation for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.9       }

(That was another \errmessage.)

NOTE: coordinate (1Y4.9999982e0],3Y0.0e0]) has been dropped because it is unbou
nded (in y). (see also unbounded coords=jump). 

使用 cmhughes 链接的代码片段,我无法让它工作,并且出现类似的错误(可能是同一个问题)

\addplot因此我尝试在列表中给出参数:

\pgfplotsforeachungrouped \foridx/\forstyle in {1/{black,solid},2/{red,dashed},3/{green,dotted},4/{blue,solid},5/{orange,dotted}}{
   \expandafter\addplot\expandafter[\forstyle]{x^\foridx};
   }

编译后,它只产生一条黑实线(它只循环遍历第一个元素)

事实上,我找不到任何使用三个变量的示例。如何使用pgffor三个或更多变量?

答案1

PGFPlotsToDo 列表 (v1.14) 在第 35 页这个已经写完了:

\pgfplotsforeachungrouped不能与三个或更多参数组合,例如\foreach

因此,目前必须找到另一种解决方案。但幸运的是,在这种情况下,通过定义自定义 ,可以轻松实现这一点cycle list

有关更多详细信息,请查看代码中的注释。

% used PGFPlots v1.14
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
    % create a cycle list that fits your needs ...
    \pgfplotscreateplotcyclelist{mylist}{
       {black,solid},
       {red,dashed},
       {green,dotted},
       {blue,solid},
       {orange,dotted}%
    }
\begin{document}
    \begin{tikzpicture}
        \begin{axis}[
            ymin=-100,
            ymax=100,
            legend pos=south east,
            % ... and apply it here ...
            % (or in `\pgfplotsset' if it should be used as default for all plots)
            cycle list name=mylist,
        ]
            % ... then your loop simplifies to ...
            \foreach \i in {1,...,5} {
                % ... where I just added `very thick' so the result can be
                % seen easier
                \addplot+ [very thick] {(x)^\i};
                    \addlegendentryexpanded{$x^\i$};
            }
        \end{axis}
    \end{tikzpicture}
\end{document}

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

相关内容