Tell me more ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

My question is that..

a=Range[-3,3];  
ListPlot[Sin[a]]

It plots a sin function from -3 to 3 by stepsize 1.
I want to show my graph other than the above, for example from -4 to 4.
Of course there dose not exist data, just want to change the range of x-axis.

I've already searched DataRange.
But I think it's just thing like scaling.

For example..

data = Table[Sin[x], {x, 0, 10, 0.1}];  
ListPlot[data]
ListPlot[data, DataRange -> {0, 1}]

I want to graph the function, of which domain is [-3,3], from arbitrary to arbitrary.
If I set my range [-4,4], my graph has not data on [-4,-3]&[3,4].
If I set my range [-2,2], my graph is chopped.

DataRange is not working properly. It's just scaling..

P.S - I know PlotRange is for y-axis. Here I need how to changing x-axis.

share|improve this question
7  
Look up PlotRange in documentation. – Vitaliy Kaurov Oct 13 '12 at 6:05
Voted to close as TL, as it's answered in the docs as Vitaliy already suggested – belisarius Oct 13 '12 at 6:42
I've already searched DataRange. I find this hard to believe since plotRange is right there in the 'SEE ALSO' in the same help page for DataRange. – Robert H Oct 13 '12 at 7:08
Thx. :) I've found solution. PlotRange->{{-4,4},All}. But is it right that DataRange is just scaling axies, not changing the range? – lachis83 Oct 13 '12 at 9:19

closed as too localized by belisarius, rcollyer, jVincent, whuber, Oleksandr R. Oct 13 '12 at 18:53

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, see the FAQ.

1 Answer

The first few steps in a new programming language can often be uncertain as we uncover the, perhaps different, approaches the language uses.

Plot

This is the simplest method is to use Plot, which plots a given function over a range of values.

Plot[Sin[x], {x, -3, 3}, PlotRange -> {{-4, 4}, Automatic}]

Mathematica graphics

This gives you a continuous line plotted between the values -3 and 3, this range is controlled via the {x,-3,3} element of the function call. This treats x as a local variable and varies it across the values given.

PlotRange -> {{-4, 4}, Automatic} controls the range of values to be included in the axis of the plot. The basic form used here is {xRange,yRange}. Automatic is a short form to tell Mathematica to choose what it thinks is the most appropriate range of coordinates. Alternatively an explicit range could have been given or the short form Full.

ListPlot

The approach you've opted for is to generate a list of data and then plots those values. You reasonably thought that DataRange might be what you need. But as you can see it produces a plot with the data you have defined in a laid across the data range you specify. As you observe it is a type of scaling.

ListPlot[Sin[a], DataRange -> {-4, 4}]

Mathematica graphics

Using PlotRange does not achieve the result you want either:

ListLinePlot[Sin[a], PlotRange -> {{-4, 4}, {-1, 1}}]

Mathematica graphics

I've used ListLinePlot to make it a little clearer where the plot region is defined.

The reason that this fails to produce the output you need is because plot functions, like ListPlot, that are designed to work on lists treat the plot range for the x coordinate range as list indices, not as function values. There are no values in your data in a at indices -4, -3, -2, 1 or zero, so you see only the plot of your data at a[[1]], a[[2]], a[[3]] and a[[4]].

Custom Ticks

Here is a tortuous solution that gets the result you want by using a combination of custom tick specifications and axes shifting.

Create some custom x-axis tick specifications of the form {x-coord,text}:

ticks = With[{ts = Range[-4, 4]}, Transpose[{Range[0, 8], Map[ToString, ts]}]]

{{0, "-4"}, {1, "-3"}, {2, "-2"}, {3, "-1"}, {4, "0"}, {5, "1"}, {6, "2"}, {7, "3"}, {8, "4"}}

Then plot your data with a shifted set of axes and the custom ticks:

plot = ListLinePlot[Sin[a], Ticks -> {ticks, Automatic}, 
                   PlotRange -> {{0, 8}, {-1, 1}}, AxesOrigin -> {4, 0}]

Mathematica graphics

PlotRangePadding

Something similar could be achieved using extending the range of the x-axis using PlotRangePadding, but because it still uses a list based function for plotting custom ticks are still required.

plot = ListLinePlot[Sin[a], PlotRangePadding -> 1, 
  Ticks -> {ticks, Automatic}, AxesOrigin -> {4, 0}]

Mathematica graphics

share|improve this answer

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