The command ListPlot will plot lists with a unit step. Is there any way to change the step size of this?

  • $\begingroup$ check the time-stamps of the answers. I am sure you are glad you switched to the MMA SE site:) $\endgroup$ – kglr Feb 10 '12 at 0:02
  • $\begingroup$ Sure am! THank you so much! I hope to learn by self discovery, but you might see me again. Thank you for all your help! $\endgroup$ – Edwin Feb 10 '12 at 1:23

Lists don't save the value they have been generated with. Consider the example of plotting a sine using a discrete point set:

data = Table[Sin[2 Pi x], {x, 0, 1, 0.05}];

enter image description here

Notice the nonsense values on the $x$ axis.

There are three possible solutions for this, all of which give the following plot as a result:

enter image description here

Using DataRange to manually specify the domain of the data in ListPlot

data = Table[Sin[2 Pi x], {x, 0, 1, 0.05}];
ListPlot[data, DataRange -> {0, 2 Pi}]

The disadvantage of this approach is that the data range is not known to the plot, and it has to be entered by the user manually. If the data itself changes, the plot will still assume these manual numbers, even if the domain is now much larger (consider expanding the above table to run from 0 to 20).

Embedding the data range into the list

data = {
        Table[Sin[2 Pi x], {x, 0, 1, 0.05}],
        DataRange -> {0, 2 Pi}
ListPlot @@ data

Disadvantage: the DataRange option is only useful for a couple of functions. When you're planning to further modify the data, you will always have to pay attention that data doesn't save the data on the first level, but on the second one of this nested list.

Incorporating the x values when generating the data

data = Table[{2 Pi x, Sin[2 Pi x]}, {x, 0, 1, 0.05}];

The disadvantage of this approach is of course a more complicated list structure, as well as about twice the amount of data.

  • $\begingroup$ If you know that the data is at equidistant places, you could just make a two-component list: data={Table[Sin[x],{x,0,2 Pi,Pi/10}],DataRange->{0,2 Pi}} and then ListPlot[data[[1]], data[[2]]] or even just ListPlot@@data. $\endgroup$ – celtschk Feb 10 '12 at 16:35
  • $\begingroup$ @celtschk Thanks, I included it in my answer. $\endgroup$ – David Feb 10 '12 at 17:30

Two approaches:

  1. Use the option DataRange (in the Options section of ListPlot in the documentation center.

  2. ListPlot also takes a list of x,y pairs. So another option is transforming your input data (a list of y values) to a list of x,y pairs

  • $\begingroup$ I prefer the second one myself; I am at least confident that points are being plotted, and not just data values. $\endgroup$ – J. M. will be back soon Feb 10 '12 at 0:24
  • $\begingroup$ @J.M, me too. Especially with irregularly-spaced multiple lists using DataRange is not any simpler. $\endgroup$ – kglr Feb 10 '12 at 0:29

Perhaps you are looking for DataRange?

dat = RandomReal[5, 20];
ListLinePlot[dat, DataRange -> {1, 5}]

Mathematica graphics


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.