I am very curious to know how seaborn changes the behavior of matplotlib functions by just import seaborn as sns
.
I want to realize the same function to change the behavior of imshow() function in pyplot, for example, i want to show the pixel value at the figure's low left corner.
It is of course possible to just redefine the imshow() function, and import the redefined ones, but the thing is I have multiple scripts that calls imshow() in many different ways, e.g., plt.imshow()
, imshow()
, and the OOP style axes.imshow()
. Is there a simple way to do this like seaborn does?
Read seaborn's source code would of course give me some clue if I have the time luxury ...

- 10,053
- 8
- 41
- 67
-
This functionality (showing pixel value) is actually on the master branch now. – tacaswell Jun 06 '15 at 03:18
-
1You can also define your own functions `my_imshow(ax, data, *args, **kwargs)` to add any extra functionality you want. – tacaswell Jun 06 '15 at 03:19
-
@tcaswell do you have an example on how to enable this functionality? i did some search but could not find it. thanks! – shelper Jun 08 '15 at 13:46
2 Answers
Seaborn does not change the behavior of matplotlib functions in the way you describe. Matplotlib exposes a number of options for customization that take effect by changing the default values of various plot parameters. When seaborn is imported, it runs some code that uses this functionality to change the global defaults.
There is an important distinction between changing default parameter values and altering the behavior of functions. What you are proposing is the latter, and it is sometimes called monkey patching. It is possible, but it would be different than what seaborn is doing, and it isn't something I would recommend in any kind of production environment.

- 46,693
- 16
- 125
- 127
-
i am not using this for production, so can you give a simple example on this "monkey patching" technique? or do you mean i have to monkey patching each one of "plt.imshow(), imshow(), axes.imshow()" separately? – shelper Jun 08 '15 at 13:49
-
If you look at the source code for `plt.imshow` it just does `ax = plt.gca()` and then `ax.imshow()`. Hopefully that helps. But I agree strongly with @tcaswell that you should write your own wrapper function to customize; it will save you a lot of trouble in the long run. – mwaskom Jun 08 '15 at 15:53
you can override whatever you want in python
my_pyplot.py
import matplotlib as mpl
def myPyPlot(*args,**kwargs):
print "You Said:",args,kwargs
mpl.pyplot = myPyPlot
main.py
import my_pyplot as mpp
from matplotlib import pyplot
print pyplot("arg1","arg2",axes="yellow")
note that you need to import your stuff before you import the modified stuff

- 110,522
- 12
- 160
- 179
-
you sure this also change the behavior of axes.imshow() which is a member function? – shelper Jun 05 '15 at 19:47
-
no this just changes the behaviour of pyplot entirely ... you would need to overload `axes.imshow` to change its behaviour... all this is showing you is that you can change the behaviour of pretty much anything by overriding it – Joran Beasley Jun 05 '15 at 19:49
-
then it is exactly the question i ask, "it is of course possible to just redefine the imshow() function, and import the redefined ones, but the thing is I have multiple scripts that calls imshow() in many different ways, e.g., plt.imshow(), imshow(), and the OOP style axes.imshow(). Is there a simple way like one time import to change the behavior of all these imshow() functions? – shelper Jun 05 '15 at 19:52
-
so if i want to overload axes.imshow, how i can overload it in class level rather than the object level? inherit will work of course, but i have to change the code (all the class names) for all my scripts .... – shelper Jun 05 '15 at 19:59