Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6002633
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:00:25+00:00 2026-05-23T01:00:25+00:00

Looking at the matplotlib documentation, it seems the standard way to add an AxesSubplot

  • 0

Looking at the matplotlib documentation, it seems the standard way to add an AxesSubplot to a Figure is to use Figure.add_subplot:

from matplotlib import pyplot

fig = pyplot.figure()
ax = fig.add_subplot(1,1,1)
ax.hist( some params .... )

I would like to be able to create AxesSubPlot-like objects independently of the figure, so I can use them in different figures. Something like

fig = pyplot.figure()
histoA = some_axes_subplot_maker.hist( some params ..... )
histoA = some_axes_subplot_maker.hist( some other params ..... )
# make one figure with both plots
fig.add_subaxes(histo1, 211)
fig.add_subaxes(histo1, 212)
fig2 = pyplot.figure()
# make a figure with the first plot only
fig2.add_subaxes(histo1, 111)

Is this possible in matplotlib and if so, how can I do this?

Update: I have not managed to decouple creation of Axes and Figures, but following examples in the answers below, can easily re-use previously created axes in new or olf Figure instances. This can be illustrated with a simple function:

def plot_axes(ax, fig=None, geometry=(1,1,1)):
    if fig is None:
        fig = plt.figure()
    if ax.get_geometry() != geometry :
        ax.change_geometry(*geometry)
    ax = fig.axes.append(ax)
    return fig
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T01:00:26+00:00Added an answer on May 23, 2026 at 1:00 am

    Typically, you just pass the axes instance to a function.

    For example:

    import matplotlib.pyplot as plt
    import numpy as np
    
    def main():
        x = np.linspace(0, 6 * np.pi, 100)
    
        fig1, (ax1, ax2) = plt.subplots(nrows=2)
        plot(x, np.sin(x), ax1)
        plot(x, np.random.random(100), ax2)
    
        fig2 = plt.figure()
        plot(x, np.cos(x))
    
        plt.show()
    
    def plot(x, y, ax=None):
        if ax is None:
            ax = plt.gca()
        line, = ax.plot(x, y, 'go')
        ax.set_ylabel('Yabba dabba do!')
        return line
    
    if __name__ == '__main__':
        main()
    

    To respond to your question, you could always do something like this:

    def subplot(data, fig=None, index=111):
        if fig is None:
            fig = plt.figure()
        ax = fig.add_subplot(index)
        ax.plot(data)
    

    Also, you can simply add an axes instance to another figure:

    import matplotlib.pyplot as plt
    
    fig1, ax = plt.subplots()
    ax.plot(range(10))
    
    fig2 = plt.figure()
    fig2.axes.append(ax)
    
    plt.show()
    

    Resizing it to match other subplot “shapes” is also possible, but it’s going to quickly become more trouble than it’s worth. The approach of just passing around a figure or axes instance (or list of instances) is much simpler for complex cases, in my experience…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Looking at posts like this and others, it seems that the correct way to
Looking to pass variables from c# to javascript to use some jquery code. Passing
There are a lot of questions about matplotlib, pylab, pyplot, ipython, so I'm sorry
Is there a way to render a 3D pie in matplotlib, or is there
Looking at the wikimedia api documentation it only talks of searching for images who's
Looking for feedback on : http://code.google.com/p/google-perftools/wiki/GooglePerformanceTools
Looking for an example that: Launches an EXE Waits for the EXE to finish.
Looking for C# class which wraps calls to do the following: read and write
Looking at what's running and nothing jumps out. Thanks!
Looking to do a very small, quick 'n dirty side project. I like the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.