4

I HAVE EDITED THIS THREAD, NEW QUESTION AT THE END

First of all, I apologize for my possibly bad English as it is not my mother tongue.

My problem is complex but I am going to try to explain it the best I can. Don't hesitate to ask more information if there is something you don't understand.

I have several data files and each file corresponds to one set of marks to plot. Those data were computed by modifying the numerical value of a parameter of my system called k_v (ranging from 0 to 1) in the definition of my function.
I want the colorbar (on the right) to display the values of the parameter k_v (not the y-axis!) and I want each plot to have a different color, corresponding to the color of the parameter k_v that is displayed in the colorbar on the right.

I have 2 problems:

  1. I want to use "marks only". If I don't, it works, but as soon as I use "marks only" every mark becomes black and I don't understand why.

  2. Right now I use a "trick" so that I don't have to add a "addplot..." command for each plot (I have more than 100 plots in the original document) and I use this to create the y axis of the colorbar.
    However, as I said before, my parameter k_v goes from 0 to 1 (and not from 1 to 4 as it currently does), so for this to work as intended, I should use the command "\foreach \m in {0.25,0.50,...,1}" instead of "\foreach \m in {1,...,4}" and I should rename my data files "data_0.25.dat", "data_0.50.dat", etc... however this won't work as I use a "." before the extension of the file and latex doesn't understand. Do you have any idea on how I could avoid this while still having a "\foreach \m in {1,...,4}" trick?

for the sake of simplification, let's use this code here:

\documentclass[varwidth]{standalone}

% *** PACKAGES ***
\usepackage{standalone} % Pour les stand-alone
\usepackage[usenames,dvipsnames]{xcolor} % 68 dvips colors à déclarer avant tikz
\usepackage{tikz} % Pour les figure Tikz
\usepackage{pgfplots} % Pour tracer les courbes à partir de fichiers de données
\usepackage{pgfplotstable} % Pour pouvoir lire les tableaux des fichiers .txt
\usepackage{filecontents} % Pour créer des fichiers .dat
% *** PACKAGES END ***

\pgfplotsset{compat=1.10} % pour définir la version de pgfplots

\begin{filecontents}{data_1.dat}
-10 0
-8 3
-8 -3
-5 1
-5 -1
\end{filecontents}
%
\begin{filecontents}{data_2.dat}
-8 0
-7.5 2.5
-7.5 -2.5
-4.5 0.8
-4.5 -0.8
\end{filecontents}
%
\begin{filecontents}{data_3.dat}
-6.5 0
-7 2
-7 -2
-4.25 0.6
-4.25 -0.6
\end{filecontents}
%
\begin{filecontents}{data_4.dat}
-5.25 0
-6.75 1.75
-6.75 -1.75
-4 0.4
-4 -0.4
\end{filecontents}

\begin{document}
\tikzset{mark options={mark size=1.5, line width=0.3pt}}

\begin{tikzpicture}

\begin{axis}[
        colorbar,
        colorbar style={ylabel=$k_v$-value},%,ytick={0,0.2,...,1}},
        width=0.7\textwidth,
  height=0.6\textwidth,
        grid=major,
        minor x tick num=1,
  xlabel={Real($\lambda (A)$)},
        minor y tick num=1,
  ylabel={Imag($\lambda (A)$)},
        tick label style={font=\footnotesize},
    ]

\foreach \m in {1,...,4}
    {\edef\temp{\noexpand\addplot[mesh,only marks,point meta=\m] table             {data_\m.dat};}\temp}

\end{axis}
\end{tikzpicture}


\end{document}

This is an example of what I want (this was plotted using matlab) enter image description here

Thank you very much for your help

EDIT: Ok I figured it wasn't the right approach, so instead I added a third column to each data file. This third column corresponds to the "point meta". If I add manually every plot, it works like a charm. However, I have more than 100 plots so I want to do it automatically using the command

\foreach \m in {1,...,4}
{\edef\temp{\noexpand\addplot+[mark=*,only marks,scatter,point meta={\thisrow{k_v}},] table {data_\m.dat};}\temp}

Problem is, \thisrow doesn't seem to work in this case. Do anyone knows how to avoid this problem?

Here is the complete code:

\documentclass[varwidth]{standalone}

% *** PACKAGES ***
\usepackage{standalone} % Pour les stand-alone
\usepackage[usenames,dvipsnames]{xcolor} % 68 dvips colors à déclarer avant tikz
\usepackage{tikz} % Pour les figure Tikz
\usepackage{pgfplots} % Pour tracer les courbes à partir de fichiers de données
\usepackage{pgfplotstable} % Pour pouvoir lire les tableaux des fichiers .txt
\usepackage{filecontents} % Pour créer des fichiers .dat
% *** PACKAGES END ***

\pgfplotsset{compat=1.12} % pour définir la version de pgfplots

\begin{filecontents}{data_1.dat}
x y k_v
-10 0 0.25
-8 3 0.25
-8 -3 0.25
-5 1 0.25
-5 -1 0.25
\end{filecontents}
%
\begin{filecontents}{data_2.dat}
x y k_v
-8 0 0.50
-7.5 2.5  0.50
-7.5 -2.5  0.50
-4.5 0.8  0.50
-4.5 -0.8  0.50
\end{filecontents}
%
\begin{filecontents}{data_3.dat}
x y k_v
-6.5 0 0.75
-7 2 0.75
-7 -2 0.75
-4.25 0.6 0.75
-4.25 -0.6 0.75
\end{filecontents}
%
\begin{filecontents}{data_4.dat}
x y k_v
-5.25 0 1
-6.75 1.75 1
-6.75 -1.75 1
-4 0.4 1
-4 -0.4 1
\end{filecontents}

\begin{document}
\tikzset{mark options={mark size=1.5, line width=0.5pt}}

\begin{tikzpicture}

\begin{axis}[
    colorbar,
    colorbar style={ylabel=$k_v$-value},%,ytick={0,0.2,...,1}},
    width=0.7\textwidth,
            height=0.6\textwidth,
    grid=major,
    minor x tick num=1,
            xlabel={Real($\lambda (A)$)},
    minor y tick num=1,
            ylabel={Imag($\lambda (A)$)},
    tick label style={font=\footnotesize},
]

\foreach \m in {1,...,4}
{\edef\temp{\noexpand\addplot+[mark=*,only marks,scatter,point meta={\thisrow{k_v}},]         table {data_\m.dat};}\temp}


    \addplot+[mark=*,only marks,scatter,point meta={\thisrow{k_v}},] table {data_1.dat};
    \addplot+[mark=*,only marks,scatter,point meta={\thisrow{k_v}},] table {data_2.dat};
    \addplot+[mark=*,only marks,scatter,point meta={\thisrow{k_v}},] table {data_3.dat};
    \addplot+[mark=*,only marks,scatter,point meta={\thisrow{k_v}},] table {data_4.dat};

\end{axis}
\end{tikzpicture}

\end{document}
  • For first use \addplot+[mesh,... Note +. For second, I am working. – user11232 Mar 26 '15 at 12:42
  • You could put the color (droop parameter) in a third column and use a scatter plot, possibly merging all the files into one. – John Kormylo Mar 26 '15 at 15:44
  • Yes John, that's what I did (see the EDIT part of the OP), however I have a new problem when I try to automate the plots: the \thisrow doesn't seem to work in "\foreach \m in {1,...,4}..." and I don't understand why – VieuxSinge Mar 26 '15 at 15:47
  • 1
    I wrote my comment before the correction (time overlap). If you added column names in the first row you could use something like \addplot+[scatter,scatter src=explicit] table[x=xcolname,y=ycolname,meta=colordata] {datafile.dat} (page 104). – John Kormylo Mar 26 '15 at 15:54
3

My original suggestion was to merge all the data files into one.

\documentclass[varwidth]{standalone}

% *** PACKAGES ***
\usepackage{standalone} % Pour les stand-alone
\usepackage[usenames,dvipsnames]{xcolor} % 68 dvips colors à déclarer avant tikz
\usepackage{tikz} % Pour les figure Tikz
\usepackage{pgfplots} % Pour tracer les courbes à partir de fichiers de données
\usepackage{pgfplotstable} % Pour pouvoir lire les tableaux des fichiers .txt
\usepackage{filecontents} % Pour créer des fichiers .dat
% *** PACKAGES END ***

\pgfplotsset{compat=1.12} % pour définir la version de pgfplots

\begin{filecontents}{data.dat}
x y k_v
-10 0 0.25
-8 3 0.25
-8 -3 0.25
-5 1 0.25
-5 -1 0.25
-8 0 0.50
-7.5 2.5  0.50
-7.5 -2.5  0.50
-4.5 0.8  0.50
-4.5 -0.8  0.50
-6.5 0 0.75
-7 2 0.75
-7 -2 0.75
-4.25 0.6 0.75
-4.25 -0.6 0.75
-5.25 0 1
-6.75 1.75 1
-6.75 -1.75 1
-4 0.4 1
-4 -0.4 1
\end{filecontents}

\begin{document}
\tikzset{mark options={mark size=1.5, line width=0.5pt}}

\begin{tikzpicture}

\begin{axis}[
    colorbar,
    colorbar style={ylabel=$k_v$-value},%,ytick={0,0.2,...,1}},
    width=0.7\textwidth,
            height=0.6\textwidth,
    grid=major,
    minor x tick num=1,
            xlabel={Real($\lambda (A)$)},
    minor y tick num=1,
            ylabel={Imag($\lambda (A)$)},
    tick label style={font=\footnotesize},
]
\addplot+[only marks,scatter,scatter src=explicit] table[x=x,y=y,meta=k_v] {data.dat};

\end{axis}
\end{tikzpicture}

\end{document}

scatter plot

A second solution consisted of removing the _ character from the file names. Sometime you can get away from it, but obviously not when including a macro as part of the name.

2

You don't need to merge all the data to one file. The trick is to use \pgfplotsinvokeforeach instead of \foreach which allows expansion. Here the code which gives the same resulting image as in the answer of John Kormylo.

\documentclass[varwidth]{standalone}
\usepackage[usenames,dvipsnames]{xcolor}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
    \pgfplotsset{compat=1.10}
\begin{filecontents}{data_1.dat}
    x y k_v
    -10 0 0.25
    -8 3 0.25
    -8 -3 0.25
    -5 1 0.25
    -5 -1 0.25
\end{filecontents}
\begin{filecontents}{data_2.dat}
    x y k_v
    -8 0 0.50
    -7.5 2.5  0.50
    -7.5 -2.5  0.50
    -4.5 0.8  0.50
    -4.5 -0.8  0.50
\end{filecontents}
\begin{filecontents}{data_3.dat}
    x y k_v
    -6.5 0 0.75
    -7 2 0.75
    -7 -2 0.75
    -4.25 0.6 0.75
    -4.25 -0.6 0.75
\end{filecontents}
\begin{filecontents}{data_4.dat}
    x y k_v
    -5.25 0 1
    -6.75 1.75 1
    -6.75 -1.75 1
    -4 0.4 1
    -4 -0.4 1
\end{filecontents}
\begin{document}
    \tikzset{
        mark options={
            mark size=1.5,
            line width=0.3pt,
        },
    }
\begin{tikzpicture}
    \begin{axis}[
        colorbar,
        colorbar style={
            ylabel=$k_v$-value,
        },
        width=0.7\textwidth,
        height=0.6\textwidth,
        grid=major,
        minor x tick num=1,
        xlabel={Real($\lambda (A)$)},
        minor y tick num=1,
        ylabel={Imag($\lambda (A)$)},
        tick label style={
            font=\footnotesize,
        },
    ]
        \pgfplotsinvokeforeach {1,...,4} {
            \addplot [
                only marks,
                scatter,
                scatter src=explicit,
            ] table [x=x,y=y,meta=k_v] {data_#1.dat};
        }
    \end{axis}
\end{tikzpicture}
\end{document}

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.