Suzuki Soma
Suzuki Soma

Reputation: 539

Strange... What does [::5,0] mean

I found a webpage which explaining how to use set_xticks and . set_xticklabels.

And they set set_xticks and 'set_xticklabels' as following...

ax.set_xticks(xx[::5,0])
ax.set_xticklabels(times[::5])
ax.set_yticks(yy[0,::5])
ax.set_yticklabels(dates[::5])

What does [::5,0] exactly mean..

I don't have any idea.....

Upvotes: 4

Views: 241

Answers (2)

Anand S Kumar
Anand S Kumar

Reputation: 90979

For a numpy array, the notation[::5,6] means to take the 6th column for that array and then in the 6th column, every 5th row starting at the first row till the last row.

Example -

In [12]: n = np.arange(100000)
In [17]: n.shape = (500,200)

In [18]: n[::1,2]
Out[18]:
array([    2,   202,   402,   602,   802,  1002,  1202,  1402,  1602,
        1802,  2002,  2202,  2402,  2602,  2802,  3002,  3202,  3402,
        3602,  3802,  4002,  4202,  4402,  4602,  4802,  .....])

In [19]: n[::5,2]
Out[19]:
array([    2,  1002,  2002,  3002,  4002,  5002,  6002,  ...])

Reference on numpy array slicing here , if you are interested.

Upvotes: 10

pvg
pvg

Reputation: 2729

This is a combination of python slicing, as described here:

https://docs.python.org/2.3/whatsnew/section-slices.html

and 'advanced slicing', which is a further extension that works with numpy arrays, as documented here:

http://docs.scipy.org/doc/numpy/reference/arrays.indexing.html

You probably want to skim through both a few times before it makes sense, although it's relatively straightforward. The answer by Anand S Kumar above explains the specific case you're asking about.

Upvotes: 1

Related Questions