Mathematica: ListPlot weirdness

  • Mathematica
  • Thread starter Rasalhague
  • Start date
  • Tags
    Mathematica
It seems that it always returns the first value. I remember reading something about this, but I can't find it now. In any case, I'm glad you found your answer. Mathematica can be a fickle mistress, but she's always worth the effort.
  • #1
Rasalhague
1,387
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
  • #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]]
 
  • #3
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
 
  • #4
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:
  • #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:
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
 
  • #7
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][COLOR="Red"]Ticks -> Automatic[/COLOR][/B], 
 AxesOrigin -> {0, 0}, [B][COLOR="red"]Ticks -> {hori, vert}[/COLOR][/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.
 
  • #8
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]
 
  • #9
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.
 

1. What is ListPlot in Mathematica?

ListPlot is a function in Mathematica that allows you to create a graphical representation of a list of data points. It is commonly used for visualizing and analyzing data sets in various fields of science and engineering.

2. Why is ListPlot sometimes referred to as "weird"?

ListPlot can be considered "weird" because it has some unique features and behaviors that may seem unusual to someone who is not familiar with the software. For example, it automatically interpolates data points and can handle missing values, which may result in unexpected visualizations.

3. How do I customize the appearance of ListPlot?

ListPlot has many options for customizing the appearance of the plot, such as changing the color, style, and size of data points, adding labels and legends, and adjusting the axes and grid lines. These can be specified using the PlotStyle and PlotOptions functions.

4. Can I plot multiple data sets on the same graph using ListPlot?

Yes, ListPlot allows you to plot multiple data sets on the same graph by specifying them in a list within the function. You can also use the Show function to combine multiple ListPlot graphs into one.

5. Are there any alternative functions to ListPlot in Mathematica?

Yes, there are several other functions in Mathematica that can be used for visualizing data, such as ListLinePlot, ListLogPlot, and ListStepPlot. These functions have similar features to ListPlot but may offer different types of visualizations or customization options.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
715
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
7
Views
954
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
873
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
344
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
7
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
731
Back
Top