In Matplotlib, a common problem are unwanted white lines between Patch objects drawn with pcolor, pcolormesh, and contourf (see this question for the former two and this question for the latter).
I've attempted to fix this automatically by adding new methods to my Axes class/subclass instances using MethodType. I do this instead of subclassing simply because I want to generate Axes by passing slices of GridSpec objects to the add_subplot method on a Figure instance, and I am not aware of how I could do this with some kind of subclassed matplotlib.axes.Subplot (but I welcome advice). Here is some example code:
from types import MethodType
import matplotlib.pyplot as plt
from matplotlib.gridspec import GridSpec
f = plt.figure()
gs = GridSpec(2,1)
ax = f.add_subplot(gs[0,0])
def _pcolormesh(self, *args, **kwargs):
p = self.pcolormesh(*args, **kwargs)
p.set_edgecolor('face')
p.set_linewidth(0.2) # Will cover white lines, without making dot in corner of each square visible
return p
def _contourf(self, *args, **kwargs):
c = self.contourf(*args, **kwargs)
for _ in c.collections:
_.set_edgecolor('face')
return c
ax.mypcolormesh = MethodType(_pcolormesh, ax)
ax.mycontourf = MethodType(_contourf, ax)
In the last line, I would prefer to be able to write ax.pcolormesh instead of ax.mypcolormesh, but this raises a RecursionError, because _pcolormesh calls the original method name... which is now aliased to itself.
So, how can I access a method on this Axes instance, override it, and preserve the original name?