1

Is it possible to find the parent array of a slice ie. the array that the slice is taken from? I would like to do this so I can add functionality to matplotlib plots which allows you to change which slice of an array you are viewing interactively in a plot. For instance, if I do this

plt.pcolormesh(myArray[0,:,:])

I would like to be able to run some code to change the plot to

plt.pcolormesh(myArray[1,:,:])

but to do that I need to know that myArray[0,:,:] is a slice of myArray.

Thanks Niall

1 Answer 1

4

With simple slices, you can look at the base attribute:

a = np.arange(50)
b = a[10:20]
print (b.base is a)

However, I don't believe that this is guaranteed to work in all circumstances...(depending on contiguousness of a, etc.)

Sign up to request clarification or add additional context in comments.

6 Comments

Basically, there is another problem with this. It could be that b.base is a.base and not b.base is a also. There is np.may_share_memory, which will tell you that the two must have some common base, but there is no way to find out if one is a slice of the other, since two arrays may be incompatible slices of the same array.
@seberg -- Precisely. This will only work in very simple circumstances. However, I think that it's a common enough simple circumstance to be worth mentioning.
Yeah, just wanted to warn, especially since there is a change with version 1.7. here. In his case, and if you also throw in strides information, you should be able to get quite far though.
Unfortunately, as helpful as this is, its doesn't seem to work with matplotlib.cm.get_array() - the base of that array is just the same array, but the looks oft things.
@nrob - For what you're wanting to do, there are simpler ways. Just write a wrapper class that holds on to the original data and sets the viewed data to a new slice on a button press or slider update. As a quick example: gist.github.com/joferkington/5682690
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.