I have my own class of shapes, and I would like to make sure that all the axes associated with it, created with subplots()or twinx(), etc., have custom behavior.
Now I am doing this by linking new methods to each axis after its creation, for example. using
import types
def my_ax_method(ax, test):
print('{0} is doing something new as a {1}.'.format(ax, test))
class MyFigure(matplotlib.figure.Figure):
def __init__(self, **kwargs):
super(MyFigure, self).__init__(**kwargs)
axes_a = None
axes_b = None
axes_c = None
def setup_axes(self, ax):
self.axes_a = ax
self.axes_b = self.axes_a.twinx()
self.axes_c = self.axes_a.twiny()
self.axes_a.my_method = types.MethodType(my_ax_method, self.axes_a)
self.axes_b.my_method = types.MethodType(my_ax_method, self.axes_b)
self.axes_c.my_method = types.MethodType(my_ax_method, self.axes_c)
in something like
fig, ax = matplotlib.pyplot.subplots(FigureClass=MyFigure)
fig.setup_axes(ax)
fig.axes_a.my_method("probe of A")
fig.axes_b.my_method("test of B")
fig.axes_c.my_method("trial of C")
This seems like a fragile way to accomplish what I'm trying to do. Is there a better, more Putin way to do this?
In particular, is there a way to ensure that all of Axesmy custom classes Figurebelong to a specific class ( as is done for the shapes themselves ): a custom Axesclass that could use these methods as part of its definition?