1
$\begingroup$

I'm trying to write some automated data analysis code for my research group, but I've run into a snag with Mathematica which I just can't seem to iron out.

We generate data of the form:

{{0,-2,-1,0,1,2...},
 {380,1,2,3,4,5...},
 {384,6,7,8,9,10...},
 .
 .
 .
} 

Where, if the matrix is called data, data[[1,2;;]] is a selection of Voltage values, and data[[2;;,1]] is a selection of Wavelength values. data[[2;;,2;;]] gives Intensity.

I know that the wavelength values are defined by the instrument, but the voltage sweep is user-defined, so its dimensions are not fixed.

In MatLab, I would do the following:

a=size(data);
hold on
for i=2:a(2)
    plot(wavelength,data(2:end,i))
end

and this would generate my desired result of a single plot, with multiple traces each corresponding to a given measurement.

In Mathematica, I've tried several methods each without success. Because ListLinePlot expects a list of form {x1,y1},{x2,y2},... I figure there must be some obvious way to treat the data to achieve the desired result, but I haven't found it. If there were an equivalent hold on function in Mathematica, I would do the following:

HoldOn
Do[
ListLinePlot[{Wavelength,data[[2;;,t]]}//Transpose],
{t,2,Dimensions[data][[2]]}
]

But as far as I can find, such a function doesn't exist.

Is there some slick way I can do this that I'm missing?

Thanks for reading

$\endgroup$

1 Answer 1

4
$\begingroup$

If I understand what you mean, then the following might work (note that I have added an extra line of fake data):

data = {
   {  0, -2, -1,  0,  1,  2},
   {380,  1,  2,  3,  4,  5},
   {384,  6,  7,  8,  9, 10},
   {386, 11, 12, 13, 14, 15}};

ListLinePlot[
  Transpose@data[[2 ;;, 2 ;;]],
  DataRange -> MinMax@data[[2 ;;, 1]],
  PlotLabels -> data[[1, 2 ;;]]
]

plots

Here DataRangeis the key element to the solution: it specifies what range of actual coordinates the data should be assumed to occupy, so you do not need to provide a list of ${x, y}$ coordinate pairs, but simply a list of $y$ coordinates, and then the abscissae are reconstructed for you.

This also makes no assumptions on the size of the data set; it simply expects the first column to contain wavelengths to set the DataRange; the first row is used as labels for each plot line.

$\endgroup$
1
  • $\begingroup$ Thanks, this is basically what I was looking for! $\endgroup$ Commented Nov 25, 2019 at 23:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.