3

Let's say we have a 2D array, image, eg. 20x20. I would like add a method, called 'imshow' to this object such that whenever I do image.imshow(**kwargs)), the method imshow will make a call of Matplotlib.pyplot.imshow

What is the best way to do this? I was thinking of writing a class with an inheritance from numpy.ndarray, and adding a method 'imshow'.

1 Answer 1

1

Just found the answer (thanks to How can a class that inherits from a NumPy array change its own values?)!

class array(np.ndarray):

    def __new__(cls, a):
        obj = np.asarray(a).view(cls)
        return obj

    def imshow(self, **kwargs):
        plt.figure()
        plt.imshow(self, **kwargs)
        plt.show()
Sign up to request clarification or add additional context in comments.

Comments

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.