Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Mathematica: ListPlot weirdness

  1. Aug 15, 2011 #1
    In Mathematica 7, I entered

    Code (Text):
    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?
     
  2. jcsd
  3. Aug 15, 2011 #2
    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]]
     
  4. Aug 15, 2011 #3

    Dale

    Staff: Mentor

    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
     
  5. Aug 15, 2011 #4
    How strange.

    Code (Text):
    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 (Text):
    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: Aug 15, 2011
  6. Aug 15, 2011 #5
  7. Aug 16, 2011 #6
    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 (Text):
    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.

     
  8. Aug 16, 2011 #7

    Dale

    Staff: Mentor

    You have specified the same option twice. It looks like it just took the first one instead of the second one.
     
  9. Aug 16, 2011 #8
    D'oh! Thanks, DaleSpam. Yeah, that was the trouble. This produces the diagram I wanted:

    Code (Text):
    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]
     
  10. Aug 17, 2011 #9

    Dale

    Staff: Mentor

    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.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook