Plotting centroidEnergy vs. distanceInAir with Mathematica ListPlot

In summary, the conversation involves the speaker trying to plot two lists of data in Mathematica and encountering errors. They discover a solution using the Thread[] function and discuss potential issues with the resulting plot. They then suggest using the PlotRange option to address the issue.
  • #1
cepheid
Staff Emeritus
Science Advisor
Gold Member
5,199
38
This didn't generate any responses in the software thread, and since this is for a homework assignment, I have moved my query here. Thanks.

Hello:

I have entered data into two lists in Mathematica:

centroidEnergy = {1.3485, 1.4780,
1.6176, 1.7820, 1.9123, 2.0274, 2.1635, 2.3163, 2.4213, 2.5191, 2.6515, \
2.7490, 2.8763, 2.9894, 3.0728, 3.1949, 3.2816, 3.3923, 3.4773, 3.5884, \
3.6804, 3.7702, 3.8374, 3.9679, 4.0507, 4.1507, 4.2163, 4.3145, 4.4039, \
4.5056, 4.5089, 4.6440, 4.7331, 4.8497, 4.9290, 5.0019, 5.0862, 5.1815, \
5.2427, 5.3365, 5.4225, 5.4800}


and

distanceInAir = {2.0523, 2.0020,
1.9517, 1.8964, 1.8511, 1.8008, 1.7505, 1.7002, 1.6499, 1.5996,
1.5493, 1.4990, 1.4488, 1.3984, 1.3481, 1.2979, 1.2476, 1.1520,
1.0967, 1.0464, 0.99606, 0.95079,
0.89546, 0.84516, 0.79486, 0.74456, 0.69426, 0.64396, 0.59366,
0.54336, 0.49306, 0.44276, 0.39247, 0.34217, 0.29186, 0.24157,
0.19269, 0.14097, 0.090671, 0.040371, 0.000132}

I am trying to plot the former vs. the latter. The recommended method I have seen to do so is to Transpose the two lists so that M. creates a list of ordered pairs of corresponding items from each one. Then, ListPlot that. However, I can't get the following command to work: it always gives the ensuing error message:

dataToPlot = Transpose[{distanceInAir, centroidEnergy}]

Transpose :: nmtx :: the first two levels of the one-dimensional list {{2.0523, 2.0020, 1.9517, 1.8964, 1.8511, 1.8008, 1.7505, <<28>> , 0.24157, 0.19269, 0.14097, 0.090671, 0.040371, 0.000132}, {<<1>>}} cannot be transposed.

?

Any ideas on how to simply plot centroidEnergy vs. distanceInAir would be greatly appreciated! Thanks.
 
Last edited:
Physics news on Phys.org
  • #2
Try the Thread[] function.

Thread[{{1, 2, 3}, {a, b, c}}]

returns

{{1, a}, {2, b}, {3, c}}

--J
 
  • #3
Thanks! That actually worked...once I discovered that I had omitted a data point in one of the lists...so one was shorter than the other...grr. :smile:

Any idea why the resulting plot in Mathematica would have x and y axes intersecting at some stupid random point instead of at (0,0)?

Edit: When I set AxesOrigin -> {0,0}, the resulting y-axis has a huge gap in it! Why?

Edit2: from the look of things, it's refusing to draw the y-axis below the y-value of the last data point. That's ridiculous! Any idea how to get around it?
 
Last edited:
  • #4
The PlotRange option might help.

ListPlot[{...}, PlotRange->{y0, yf}]

--J
 

1. What is the purpose of plotting centroidEnergy vs. distanceInAir with Mathematica ListPlot?

The purpose of plotting centroidEnergy vs. distanceInAir with Mathematica ListPlot is to visualize the relationship between these two variables and analyze any patterns or trends that may exist. This can help in understanding the behavior of the data and making informed decisions based on the plotted data.

2. How do I create a ListPlot in Mathematica?

To create a ListPlot in Mathematica, you can use the ListPlot function and provide the data points as input. For example, ListPlot[{1,2,3,4}] will create a simple plot with the data points {1,2,3,4} on the y-axis and their corresponding positions on the x-axis.

3. Can I customize the appearance of the ListPlot?

Yes, you can customize the appearance of the ListPlot by using various options such as changing the plot style, adding labels and titles, adjusting the axes, and more. You can refer to the Mathematica documentation for a complete list of available options.

4. How can I add error bars to the ListPlot?

You can add error bars to the ListPlot by using the ErrorBar function and providing the error values as input. For example, ErrorBar[{0.1, 0.2}] will add error bars of length 0.1 and 0.2 to the corresponding data points in the ListPlot.

5. Is it possible to add a trendline to the ListPlot?

Yes, you can add a trendline to the ListPlot by using the LinearModelFit or NonlinearModelFit functions and then using the Show function to overlay the trendline on the ListPlot. These functions use statistical methods to fit a line or curve to the data points and provide the necessary information for plotting the trendline.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
8K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
Back
Top