Changing the x-axis origin for ListPlot in Mathematica

December 12th, 2007 | Categories: math software, mathematica | Tags:

Recently, someone over at comp.soft-sys.math.mathematica had a simple problem – he had a 1d array of y-values which he wanted to plot. Easy enough – if his array is in a variable called dataset all he would need to do is

Listplot[dataset]

which would create a plot of x-y values with the x values being 1,2,3….. and so on. The problem the poster had was that he wanted the x-axis to start at 100 rather than the default which is 1.

Several people, including me, tried to be helpful and posted solutions that were along the lines of ‘create an explicit set of x-values, starting at 100, do some trickery to turn the two arrays into a set of x,y pairs and plot the result’. For example you could do this with

(*Assume that the array is in a variable called dataset*)
x = Range[100, 100 + Length[dataset] - 1];
ListPlot[Transpose@{x, dataset}]

This works fine but we should have read the new version 6 documentation because (as pointed out by someone else on the forum) there is now a much easier way of doing this using the DataRange option.

ListPlot[dataset, DataRange -> {100, 100 + Length[dataset]}]
No comments yet.