Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

RandomInteger[3,3] produces a vector of length-3 randomly. But it counts index from 1. I need a vector, index of which starts from 0. Because of this, when plotting it, my x-axis starts from 1. Look at the following.

ListPlot[RandomInteger[3, 3]]

So I've searched HELP about this, and I found the option about AxesOrigin. I tried out the following.

ListPlot[RandomInteger[3, 3], AxesOrigin -> {1, 0}]

Look at the values of the axis. It's just shifting the axis without adjusting index of the vector. It is starting from 1 still.

How can I count my vectors from the index of 0?

share|improve this question
    
It is not clear what you want. If you want to have Mathematica lists accept 0 based indexing you are out of luck. If you want to have your list include 0 values that is easy just do RandomInteger[{0,3},3]. –  Gabriel Oct 14 '12 at 7:27
    
Welcome to Mathematica.SE! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Read the FAQs! 3) When you see good Q&A, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. ALSO, remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign` –  Vitaliy Kaurov Oct 14 '12 at 7:53
add comment

2 Answers

------- Edit: @@rm-rf comment -------

@@rm-rf suggested a very good idea - this is basically an option to ListPlot which I missed - DataRange. In simplest terms this works:

ListLinePlot[RandomInteger[3, 3], DataRange -> {0, 2}] 

------- older answer -------

Whenever ListPlot makes graphics from a simplest list it interprets it as y-data and indexes x-data automatically starting from 1. But if you give it your own index - it will take it in account. You need to produce pairs of numbers where 1st in a pair number is your index. You can do it a number of ways, - a few equivalent examples:

data = Transpose[{Range[0, 2], RandomInteger[3, 3]}]

{{0, 3}, {1, 3}, {2, 3}}

data = Table[{k, RandomInteger[3]}, {k, 0, 2}]

{{0, 1}, {1, 1}, {2, 1}}

data = {#, RandomInteger[3]} & /@ Range[0, 2]

{{0, 3}, {1, 1}, {2, 1}}

It now will work:

ListLinePlot[data, AxesOrigin -> {0, 0}, 
  Mesh -> All, MeshStyle -> Directive[Red, PointSize[Large]]]

enter image description here

share|improve this answer
    
I think he simply needs DataRange... –  rm -rf Oct 14 '12 at 9:00
    
@rm-rf excellent, slipped my mind :) If you post it as your answer I will remove my edit. –  Vitaliy Kaurov Oct 14 '12 at 9:16
    
Nah, it's ok... You have a full answer with other details, so it's better in yours :) –  rm -rf Oct 14 '12 at 14:22
add comment

To achieve the result you want, you need to control both the PlotRange and the AxesOrigin. PlotRange controls which part of the x and y axis are display and AxesOrigin controls where the axes are placed.

ListLinePlot[RandomInteger[3, 3], 
              PlotRange -> {{0.9, 3.1}, {0, 3.1}},
              AxesOrigin -> {1, 0}, 
              PlotMarkers->Automatic]

Mathematica graphics

One point to note is that you are not changing the indices of the random vector you have created.

Mathematica stores data in lists whose indices start at 1, what you are modifying here is the visual appearance of the plot of that data.

The answer to your previous question gives some more information about plot ranges Set range of x axis

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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