0

I have a CSV file with three columns. I would like to make a scatter plot with numbers in the first column as the x-values of the plot points, and the ratio of the other two numbers on each line as their y-values.

In general I would like to read data from a CSV file, and define new attributes which are functions of the attributes which come from the file. This is like the function input format described in section 81.4 of the TikZ manual, except that the "independent variables" are not to be taken from an interval but from the corresponding column of the CSV file.

If this is not possible with TikZ datavisualization, is it possible with PGFPlots? I have never used it. If it is possible with neither I will use awk or something similar to stick the calculated values onto the end of each line of the file.

A very slightly modified version of my real LaTeX file follows. Compilation fails since seq and par are not from the same data block as the definition of speedup, but this minimal non-working example illustrates what I want to do.

\documentclass[tikz]{standalone}
\usetikzlibrary{datavisualization.formats.functions}

\begin{document}

\begin{tikzpicture}
    \datavisualization [
        scientific axes,
        x axis={attribute=nproc},
        y axis={attribute=speedup},
        visualize as scatter,
    ]
        data {
            nproc,  seq,    par
            1,  10.1,   10.3
            2,  12.7,   5.8
            4,  9.3,    2.9
            10, 11.4,   1.5
            20, 9.9,    0.6
        }
        data [format=function] {
            func speedup = \value seq / \value par;
        }
    ;
\end{tikzpicture}

\end{document}
3

First of all, I would be surprised if this was not possible with datavisualization. However, I almost never used this tool and feel a bit more comfortable with pgfplots, with which you can simply say

y expr=\thisrow{seq}/\thisrow{par}

to make the vertical coordinate the quotient of the entries of two different rows. One could use declare function and so and so forth, but this is a very basic example.

\documentclass[tikz,border=3mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17}
\begin{document}
\begin{tikzpicture}
\begin{axis}[xlabel={$n_\mathrm{proc}$},ylabel=speedup]
\addplot[scatter,only marks] table[col sep=comma,x=nproc,y expr=\thisrow{seq}/\thisrow{par}]{
            nproc,  seq,    par
            1,  10.1,   10.3
            2,  12.7,   5.8
            4,  9.3,    2.9
            10, 11.4,   1.5
            20, 9.9,    0.6
            };
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.