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 8379067
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T16:04:48+00:00 2026-06-09T16:04:48+00:00

I have a problem with Matplotlib’s subplots. I do not know the number of

  • 0

I have a problem with Matplotlib’s subplots. I do not know the number of subplots I want to plot beforehand, but I know that I want them in two rows. so I cannot use

plt.subplot(212)

because I don’t know the number that I should provide.

It should look like this:

example for plot

Right now, I plot all the plots into a folder and put them together with illustrator, but there has to be a better way with Matplotlib. I can provide my code if I was unclear somewhere.

  • 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-06-09T16:04:50+00:00Added an answer on June 9, 2026 at 4:04 pm

    My understanding is that you only know the number of plots at runtime and hence are struggling with the shorthand syntax, e.g.:

    plt.subplot(121)
    

    Thankfully, to save you having to do some awkward math to figure out this number programatically, there is another interface which allows you to use the form:

    plt.subplot(n_cols, n_rows, plot_num)
    

    So in your case, given you want n plots, you can do:

    n_plots = 5 # (or however many you programatically figure out you need)
    n_cols = 2
    n_rows = (n_plots + 1) // n_cols
    for plot_num in range(n_plots):
       ax = plt.subplot(n_cols, n_rows, plot_num)
       # ... do some plotting
    

    Alternatively, there is also a slightly more pythonic interface which you may wish to be aware of:

    fig, subplots = plt.subplots(n_cols, n_rows)
    for ax in subplots:
       # ... do some plotting
    

    (Notice that this was subplots() not the plain subplot()). Although I must admit, I have never used this latter interface.

    HTH

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

Sidebar

Related Questions

I have problem SIMILAR to preventing form data reposting, but not quite the same
I have a matplotlib figure that I want to be able to switch between
My problem is: I have Matplotlib figure in PyGTK application, that is constatly updated
I have a Python program that generates a histogram using matplotlib. The problem is
Using matplotlib/pylab.... How do I plot 5 heatmaps as subplots which have the same
I have a problem displaying non- ASCII characters in Matplotlib, these characters are rendered
I have a problem getting matplotlib 1.0.0 to work in Python 2.5.2 ( Ubuntu
I have a simple problem in python and matplotlib. I have 3 lists :
I have a class which I use to plot things then save them to
I have a problem with Matplotlib 1.0.1 I create a figure, and the I

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.