5

I have been using matlab2tikz to generate a lot of my plots, but I am finding it struggling at this particular job so would like to inquire about how to do it properly in pgfplots / tikz.

Essentially I would like to plot a number of curves on one graph, and have a spectrum color map color them with colors evenly distributed on the spectrum. In MATLAB this is quite simple:

figure
const = linspace(0,pi/4,50)';
x = linspace(0,pi,128);
plots = sin(-const*x)';
noOfLines = length(const);
cmp = jet(noOfLines);
hLine = plot(plots);
for line = 1:noOfLines
set(hLine(line),'Color',cmp(line,:));
end
set(hLine,'LineWidth',1.5);

matlab colormap

I can export this with matlab2tikz and compile it which produces this gorgeous plot:

tikzPlot

The problem is the tikz code is kind of brute forced. It starts with 50 costumed \definecolor

\definecolor{mycolor1}{rgb}{0.00000,0.00000,0.53846}%
\definecolor{mycolor2}{rgb}{0.00000,0.00000,0.61538}%
\definecolor{mycolor3}{rgb}{0.00000,0.07692,1.00000}% ...

for each color that is going to be used, and then 50 \addplot

\addplot [color=mycolor2,solid,line width=1.5pt,forget plot]
  table[row sep=crcr]{1 0\\
2   -0.000396497032881626\\
3   -0.000792994003429991\\

to plot each line with the correct color. I understand I am asking an aweful lot from matlab2tikz so I guess I would like to know how to do this properly using tikz / pgfplots. I have another data set that is similar but it has a lot more plots and comes out to 1.3 mb, taking a long time to compile.

I am happy with creating a pgfplot from a dataset that is in a text file with just the raw data (which would be the plots matrix in my MATLAB workspace in the above example), but have no idea how to proceed from here. Thanks.

EDIT:

In MATLAB my code will be in the workspace like:

matlab workspace

I then dlmwrite it to a .dat file by:

dlmwrite('data.dat',myData,'delimiter', '\t');

Then I try to \addplot3 and get:

plot

This is my dataset:

0.99934 1.0005  1.0007  1.0001  0.99934
1.0029  1.0012  0.99933 0.99862 0.9979
1.0013  0.9988  1.0023  0.99525 0.99771
0.99774 0.99582 0.9943  0.99217 0.98699
0.9912  0.98502 0.97814 0.96116 0.94135
0.97022 0.94084 0.89332 0.83747 0.77688
0.88784 0.75475 0.61326 0.462   0.3339
0.58506 0.30003 0.11623 0.034422    0.0078042
0.099072    0.0090417   0.0031278   0.00023727  0.00050435
-0.0014491  0.00053296  -0.00028736 -0.0021953  0.00056537
-0.0024348  5.2456e-05  0.001471    5.6913e-05  -0.0010844
0.0019199   -0.0023666  0.00065858  -2.7668e-05 0.00034113

Thanks for all your help.

  • I think you are using tabs for delimiter but you are telling this to pgfplots. Whitespace separators work just fine. – percusse Aug 28 '14 at 10:05
  • Hi @percusse, I have tried with [col sep=tab] but still get the same picture. Thanks – Steve Hatcher Aug 29 '14 at 3:13
5

Your const variable appears to be like a y variable and the plotted function is actually f(x,y) = sin(-x*y).

This can be plotted directly in pgfplots:

\documentclass{standalone}

\usepackage{pgfplots}

\pgfplotsset{
    compat=1.11,
    trig format plots=rad,
}

\begin{document}

\begin{tikzpicture}
    \begin{axis}[
        view={0}{0},
        enlarge z limits=false,
        enlarge x limits=upper,
        colormap/jet,
    ]
    \addplot3[
        mesh,
        patch type=line,
        domain=0:pi,samples=128,
        domain y=0:pi/4, samples y=50,
        point meta=y,
    ]
    {sin(-y*x)};
    \end{axis}
\end{tikzpicture}
\end{document}

enter image description here

The key ideas are to make a 3D mesh plot, and visualize the mesh lines by means of their scanlines (i.e. patch type=line) and show only the X/Z plane. I used point meta=y in order to define the y coordinate as color data.

EDIT

The same approach is possible if you place the data matrix into a table:

\documentclass{standalone}

\usepackage{pgfplots}

\pgfplotsset{
    compat=1.11,
}

\begin{document}

\begin{tikzpicture}
    \begin{axis}[
        view={0}{0},
        enlarge z limits=false,
        enlarge x limits=upper,
        colormap/jet,
    ]
    \addplot3[
        mesh,
        patch type=line,
        point meta=y,
    ]
    table {P.dat};
    \end{axis}
\end{tikzpicture}
\end{document}

enter image description here

The data table contains the same data, it is of the form

0.0e0 0.0e0 0.0e0 0.0e0
2.47371e-2 0.0e0 0.0e0 0.0e0
4.94742e-2 0.0e0 0.0e0 0.0e0
7.42113e-2 0.0e0 0.0e0 0.0e0
9.8948401e-2 0.0e0 0.0e0 0.0e0
1.23685501e-1 0.0e0 0.0e0 0.0e0
1.4842259e-1 0.0e0 0.0e0 0.0e0
1.7315968e-1 0.0e0 0.0e0 0.0e0
1.9789677e-1 0.0e0 0.0e0 0.0e0
[...]
3.0673993e0 0.0e0 0.0e0 0.0e0
3.0921364e0 0.0e0 0.0e0 0.0e0
3.1168735e0 0.0e0 0.0e0 0.0e0
3.1416106e0 0.0e0 0.0e0 0.0e0

0.0e0 1.60283e-2 0.0e0 1.60283e-2
2.47371e-2 1.60283e-2 -3.8e-4 1.60283e-2
4.94742e-2 1.60283e-2 -8.0e-4 1.60283e-2
7.42113e-2 1.60283e-2 -1.19e-3 1.60283e-2
9.8948401e-2 1.60283e-2 -1.59e-3 1.60283e-2
[...]

It is given in scanlines (please ignore the fourth column; I exported it together with my color data which is the y coordinate). The precise format is described in the pgfplots manual (section about 3d plots).

NOTE: pgfplots cannot transpose the data file. Consequently, it will only show scanlines along a specific axis. You will need to transpose it if it does not fit.

  • Hi Christian, thanks for your reply. I must not have been quite clear enough in my question though. What I am actually after is a method to do this, but taking data from an external file, that is not generated by a mathematical expression (that was just for an example). So my x-y data sets will be in columns in an external text file and added like in 3.3.1 of the manual Getting the Data into TeX. So basically each \addplot table needs to be added with the right color set I am thinking, but this is where I am lost. Thanks – Steve Hatcher Aug 27 '14 at 3:00
  • That works in the same way; see my edit. – Christian Feuersänger Aug 27 '14 at 16:18
  • Hi Christian, thanks. I am still having problems getting this working sorry. My data comes from MATLAB in the form of columns. Each column being a, x-y plot. I save it and use your code but only get one funny looking plot. I have updated my question with just an example of what is going on. Thanks for your assistance. – Steve Hatcher Aug 28 '14 at 2:37
  • You are right, the details of the 3d table format make the difference here... I posted this as tex.stackexchange.com/questions/198639/… together with an in-depth answer. – Christian Feuersänger Aug 30 '14 at 10:17

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.