How can I get my ListPlot to go from {x,-10,10} on x?

  • Mathematica
  • Thread starter Freya
  • Start date
In summary: That sounds like your list has been transposed. So instead of {{x1,y1},{x2,y2},...} your list goes {{x1,x2,...},{y1,y2,...}}.In summary, ListPlot is not printing the correct graphs when list pairs are used.
  • #1
Freya
15
1
I have some code that is basically taking a range of energies, putting them into some recurrence equations and solving, and then I want to plot the results.

Code:
\[HBar] = 1;
m = 1;

Do[Energy[z] = 0.5 + 5 z, {z, 0, 14}]
Table[Energy[z], {z, 0, 14}];
Do[Entot[n, o] = Energy[o] - \[HBar]*\[Omega]*n, {n, -10, 10}, {o, 0, 
  14}]
Do[k[n, o] = 2*m*(Entot[n, o])^(1/2), {n, -10, 10}, {o, 0, 14}]
\[Omega] = 1;
s = 10;
k0 = 10;
\[Alpha] = 10;
t0 = 1;
t[-11] = 0.01;
t[11] = 0.01;
r[-11] = 0.01;
r[11] = 0.01;
ClearAll[plot1]
Do[eqn[n, o] = -t[n] + 
    KroneckerDelta[n, 
     1] + (s*m/(2*\[HBar]^2*I*k[n, o]))*(t[n - 1] + 
       t[n + 1]), {n, -10, 10}, {o, 0, 14}];
tab1 = Table[eqn[n, o], {o, 0, 14}, {n, -10, 10}];
tab2 = Table[t[n], {n, -10, 10}];

Do[plot1[p] = NSolve[tab1[[p]] == 0, tab2], {p, 1, 15}]
Do[f[n, o] = t[n] /. plot1[o], {n, -10, 10}, {o, 1, 15}]
ListPlot[Abs[
  Table[Flatten[Table[f[n, o], {n, -10, 10}]], {o, 1, 15}]], 
 PlotRange -> {0, 1.5}, PlotLegends -> Automatic]

At the moment, the ListPlot at the bottom takes the index of the list, so it goes from 0 to 21. But really, this should go from -10 to 10, following the iteration of n. I have tried everything it seems, from making my table go like [{n,f[n,o]},{n,-10,10}] but this doesn't work. I've tried making a separate list of n's and then transpose this with the f's, but as f is going over o as well, I think the length discontinuity is causing issues.

Any help would be much appreciated, and apologies if my code's a mess, I'm very much a rookie.

Thanks!
 
Physics news on Phys.org
  • #2
Can you create list pairs? The examples I saw of listplot adjusted the x-axis when list pairs were used.
 
  • #3
jedishrfu said:
Can you create list pairs? The examples I saw of listplot adjusted the x-axis when list pairs were used.
Thanks for your reply!

I tried that but due to my list being effectively a list with 21 elements, with 15 in each element, it can't be done simply it seems, I think I need some tricky syntax
 
  • #4
Freya said:
I have tried everything it seems, from making my table go like [{n,f[n,o]},{n,-10,10}]
This is the right way to do it. What error did you get when you tried this?

Try just one fixed value of o first.
 
  • #6
Dale said:
This is the right way to do it. What error did you get when you tried this?

Try just one fixed value of o first.
It doesn't get an error per say, but the graph it spits out is wrong, if I look at just one o I get a 2 sets of data plotted (or so it seems) one that's a straight line going from -10 to 10 in y (no idea why) and then roughly the right shape of the graph for f, but x now goes from 0 to 42.
 
  • #7
List pairs are:

(-3,9), (-2,4), (-1,1),(0,0), (1,1),(2,4),(3,9)...

ListPlot will then adjust the X-axis to match the axes to range of x values and the range of y values which in this case is
x: -3 to 3 and y:0 to 9
 
  • #8
jedishrfu said:
List pairs are:

(-3,9), (-2,4), (-1,1),(0,0), (1,1),(2,4),(3,9)...

ListPlot will then adjust the X-axis to match the axes to range of x values and the range of y values which in this case is
x: -3 to 3 and y:0 to 9
I've been trying to do this but I think the issue is my code isn't quite {x1,y1},{x2,y2} etc, I have several values for each x
 
  • #9
Freya said:
It doesn't get an error per say, but the graph it spits out is wrong, if I look at just one o I get a 2 sets of data plotted (or so it seems) one that's a straight line going from -10 to 10 in y (no idea why) and then roughly the right shape of the graph for f, but x now goes from 0 to 42.
That sounds like your list has been transposed. So instead of {{x1,y1},{x2,y2},...} your list goes {{x1,x2,...},{y1,y2,...}}.
 
  • #10
Dale said:
That sounds like your list has been transposed. So instead of {{x1,y1},{x2,y2},...} your list goes {{x1,x2,...},{y1,y2,...}}.
I may be wrong, but I don't think that's my problem, I think it may be further back in my code as when I print values of f, they are already in a list, I'll play around and see what I can find
 
  • #11
It is easy enough to check. Just make a new variable and set it equal to the value sent to ListPlot. I bet it isn't shaped right.
 

1. How can I change the range of my ListPlot on the x-axis?

To change the range of the ListPlot on the x-axis, you can use the PlotRange option in the ListPlot function. Set the desired range using the format {xmin, xmax}.

2. Can I specify a specific starting and ending point for my ListPlot on the x-axis?

Yes, you can specify a starting and ending point for your ListPlot on the x-axis by using the PlotRange option in the ListPlot function. Simply set the desired range using the format {xmin, xmax}.

3. How can I make my ListPlot start at a negative value on the x-axis?

To make your ListPlot start at a negative value on the x-axis, you can use the PlotRange option in the ListPlot function. Set the desired starting value using the format {xmin, xmax} where xmin is the negative value you want the plot to start at.

4. Is it possible to have a ListPlot that goes beyond the specified range on the x-axis?

Yes, it is possible to have a ListPlot that goes beyond the specified range on the x-axis. This can be done by using the PlotRange option in the ListPlot function and setting the desired range using the format {xmin, xmax}. However, the plot will only display up to the specified range, any data points outside of the range will not be shown.

5. Can I use non-numeric values for the range of my ListPlot on the x-axis?

No, the range of a ListPlot on the x-axis must be specified using numeric values. Non-numeric values will result in an error. If you want to label the x-axis with non-numeric values, you can use the Ticks option in the ListPlot function to specify custom tick marks and labels.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
219
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
Replies
1
Views
327
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
Replies
0
Views
415
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
7K
  • Calculus and Beyond Homework Help
Replies
1
Views
600
  • Introductory Physics Homework Help
Replies
15
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
903
Back
Top