4

I am new to pgfplots and would like to get your help on my question. Basically I need to read y value from a file that only contains a list of data (in one column), normalize each value by y=(y-min(Y))/(max(Y)-min(Y)) where max(Y) is the maximum value in the list and min(Y) is the minimum value in the list and plot them. For example, if I have {1,2,3,4} then on the plot, the value of y will be {0, 1/3, 2/3,1}.

I use \addplot table [x expr=\coordindex, y index = 0] {filename}; to draw x and y on the plot. What's the normalization expression for y? Is there existing function to do that? If not, can we extract the max(Y) and min(Y) in pgfplots?

4
  • The simple way is to recalculate your table tat it will contain normalized data. Other way is to find maximal value in your table and then determine the plotting function in the way as you mentioned in your question.
    – Zarko
    May 19 '15 at 12:04
  • 1
    Thanks Zarko. I was thinking to pre-process (normalize) data before read into pgfplots. Just wonder whether I can do it in pgfplots as well.
    – user78489
    May 19 '15 at 12:42
  • As far as I know, the pgfplots is exelent tool for visualization of data or drawing given function. It is not intenede for data procesing. And as I said, if you manualy determines min and max value, the normalization proces can be performed by (considering your formula) with pgfplot.
    – Zarko
    May 19 '15 at 12:48
  • I see. I will pre-process all data then since we have a large number of files for plotting. Thanks.
    – user78489
    May 19 '15 at 12:58
4

As was already discussed in the comments below the question it is the best solution to preprocess the data externally when you have a large amount of data.

Here a solution using pgfplots with the help of pgfplotstable.

\documentclass[border=2pt]{standalone}
\usepackage{pgfplots}
\usepackage{pgfplotstable}      % needed to sort the data table
    % store data in TXT file
    \begin{filecontents}{data.txt}
        y
        2
        1
        4
        3
    \end{filecontents}
\begin{document}
    \begin{tikzpicture}
            % store sorted data in `\data'
            \pgfplotstablesort[sort key=y]{\data}{data.txt}
            % store the first and last value of the (sorted) `\data' table
            % which correspond to the min and max values
            \pgfplotstablegetelem{0}{y}\of{\data}
                \pgfmathsetmacro{\Min}{\pgfplotsretval}
            \pgfplotstablegetrowsof{\data}
                    \pgfmathsetmacro{\Max}{\pgfplotsretval-1}
                \pgfplotstablegetelem{\Max}{y}\of{\data}
                    \pgfmathsetmacro{\Max}{\pgfplotsretval}
        \begin{axis}[
            only marks,
        ]
            \addplot
                table [
                    x expr=\coordindex,
                    y expr=(\thisrow{y}-\Min)/(\Max-\Min),
                ] {data.txt};
        \end{axis}
    \end{tikzpicture}
\end{document}

image showing the result of above code

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.