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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T03:54:51+00:00 2026-05-14T03:54:51+00:00

With, say, 3 rows of subplots in matplotlib, xlabels of one row can overlap

  • 0

With, say, 3 rows of subplots in matplotlib, xlabels of one row can overlap the title of the next. One has to fiddle with pl.subplots_adjust(hspace), which is annoying.

Is there a recipe for hspace that prevents overlaps and works for any nrow?

""" matplotlib xlabels overlap titles ? """
import sys
import numpy as np
import pylab as pl

nrow = 3
hspace = .4  # of plot height, titles and xlabels both fall within this ??
exec "\n".join( sys.argv[1:] )  # nrow= ...

y = np.arange(10)
pl.subplots_adjust( hspace=hspace )

for jrow in range( 1, nrow+1 ):
    pl.subplot( nrow, 1, jrow )
    pl.plot( y**jrow )
    pl.title( 5 * ("title %d " % jrow) )
    pl.xlabel( 5 * ("xlabel %d " % jrow) )

pl.show()

My versions:

  • matplotlib 0.99.1.1,
  • Python 2.6.4,
  • Mac OSX 10.4.11,
  • backend: Qt4Agg (TkAgg => Exception in Tkinter callback)

(For many extra points, can anyone outline how matplotlib’s packer / spacer works, along the lines of chapter 17 “the packer” in the Tcl/Tk book?)

  • 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-14T03:54:51+00:00Added an answer on May 14, 2026 at 3:54 am

    I find this quite tricky, but there is some information on it here at the MatPlotLib FAQ. It is rather cumbersome, and requires finding out about what space individual elements (ticklabels) take up…

    Update:
    The page states that the tight_layout() function is the easiest way to go, which attempts to automatically correct spacing.

    Otherwise, it shows ways to acquire the sizes of various elements (eg. labels) so you can then correct the spacings/positions of your axes elements. Here is an example from the above FAQ page, which determines the width of a very wide y-axis label, and adjusts the axis width accordingly:

    import matplotlib.pyplot as plt
    import matplotlib.transforms as mtransforms
    fig = plt.figure()
    ax = fig.add_subplot(111)
    ax.plot(range(10))
    ax.set_yticks((2,5,7))
    labels = ax.set_yticklabels(('really, really, really', 'long', 'labels'))
    
    def on_draw(event):
       bboxes = []
       for label in labels:
           bbox = label.get_window_extent()
           # the figure transform goes from relative coords->pixels and we
           # want the inverse of that
           bboxi = bbox.inverse_transformed(fig.transFigure)
           bboxes.append(bboxi)
    
       # this is the bbox that bounds all the bboxes, again in relative
       # figure coords
       bbox = mtransforms.Bbox.union(bboxes)
       if fig.subplotpars.left < bbox.width:
           # we need to move it over
           fig.subplots_adjust(left=1.1*bbox.width) # pad a little
           fig.canvas.draw()
       return False
    
    fig.canvas.mpl_connect('draw_event', on_draw)
    
    plt.show()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say you have a MySQL 5.0 MyISAM table with 100 million rows, with one
I have one table having 10 rows.Now on page load lets say first 7
I have data in some text file which has let's say 10000 rows and
I am inserting say 500 rows using INSERT IGNORE, if the row already exists
If I select a column in a table, say 10 rows, can I use
I have a DataTable with 10 rows say one of the columns numbered 1
I'm working with large CSV. How can I take a random sample of rows—say,
Kinda stuck here... I have an application with lets say 5000 rows of data
Say I have a very simple WPF grid (6 rows x 6 columns) defined
Let's say I have a table with 1M rows and I need to add

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.