Plotting Nested Lists in Mathematica

In summary, the conversation discusses a problem with plotting nested lists and the need to plot two data sets (A and B) with dimensions 5x50. The expert suggests transposing the data or using Table and Part to get the desired results. The conversation ends with gratitude from the original poster for the helpful solution.
  • #1
ChristinaJ
Gold Member
35
1
All,

I am having trouble plotting nested lists.

I have two data sets (A and B), both with dimensions 5x50.

So far I have

plot = Map[
ListPlot[#, PlotRange -> {0, Max[A]},
AxesOrigin -> {0, 0}, Frame -> True,
FrameLabel -> {"position", "Intensity"}] &, A];

which gives me 5 plots of 50 points each. What I need is to also plot data B together with data A, ending with 5 plots each having two lines of 50 points.

Each plot is ListPlot[An,Bn] where n=1..5 but I just can't figure it out.

Many thanks for any help and apologies for trespassing upon your time.

Christina
 
Physics news on Phys.org
  • #2
I think that you just need to transpose your data:

Code:
A = Accumulate /@ RandomReal[{0, 2}, {5, 50}];
B = Accumulate /@ RandomReal[{0, 1}, {5, 50}];

ListPlot[#, PlotRange -> {0, Max[A]}, AxesOrigin -> {0, 0}, 
   Frame -> True, Joined -> True, 
   FrameLabel -> {"position", "Intensity"}] & /@ Transpose[{A, B}]

attachment.php?attachmentid=34374&stc=1&d=1302822454.png


You can also get the same results using Table and Part ( [[ ]] )

Code:
Table[ListPlot[{A[[i]], B[[i]]}, PlotRange -> {0, Max[A]}, 
  AxesOrigin -> {0, 0}, Frame -> True, Joined -> True, 
  FrameLabel -> {"position", "Intensity"}], {i, 1, 5}]
 

Attachments

  • Untitled-2.png
    Untitled-2.png
    8.7 KB · Views: 862
  • #3
It worked perfectly, just what I was looking for.

Many thanks for your help.

Christina
 

Related to Plotting Nested Lists in Mathematica

1. What is a nested list in Mathematica?

A nested list in Mathematica is a list that contains other lists as its elements. These sublists can also contain further sublists, creating a hierarchical structure. This is useful for organizing and manipulating complex data in a structured way.

2. How do I create a nested list in Mathematica?

To create a nested list, you can use curly brackets to enclose the elements of the outer list, and then use additional curly brackets to enclose the elements of the inner lists. For example: list = {{1,2,3}, {4,5,6}, {7,8,9}} will create a nested list with three sublists.

3. How do I access elements in a nested list?

You can access elements in a nested list by using double square brackets and specifying the index of the sublist, followed by the index of the element within that sublist. For example, list[[2,3]] will return the third element in the second sublist.

4. Can I use functions on nested lists in Mathematica?

Yes, you can use various functions on nested lists in Mathematica. For example, you can use the Map function to apply a function to each element in the nested list, or you can use the Flatten function to flatten a nested list into a single list.

5. How can I plot nested lists in Mathematica?

To plot a nested list in Mathematica, you can use the ListPlot function. However, since nested lists have a hierarchical structure, you may need to use the Flatten function to convert it into a format that is compatible with the ListPlot function.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
331
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
18
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
6K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
963
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
Back
Top