# Plot 3D matrix as 2D plot and 3rd dimension as color in Mathematica

1. May 6, 2011

### Muchacho

Hi!

It's been like two days since I have tried to make this work, still I got nothing. Searched Google etc. but no help there.

I have a three dimensional matrix in form of {{a,b,c}, {d,e,f}, {g,h,i}, ... etc. } with a total of 51 elements, i.e. 51x3 matrix.

What I want is to plot it as a 2D line using first 2 elements {{a,b},{d,e},{g,h}, ... etc.} and to color the plot using the values from 3rd element {c, f, i, ... etc.}.
And I have to use built in ColorData["Rainbow"] scheme.

The data is quite simple - plotting both parts separately => the first two elements will give letter U upside down and the third element is a 12th order polynomal that looks similar to Sin2[x].

It is somehow possible to fit functions to given data and to plot them using parametric ones (there is a common parameter for all values) but will it help somehow? Since there will be 3 parametric functions, it will still be only possible to plot them using parametric 3D plot function.

Here are a first 15 elements from given array:
{{-1256.89, 1428.53, 3.95091}, {-1244.34, 1494.18,
4.58655}, {-1217.63, 1556.05, 4.9574}, {-1188.06, 1615.99,
5.09292}, {-1168.71, 1679.1, 5.02187}, {-1137.94, 1737.54,
4.7725}, {-1114.76, 1799.63, 4.37291}, {-1084.5, 1858.54,
3.85128}, {-1054.75, 1918.34, 3.23608}, {-1027.34, 1981.06,
2.55586}, {-995.382, 2042.13, 1.83892}, {-963.726, 2105.59,
1.1125}, {-931.068, 2171.05, 0.401884}, {-892.135, 2233.19,
0.27072}, {-848.104, 2292.43, 0.887201}}

If this is not possible in Mathematica, can MatLAB or other software do it?

Last edited: May 6, 2011
2. May 6, 2011

### Staff: Mentor

Assuming that your 3D data is in an array named dat and your common parameter for all three is in an array called param then you can write:

interp = Table[Interpolation[Transpose[{param, dat[[All, i]]}]], {i, 3}]

That will give you a table of interpolation functions for each of the three values in your dat array. Then you can write:

ParametricPlot[{interp[[1]][t], interp[[2]][t]}, {t, Min[param], Max[param]},
PlotStyle -> Thick,
ColorFunction -> Function[{x, y, t}, ColorData["Rainbow"][interp[[3]][t]]],
ColorFunctionScaling -> False]

3. May 6, 2011

### Muchacho

I am so thankful for your help! Thanks! Now it finally works!

Just one more technical question - because there are values of interp[[3]] exceeds 1 (the Range of Rainbow color function is {0, 1}) and although there is ColorFunctionScaling -> False, it still uses only values in the range {0, 1}, where values are larger than 1, the color is red.
If I scale the values of the interp[[3]] so that they are in range {0, 1}, still ColorFunctionScaling -> True gives single color (indicating it is out of range?) and only with ColorFunctionScaling -> False desired result is achieved. Why? And is there a way for it to work without scaling the values of function manually?

4. Jan 4, 2012

### vineelkanna

Im getting an errors if I used that code, its giving many errors and the main one is
Transpose::tperm: "Permutation {i,3.} is longer than the dimensions {2} of the array"
Please, Can you help me out.

5. Jan 4, 2012

### Bill Simpson

Post exactly the code you are using. Use the simplest code that shows the error, but not simplified to the point where when that works you will have to ask to have your next version fixed. Either scrape your code from Mathematica to the forum and then carefully manually fix any special characters to be plain ASCII so someone trying to help will be able to scrape it off the forum, paste it into Mathematica and be able to exactly reproduce your error, OR attach a small simple notebook with the needed information.

6. Jan 4, 2012

### vineelkanna

Just to test, I took the same values and code from above. This is the code:
param = {{-1256.89, 1428.53, 3.95091}, {-1244.34, 1494.18,
4.58655}, {-1217.63, 1556.05, 4.9574}, {-1188.06, 1615.99,
5.09292}, {-1168.71, 1679.1, 5.02187}, {-1137.94, 1737.54,
4.7725}, {-1114.76, 1799.63, 4.37291}, {-1084.5, 1858.54,
3.85128}, {-1054.75, 1918.34, 3.23608}, {-1027.34, 1981.06,
2.55586}, {-995.382, 2042.13, 1.83892}, {-963.726, 2105.59,
1.1125}, {-931.068, 2171.05, 0.401884}, {-892.135, 2233.19,
0.27072}, {-848.104, 2292.43, 0.887201}}
interp = Table[
Interpolation[Transpose[{param, dat[[All, i]]}]], {i, 3}]
ParametricPlot[{interp[[1]][t], interp[[2]][t]}, {t, Min[param],
Max[param]}, PlotStyle -> Thick,
ColorFunction ->
Function[{x, y, t}, ColorData["Rainbow"][interp[[3]][t]]],
ColorFunctionScaling -> False]

7. Jan 4, 2012

### vineelkanna

The error is this, I tweaked the code But no help. Please tell me how can I get this correct ..

Symbol::argx: Symbol called with 0 arguments; 1 argument is expected. >>
Transpose::nmtx: The first two levels of the one-dimensional list {{{-1256.89,1428.53,3.95091},{-1244.34,1494.18,4.58655},{-1217.63,1556.05,4.9574},{-1188.06,1615.99,5.09292},{-1168.71,1679.1,5.02187},<<5>>,{-995.382,2042.13,1.83892},{-963.726,2105.59,1.1125},{-931.068,2171.05,0.401884},{-892.135,2233.19,0.27072},{-848.104,2292.43,0.887201}},Symbol[]} cannot be transposed. >>
Interpolation::innd: First argument in Transpose[<<1>>] does not contain a list of data and coordinates. >>
Symbol::argx: Symbol called with 0 arguments; 1 argument is expected. >>

8. Jan 4, 2012

### vineelkanna

I need to get some thing like this

File size:
38.8 KB
Views:
111
9. Jan 4, 2012

### Bill Simpson

And where is dat defined and initialized? Not in the code you show. That appears to be the first source of errors.