6
$\begingroup$

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?

$\endgroup$
1
  • $\begingroup$ 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]. $\endgroup$
    – Gabriel
    Oct 14, 2012 at 7:27

2 Answers 2

7
$\begingroup$

------- 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

$\endgroup$
3
  • 1
    $\begingroup$ I think he simply needs DataRange... $\endgroup$
    – rm -rf
    Oct 14, 2012 at 9:00
  • $\begingroup$ @rm-rf excellent, slipped my mind :) If you post it as your answer I will remove my edit. $\endgroup$ Oct 14, 2012 at 9:16
  • 1
    $\begingroup$ Nah, it's ok... You have a full answer with other details, so it's better in yours :) $\endgroup$
    – rm -rf
    Oct 14, 2012 at 14:22
2
$\begingroup$

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

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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