Mathematica Mathematica: ListPlot weirdness

  • Thread starter Thread starter Rasalhague
  • Start date Start date
  • Tags Tags
    Mathematica
AI Thread Summary
In Mathematica 7, a user encountered issues with plotting points using ListPlot, specifically with the display of points and axis scaling. The initial plot only showed four points instead of the expected eight, and the aspect ratio settings did not yield equally scaled axes. Suggestions included using Automatic for aspect ratio and defining custom ticks to ensure all integers were labeled on both axes. The user successfully adjusted the axes to cross at (0,0) and ensured all points were visible by modifying the plot style and tick settings. A mistake in specifying the Ticks option twice led to confusion, but once corrected, the desired plot was achieved. The discussion highlighted the importance of understanding option precedence in Mathematica, where the first specified option is used when duplicates are present.
Rasalhague
Messages
1,383
Reaction score
2
In Mathematica 7, I entered

Code:
u = {1, 2, 3, 5, 7, 8, 10, 11}; v = {1, 2, 4, 5, 4, 3, 1, 1}; w = 
 Transpose[{u, v}]; ListPlot[w, PlotRange -> All, 
 AspectRatio -> 1/1]

I wanted this to display a dot at each of the 8 points of w,

{{1, 1}, {2, 2}, {3, 4}, {5, 5}, {7, 4}, {8, 3}, {10, 1}, {11, 1}},

and to show them on a diagram whose axes have the same scale. Instead, it displays a dot at these 4 points only

{{2, 4}, {5, 5}, {7, 4}, {8, 3}},

and the gaps between integers are much smaller on the horizontal axis. Replacing 1/1 with Automatic produces axes of a similar scale, but they look untidy because only every other integer is labelled on the horizonal axis, whereas every integer is labelled on the vertical axis. Replacing All with Full makes no visible difference.

The axes are at least long enough to accomadate all of the correct points: the vertical one goes up to 5, the horizontal to 11.

Why doesn't it plot the right points? Why does it not plot the full amount of points? Why doesn't 1/1 give equally scaled axes? How can I plot all of the right points on equally scalled axes with all integers labelled on both axes?
 
Physics news on Phys.org
I believe your points are actually there, they just aren't obvious.

This should expose them

u = {1, 2, 3, 5, 7, 8, 10, 11}; v = {1, 2, 4, 5, 4, 3, 1, 1}; w = Transpose[{u, v}]; ListPlot[w, PlotRange -> All, AspectRatio -> 1/1, PlotStyle -> PointSize[0.05]]
 
I don't get the behavior you get. For me it plots all of the points.

As you say, if you want the scale to be the same you need to use Automatic instead of 1.

If you want to force some specific tick marking you can e.g. define:
ticks[min_, max_] := Range[Ceiling[min], Floor[max]]
and then use the option
Ticks->ticks
 
How strange.

Code:
ListPlot[w, PlotRange -> All, AspectRatio -> Automatic, 
 PlotStyle -> PointSize[0.05]]

This produces a full set 8 big dots. It looks as if the axes actually cross at (2,1), in which case the dots are in the right places after all. How can I get the axes to cross in the standard place, at (0,0)? And is the a way to specify the scales numerically?

Code:
ticks[min_, max_] := Range[Ceiling[min], Floor[max]]; ListPlot[w, 
 PlotRange -> All, AspectRatio -> Automatic, 
 PlotStyle -> PointSize[0.025], Ticks -> ticks]

This labels all integers along the horizontal and vertical axes except at the quirky origin, (2,1).
 
Last edited:
Thanks! So, that's got the axes to cross at (0,0), or wherever I might want, and now I know how to force the dots to be the same size and all visible. DaleSpam's suggestion worked for the ticks, but I wonder why this alternative doesn't:

Code:
u = {1, 2, 3, 5, 7, 8, 10, 11}; v = {1, 2, 4, 5, 4, 3, 1, 1}; w = 
 Transpose[{u, v}]; hori = 
 Transpose[{Table[i, {i, 11}], Table[i, {i, 11}]}]; vert = 
 Transpose[{Table[i, {i, 5}], Table[i, {i, 5}]}]; ListPlot[w, 
 PlotStyle -> PointSize[0.01], Ticks -> Automatic, 
 AxesOrigin -> {0, 0}, Ticks -> {hori, vert}]

Shouldn't that put ticks at every unit from 1 to 11 on the horizontal axis, and label them in the natural way: 1, 2, 3,... ,11? (Instead, it puts ticks at intervals of 0.5, and labels them 1, 2, 3, etc. on the vertical, but 2, 4, 6, etc. on the horizontal.

{{x1,label1},{x2,label2},...} tick marks drawn with the specified labels
 
Rasalhague said:
Thanks! So, that's got the axes to cross at (0,0), or wherever I might want, and now I know how to force the dots to be the same size and all visible. DaleSpam's suggestion worked for the ticks, but I wonder why this alternative doesn't:

Code:
u = {1, 2, 3, 5, 7, 8, 10, 11}; v = {1, 2, 4, 5, 4, 3, 1, 1}; w = 
 Transpose[{u, v}]; hori = 
 Transpose[{Table[i, {i, 11}], Table[i, {i, 11}]}]; vert = 
 Transpose[{Table[i, {i, 5}], Table[i, {i, 5}]}]; ListPlot[w, 
 PlotStyle -> PointSize[0.01], [B]Ticks -> Automatic[/B], 
 AxesOrigin -> {0, 0}, [B]Ticks -> {hori, vert}[/B]]

Shouldn't that put ticks at every unit from 1 to 11 on the horizontal axis, and label them in the natural way: 1, 2, 3,... ,11? (Instead, it puts ticks at intervals of 0.5, and labels them 1, 2, 3, etc. on the vertical, but 2, 4, 6, etc. on the horizontal.
You have specified the same option twice. It looks like it just took the first one instead of the second one.
 
D'oh! Thanks, DaleSpam. Yeah, that was the trouble. This produces the diagram I wanted:

Code:
u = {1, 2, 3, 5, 7, 8, 10, 11}; v = {1, 2, 4, 5, 4, 3, 1, 1}; w = 
 Transpose[{u, v}]; hori = 
 Transpose[{Table[i, {i, 11}], Table[i, {i, 11}]}]; vert = 
 Transpose[{Table[i, {i, 5}], Table[i, {i, 5}]}]; ListPlot[w, 
 PlotStyle -> PointSize[0.01], AxesOrigin -> {0, 0}, 
 Ticks -> {hori, vert}, AspectRatio -> Automatic]
 
No worries. Actually, I learned something new from the mistake. It seemed strange to me that it silently used the first option. I had never observed that behavior, and I would have expected it to either throw an error or to use the last value. But it seems very consistent; if you have a sequence of options in which one option is defined multiple times it always seems to use the first one. I think it has to do with the behavior of the built-in function OptionValue which is used in programming functions with options.
 

Similar threads

Replies
2
Views
2K
Replies
2
Views
1K
Replies
1
Views
2K
Replies
3
Views
2K
Replies
18
Views
4K
Replies
7
Views
2K
Back
Top