The title pretty much says it.
However, the way matplotlib is set up, it's not possible to simply inherit from Axes and have it work.
The Axes object is never used directly, typically it's only returned from calls to subplot or other functions.
There's a couple reasons I want to do this. First, to reduce reproducing plots with similar parameters over and over. Something like this:
class LogTemp(plt.Axes):
""" Axes to display temperature over time, in logscale """
def __init__(self, *args, **kwargs):
super.__init__(*args, **kwargs)
self.set_xlabel("Time (s)")
self.set_ylabel("Temperature(C)")
self.set_yscale('log')
It wouldn't be hard to write a custom function for this, although it wouldn't be as flexible. The bigger reason is that I want to override some of the default behavior. As a very simple example, consider
class Negative(plt.Axes):
""" Plots negative of arguments """
def plot(self, x, y, *args, **kwargs):
super().plot(x, -y, *args, **kwargs)
or
class Outliers(plt.Axes):
""" Highlight outliers in plotted data """
def plot(self, x, y, **kwargs):
out = y > 3*y.std()
super().plot(x, -y, **kwargs)
super().plot(x[out], y[out], marker='x', linestyle='', **kwargs)
Trying to modify more than one aspect of behavior will very quickly become messy if using functions.
However, I haven't found a way to have matplotlib easily handle new Axes classes.
The docs don't mention it anywhere that I've seen.
This question addresses inheriting from the Figure class.
The custom class can then be passed into some matplotlib functions.
An unanswered question here suggests the Axes aren't nearly as straightforward.
Update: It's possible to monkey patch matplotlib.axes.Axes to override the default behavior, but this can only be done once when the program is first executed. Using multiple custom Axes is not possible with this approach.