Can I get the axis that will be returned by `pyplo

2020-04-16 04:52发布

问题:

I'm using a custom figure class in a call to pyplot's subplot()

fig, ax = matplotlib.pyplot.subplots(FigureClass=MyFigure)

and would like to use the axis object(s), ax, normally returned by subplot(), in the constructor to the custom figure class. For example, I'd like to take that axis and twin it:

class MyFigure(matplotlib.figure.Figure):

    def __init__(self, *args, **kwargs):
        super(MyFigure, self).__init__(**kwargs)

        self.ax_one = self.method_that_gets_the_ax_returned_by_subplots()
        self.ax_two = self.ax_one.twinx()
        self.ax_three = self.ax_one.twinx()

but I can't find a way to get (what will be returned as) ax here. Using gca() results in a blank figure and an "extra" axis, for example; while using get_axes() results in errors (it's an empty list).

Is there a way to get the axis that will be returned by subplots inside the constructor for the figure it creates?