在轴环境中的 addplot 中使用变量

在轴环境中的 addplot 中使用变量

我想制作一个宏,\simplePlotWithFuncs既能从.csv文件创建图,也能从给定函数创建图。我需要它非常灵活,这样我就可以设置很多东西。由于 LaTeX 宏中的参数数量有限,我被迫将其中一些参数用作多个参数,然后再解析它们。

问题是,这种方法一直有效,直到我需要addplotaxis环境中为宏提供参数。在那里我遇到了不同的错误,我不知道如何解决这个问题。

我也见过类似的问题:

\foreach 在轴环境中不起作用

在 pgfplots 中使用 foreach 循环变量作为节点标签

在 pgfplots 中添加 \foreach 变量

在 pgfplots 循环中使用 \addlegendentry 时出现问题

但不幸的是,我仍然无法解决这个问题。


我的尝试

  1. 我试图\evaluate在 pgfplots\foreach循环中使用参数:

    \foreach [evaluate=\datacol(1) as \datacolI] \datafile in {#1} {
    \addplot+[only marks, mark=square*] table [x=\datacolI, y=time, col sep=comma] {\datafile};
    \addtocounter{counter}{-1}
    }
    
  2. 我也尝试过使用构造\edef\variable{\noexpand},但它也不起作用:

    \foreach \datafile in {#1} {
     \edef\temp{\noexpand\addplot+[only marks, mark=square*] table [x=\datacolI, y=time, col sep=comma] {\datafile}};
     \temp
     \addtocounter{counter}{-1}
    }
    

错误

! Incomplete \iffalse; all text was ignored after line 70.
<inserted text> 
                \fi 
<*> main.tex

? 
! Emergency stop.
<inserted text> 
                \fi 
<*> main.tex

!  ==> Fatal error occurred, no output PDF file produced!

用我的尝试编写代码

\documentclass[10pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage{subfig}     % makra pro "podobrazky" a "podtabulky"
\usepackage{tikz}       % makra pro kresleni
\usepackage{underscore}
\usepackage{amsmath}
\usepackage{pgffor}
\usepackage{pgfplots}
\usepackage{tikz}
\usepackage{arrayjobx}

\pgfplotsset{compat=newest}

\newcounter{counter}

\newcommand{\simplePlotWithFuncs}[9]{
\newarray\labels
\readarray{labels}{#2}
\newarray\datacol
\readarray{datacol}{#3}
\edef\tmp{\datacol(1)}
\begin{figure}[hbtp!]
\centering
\begin{tikzpicture}[domain=0:25, scale=0.75, transform shape]
\begin{axis}[
xlabel=\labels(1),
ylabel=\labels(2),
legend pos=#4,
xmajorgrids=true,
ymajorgrids=true,
grid style=dashed,
width=\textwidth*0.8,
height=\textwidth*0.5,
cycle list name = color list,
each nth point = 1,
filter discard warning=false
]
\setcounter{counter}{0}
\foreach [evaluate=\datacol(1) as \datacolI] \datafile in {#1} {
    \addplot+[only marks, mark=square*] table [x=\datacolI, y=time, col sep=comma] {\datafile};
\addtocounter{counter}{-1}
}
\pgfplotsset{cycle list shift=\value{counter}-1}
\foreach \func in {#9} {
    \addplot+[mark=none, dashed]{\func};
}
\legend{#5}
\end{axis}
\end{tikzpicture}
\caption{#6}
\label{#7}
\smallskip
{\small #8}
\end{figure}
\medskip
}

\begin{document}

\simplePlotWithFuncs
    {ratioThProcs-size10.csv}
    {Number of processes & Time [s]}
    {nprocs&time}
    {north east}
    {size 10x10x10}
    {Stiffness matrix assembling speed dependence on decomposition style}   
    {fig:threadsProcsDecompDifference}
    {Fixed size of problem. Changing style of the decomposition - ratio of MPI processes and threads. Number of threads per process is computed by the formula $\frac{24}{nprocs}$. Points are measured values, dashed line is a hyperbola got by inverse regression.}
    {0.066181 + 0.548915/x}
\end{document}

CSV 文件

nprocs,nthreads,size,time
1,24,10,0.656395
2,12,10,0.256914
3,8,10,0.271941
4,6,10,0.174338
6,4,10,0.130503
8,3,10,0.123015
12,2,10,0.173938
24,1,10,0.114695

答案1

\check我设法使用包中的命令使其工作arrarjobx

  • datacol(1)使用以下命令检索\checkdatacol
\newarray\datacol
\readarray{datacol}{#3}
\checkdatacol(1)
\edef\datacolI{\cachedata}
  • \datacolI然后在循环中使用:
\foreach \datafile in {#1} {
    \addplot+[only marks, mark=square*] table [x=\datacolI, y=time, col sep=comma] {\datafile};
    \addtocounter{counter}{-1}
}

相关内容