Mathematica Issue with ListPlot3D Mathematica

  • Thread starter Thread starter Maior272
  • Start date Start date
  • Tags Tags
    Mathematica
Click For Summary
The user is encountering an issue with ListPlot3D in Mathematica, which returns an empty plot despite having the correct dimensions for the data. They are processing 100 spectral data files, normalizing the spectrum against a white light spectrum, and storing the results in a three-dimensional table. The problem arises because ListPlot3D requires a flat list of coordinates, while the user's data is structured as a list of lists. The suggested solution is to use Flatten[data, 1] to convert the data into the appropriate format for plotting. This adjustment should resolve the plotting issue.
Maior272
Messages
1
Reaction score
0
Hi everybody,

I am relatively new to Mathematica, so please don't be to harsh on me regarding beginners' mistakes ;-)

I wanted to write a script, which evaluates and plots a series of 100 spectra. Therefore I have 100 spectral data files, which provide the spectrum in the first 1340 lines and background in the second 1340 lines. Additionaly I want to normalize the spectrum with a whitelightspectrum recorded in a second file.
Hence I want to calculate (Spectrum-Background)/whitelight for each value.
Therefore I calculate all the values using 2 interlaces for loops putting the values in a table, which works fine.
My problem appears in the very last step as I want to plot the data using ListPlot3D which only returns an empty box (however it has the right dimensions).

I would appreciate very much you help :-)

Here is my source code:

ClearAll["Global`*"]
numspectra = 100;
time = 0;
data = Table[datafunc, {i1, numspectra}, {i2, 1340}, {i3, 3}];
spectrum = Table[Import["...\#1_13_" <> ToString <> ".txt", "Data"], {i, numspectra}];
whitelight = Table[Import["...\white 100spec_53_" <> ToString <> ".txt", "Data"], {u, numspectra}];
ToExpression[spectrum]
ToExpression[whitelight]

For[h = 1, h <= numspectra, h++,
time = time + 1;
For[j = 1, j <= 1340, j++,
wavelength = Part[spectrum, h, j, 1];
If[Part[whitelight, h, j, 3] == 0, Part[whitelight, h, j, 3] = 1, Null]; (*Avoid division by 0*)
bgcorr = (Part[spectrum, h, j, 3] - Part[spectrum, h, 1340 + j, 3])/ Part[whitelight, h, j, 3];

Part[data, h, j, 1] = wavelength;
Part[data, h, j, 2] = time;
Part[data, h, j, 3] = bgcorr;
]
]
ListPlot3D[data]
 
Physics news on Phys.org
I think that what you need is:

ListPlot3D[Flatten[data,1]]

The problem is that data is a list of lists of coordinates (each coordinate being a list of 3 coordinates, in your case wavelength, time, and bgcorr). ListPlot3D expects a list of coordinates, not a list of lists of coordinates. Flatten flattens out the top level of the list.
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
5
Views
2K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 52 ·
2
Replies
52
Views
13K