Search code examples
pythonindexingsliceellipsis

Can't understand a matplotlib's example where there are both ellipsis and colons probably associated with indices


I have a question about this matplotlib's example.

Here's the part that I don't understand

def update_line(num, data, line):
    line.set_data(data[...,:num])
    return line,

What does line.set_data(data[...,:num]) do?


Solution

  • It's a special syntax provided by numpy for slicing in multidimensional arrays. The general syntax is a[s1,s2, ... , sn], where si is the expression used for usual slicing or indexing sequences and defines desired slice in i'th dimension. For example, a[5,2:3,1::2].

    The ... is the shortening for getting full slices in all dimensions. For example a[...,3] is the shortening for a[:,:,3] if a is three-dimensional.