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

In summary, to plot a 3D matrix as a 2D plot in Mathematica, use the ListPlot3D function. To add a third dimension as a color, use the ColorFunction option. The color scheme can be customized with the ColorData function. The viewing angle can be changed with the ViewPoint, ViewVertical, and ViewAngle options. Labels and a legend can be added with the PlotLabel and PlotLegends options. Additional customization can be done with options like LabelStyle and LegendLayout.
  • #1
Muchacho
3
0
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:
Physics news on Phys.org
  • #2
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]

To plot your colored curve.
 
  • #3
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
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
vineelkanna said:
Im getting an errors if I used that code.

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
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
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
I need to get some thing like this
 

Attachments

  • Picture 3.png
    Picture 3.png
    11.7 KB · Views: 711
  • #9
vineelkanna said:
Just to test, I took the same values and code from above. This is the code:...dat[[All, i]]...

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

1. How can I plot a 3D matrix as a 2D plot in Mathematica?

To plot a 3D matrix as a 2D plot in Mathematica, you can use the ListPlot3D function. This function takes in a matrix as its argument and plots it as a 3D surface. By default, the x and y coordinates of the points in the matrix will correspond to the indices of the matrix, while the z coordinate will correspond to the values in the matrix.

2. How do I add a third dimension as a color in my 2D plot?

To add a third dimension as a color in your 2D plot, you can use the ColorFunction option in the ListPlot3D function. This option allows you to specify a function that will assign a color to each point in the plot based on its z coordinate. You can also use the ColorData function to choose from a variety of predefined color schemes.

3. Can I customize the color scheme for my 3D matrix plot?

Yes, you can customize the color scheme for your 3D matrix plot by using the ColorFunction option in the ListPlot3D function. You can also use the ColorData function to create your own custom color scheme.

4. How can I change the viewing angle of my 3D matrix plot?

The viewing angle of a 3D matrix plot can be changed by using the ViewPoint and ViewVertical options in the ListPlot3D function. These options allow you to specify the location and direction of the viewpoint for the plot. You can also use the ViewAngle option to adjust the zoom level of the plot.

5. Is it possible to add labels and a legend to a 3D matrix plot?

Yes, it is possible to add labels and a legend to a 3D matrix plot in Mathematica. You can use the PlotLabel option to add a label to the plot, and the PlotLegends option to add a legend. You can also customize the appearance of the labels and legend by using additional options such as LabelStyle and LegendLayout.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
Back
Top