I need to do density plot and contour plot of Raman spectra with a specific peak, Raman mode, as a way to study the intensity distribution of such peak throughout the sample used. As such frequencies are generally associated with particular functional groups (see the table here), this can give information about the spatial distribution of these groups within a sample. My trouble is that I have this data in a different text file for each grid point, with names like


Inside each file is a two-dimensional table of data giving the frequency and intensity for the spectrum collected there. How do I combine this data? Once I have combined it, how do I plot it? I am hoping to present the data in a similar for to the figure below,

enter image description here

This is my data from my dropbox account.

  • 7
    $\begingroup$ Nadiah, please realize that the question is much too vague to be answered as it stands and may be closed. You may want to edit your question to include some sample raw data, and perhaps an example of the output you seek (e.g. a picture from a textbook, a web link). $\endgroup$
    – MarcoB
    Commented Feb 22, 2016 at 14:15
  • 1
    $\begingroup$ There is no question in your post. On this site we answer specific questions. We do not hold discussions on vaguely described broad topics. $\endgroup$
    – m_goldberg
    Commented Feb 22, 2016 at 15:32
  • 3
    $\begingroup$ Beside the valid remark of @m_goldberg, an intensity distribution of a Raman mode (in real space, I suppose?) will have little to do with the physics of a material, and more with the sample quality across its surface. Otherwise, a Raman spectrum is a single-valued function of a single variable and is not suitable for density/contour plots. $\endgroup$
    – LLlAMnYP
    Commented Feb 22, 2016 at 15:46
  • 1
    $\begingroup$ thank you for your responses, I am agree with you ,let me change my question. $\endgroup$ Commented Feb 23, 2016 at 10:46
  • 2
    $\begingroup$ @LLlAMnYP - It sounds like OP is describing a form of Raman microscopy, and if you look only at the frequency ranges, for example, of a carbonyl stretch, it could show the regions where oxygen impurities are located. $\endgroup$
    – Jason B.
    Commented Feb 23, 2016 at 11:21

1 Answer 1


The first thing you need to do is to extract the x and y value from the filename, that's what the StringCases is doing below. Then you want to import the data from the text files and prepend the x and y values to each element of the spectrum, that's what the Join is doing.

filenamelist = FileNames["*.txt"];
import[fn_] := 
       "X_" ~~ a__ ~~ "__Y_" ~~ b__ ~~ "__Time" -> {a, b}], #] & /@ 
  Import[fn, "Table"]
data = Flatten[import /@ filenamelist, 1];

You can plot the entire data set at once using one of the 4-dimensional plotting functions, like ListDensityPlot3D or ListSliceDensityPlot3D

<< "http://pastebin.com/raw.php?i=sqYFdrkY"(*This imports my favorite color function :-)  *)
 ColorFunction -> ParulaCM, AxesLabel -> {"X", "Y", "ω"}]

enter image description here

ListSliceDensityPlot3D[data, {"ZStackedPlanes", 8}, 
 ColorFunction -> ParulaCM, AxesLabel -> {"X", "Y", "ω"}]

enter image description here

But these 4-dimensional plots (x, y, omega, and color) are hard to view information from, so I like to use 2D density plots personally. So let's split the data up into different lists for each frequency,

dataSR = GatherBy[data, #[[3]] &][[All, 
   All, {1, 2, 4}]];(*the spectrally-resolved raman maps*)
frequencies = 
 DeleteDuplicates[data[[All, 3]]]; (* a list of the frequencies *)

Now we can make a density plot for a single frequency,

ListDensityPlot[dataSR[[1]], ColorFunction -> ParulaCM, 
 PlotRange -> All, PlotLabel -> 
  Row[{"\[Omega] = ", frequencies[[1]], 
    "\!\(\*SuperscriptBox[\(cm\), \(-1\)]\)"}], 
 FrameLabel -> {"X", "Y"}, BaseStyle -> 16, ImageSize -> 400]

enter image description here

You notice that it is a bit blocky, and that is due to the fact that the spatial resolution is low. There are only 16 grid points in the x direction and 13 in the y direction.

You can interpolate the data to get a better plot if you like, but to do so you need to reshape the data into the form of an array,

{xrange, yrange, \[Omega]range, intensityrange} = 
  MinMax /@ Transpose[data];
ListDensityPlot[Transpose@Partition[dataSR[[1, All, 3]], 16], 
 ColorFunction -> ParulaCM, PlotRange -> All, InterpolationOrder -> 2,
  PlotLegends -> Automatic, 
 PlotLabel -> 
  Row[{"\[Omega] = ", frequencies[[1]], 
    "\!\(\*SuperscriptBox[\(cm\), \(-1\)]\)"}], 
 FrameLabel -> {"X", "Y"}, BaseStyle -> 16, ImageSize -> 400, 
 DataRange -> {xrange, yrange}]

enter image description here

I think that looks pretty decent, but you may prefer the non-interpolated form as an honest display of the data, up to you. You could also change the plotting scale, perhaps using a nonlinear scale to highlight weak features, or omit the PlotRange->All option so that the bright spot in the corner doesn't override the plot.

You can do a density plot for all the different frequencies and collect them in an image list,

imglist = 
  ListDensityPlot[Transpose@Partition[dataSR[[#, All, 3]], 16], 
     ColorFunction -> ParulaCM, PlotRange -> All, 
     InterpolationOrder -> 2, PlotLegends -> Automatic, 
     PlotLabel -> 
      Row[{"\[Omega] = ", frequencies[[#]], 
        "\!\(\*SuperscriptBox[\(cm\), \(-1\)]\)"}], 
     FrameLabel -> {"X", "Y"}, BaseStyle -> 16, ImageSize -> 400, 
     DataRange -> {xrange, yrange}] & /@ Range[Length@frequencies];

which can then be exported as a movie file, or you can scan through it manually using Manipulate

Manipulate[imglist[[n]], {{n, 1}, 1, Length@imglist, 1}]

enter image description here

As you can see from the movie, there isn't a lot of variation of the map with different frequencies, there are mostly just the three bright spots, but if you scan more slowly you can find interesting bits like this

enter image description here

Hope this helps.


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.