Graphics, Figures & TablesUse variables in addplot in axis environment

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
Eenoku
Posts: 1
Joined: Tue Dec 20, 2016 12:03 pm

Use variables in addplot in axis environment

Post by Eenoku »

I wanted to make a macro \simplePlotWithFuncs creating plots both from CSV files and from given functions. I need it to be quite versatile, so I can set a lot of things. Because of limited number of parameters in the LaTeX macros I'm forced to use some of them for multiple arguments and parse those afterwards.


The problem is, this approach worked OK until I needed to give parameters to the addplot macro in axis environment. There I'm getting different errors and I don't know, how to solve this.


I have seen similar questions:

http://tex.stackexchange.com/questions/ ... nvironment

http://tex.stackexchange.com/questions/ ... n-pgfplots

http://tex.stackexchange.com/questions/ ... n-pgfplots

http://tex.stackexchange.com/questions/ ... h-pgfplots

But, unfortunately, I'm still not able to solve the problem.


Thank you very much for all your answers and recommendations,
Eenoku


My attempts
1] I've tried to use `evaluate` parameter in pgfplots `foreach` cycle:

Code: Select all

\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] I've also tried to use the construction `\edef\variable{\noexpand`, but it doesn't work, too:

Code: Select all

\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}
        }

Error

Code: Select all

! 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!
Code with my attempt

Code: Select all

\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 file

Code: Select all

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

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

Stefan Kottwitz
Site Admin
Posts: 10348
Joined: Mon Mar 10, 2008 9:44 pm

Use variables in addplot in axis environment

Post by Stefan Kottwitz »

Hi Eenoku,

welcome to the forum!

It's nice that you post such a complete example. Maybe it can be a bit simplified to better focus on the specific problem?

It worked until you introduced an additional parameter. How does the working code look? So we could take a look at the specific difference. Maybe it's a bit late but I don't see the cause right now.

Instead of \foreach, you could use \pgfplotsinvokeforeach or \pgfplotsinvokeforeachungrouped, see 8.1 Utility Commands in the pgfplots manual.

Stefan
LaTeX.org admin
Post Reply