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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T12:17:40+00:00 2026-06-10T12:17:40+00:00

I have a few boxplots in matplotlib that I want to zoom in on

  • 0

I have a few boxplots in matplotlib that I want to zoom in on a particular y-range ([0,0.1]) using inset axes. It is not clear to me from the example in the documentation how I should do this for multiple boxplots on the same figure. I was trying to modify the code provided this example, but there was too much unnecessary complexity. My code is pretty simple:

# dataToPlot is a list of lists, containing some data. 
plt.figure()
plt.boxplot(dataToPlot)
plt.savefig( 'image.jpeg', bbox_inches=0)

How do I add inset axes and zoom in on the first boxplot of the two? How can I do it for both?

EDIT: I tried the code below, but here’s what I got:
enter image description here

What went wrong?

# what's the meaning of these two parameters?
fig = plt.figure(1, [5,4])
# what does 111 mean?
ax = fig.add_subplot(111)
ax.boxplot(data)
# ax.set_xlim(0,21)  # done automatically based on the no. of samples, right?
# ax.set_ylim(0,300) # done automatically based on max value in my samples, right?
# Create the zoomed axes
axins = zoomed_inset_axes(ax, 6, loc=1) # zoom = 6, location = 1 (upper right)
axins.boxplot(data)
# sub region of the original image
#here I am selecting the first boxplot by choosing appropriate values for x1 and x2 
# on the y-axis, I'm selecting the range which I want to zoom in, right?
x1, x2, y1, y2 = 0.9, 1.1, 0.0, 0.01
axins.set_xlim(x1, x2)
axins.set_ylim(y1, y2)
# even though it's false, I still see all numbers on both axes, how do I remove them?
plt.xticks(visible=False)
plt.yticks(visible=False)
# draw a bbox of the region of the inset axes in the parent axes and
# connecting lines between the bbox and the inset axes area
# what are fc and ec here? where do loc1 and loc2 come from?
mark_inset(ax, axins, loc1=2, loc2=4, fc="none", ec="0.5")
plt.savefig( 'img.jpeg', bbox_inches=0)
  • 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-10T12:17:40+00:00Added an answer on June 10, 2026 at 12:17 pm

    The loc determines the location of the zoomed axis, 1 for upper right, 2 for upper left and so on. I modified the example code slightly to generate multiple zoomed axis.

    import matplotlib.pyplot as plt
    
    from mpl_toolkits.axes_grid1.inset_locator import zoomed_inset_axes
    from mpl_toolkits.axes_grid1.inset_locator import mark_inset
    
    import numpy as np
    
    def get_demo_image():
        from matplotlib.cbook import get_sample_data
        import numpy as np
        f = get_sample_data("axes_grid/bivariate_normal.npy", asfileobj=False)
        z = np.load(f)
        # z is a numpy array of 15x15
        return z, (-3,4,-4,3)
    
    
    fig = plt.figure(1, [5,4])
    ax = fig.add_subplot(111)
    
    # prepare the demo image
    Z, extent = get_demo_image()
    Z2 = np.zeros([150, 150], dtype="d")
    ny, nx = Z.shape
    Z2[30:30+ny, 30:30+nx] = Z
    
    # extent = [-3, 4, -4, 3]
    ax.imshow(Z2, extent=extent, interpolation="nearest",
              origin="lower")
    
    axins = zoomed_inset_axes(ax, 6, loc=1) # zoom = 6
    axins.imshow(Z2, extent=extent, interpolation="nearest",
                 origin="lower")
    
    # sub region of the original image
    x1, x2, y1, y2 = -1.5, -0.9, -2.5, -1.9
    axins.set_xlim(x1, x2)
    axins.set_ylim(y1, y2)
    
    axins1 = zoomed_inset_axes(ax, 8, loc=2) # zoom = 8
    axins1.imshow(Z2, extent=extent, interpolation="nearest",
                 origin="lower")
    
    # sub region of the original image
    x1, x2, y1, y2 = -1.2, -0.9, -2.2, -1.9
    axins1.set_xlim(x1, x2)
    axins1.set_ylim(y1, y2)
    
    plt.xticks(visible=False)
    plt.yticks(visible=False)
    
    # draw a bbox of the region of the inset axes in the parent axes and
    # connecting lines between the bbox and the inset axes area
    mark_inset(ax, axins, loc1=2, loc2=4, fc="none", ec="0.5")
    mark_inset(ax, axins1, loc1=2, loc2=4, fc="none", ec="0.5")
    
    plt.draw()
    plt.show()
    

    enter image description here

    Edit1:

    Similarly, you can also add zoomed axis in a boxplot. Here is an example

    from pylab import *
    from mpl_toolkits.axes_grid1.inset_locator import zoomed_inset_axes
    from mpl_toolkits.axes_grid1.inset_locator import mark_inset
    
    # fake up some data
    spread = rand(50) * 100 
    center = ones(25) * 50
    flier_high = rand(10) * 100 + 100
    flier_low = rand(10) * -100
    data = concatenate((spread, center, flier_high, flier_low), 0)
    
    # fake up some more data
    spread= rand(50) * 100
    center = ones(25) * 40
    flier_high = rand(10) * 100 + 100
    flier_low = rand(10) * -100
    d2 = concatenate( (spread, center, flier_high, flier_low), 0 )
    data.shape = (-1, 1)
    d2.shape = (-1, 1)
    data = [data, d2, d2[::2,0]]
    
    # multiple box plots on one figure
    fig = plt.figure(1, [5,4])
    ax = fig.add_subplot(111)
    ax.boxplot(data)
    ax.set_xlim(0.5,5)
    ax.set_ylim(0,300)
    
    # Create the zoomed axes
    axins = zoomed_inset_axes(ax, 3, loc=1) # zoom = 3, location = 1 (upper right)
    axins.boxplot(data)
    
    # sub region of the original image
    x1, x2, y1, y2 = 0.9, 1.1, 125, 175
    axins.set_xlim(x1, x2)
    axins.set_ylim(y1, y2)
    plt.xticks(visible=False)
    plt.yticks(visible=False)
    
    # draw bboxes of the two regions of the inset axes in the parent axes and
    # connect lines between the bbox and the inset axes area
    mark_inset(ax, axins, loc1=2, loc2=4, fc="none", ec="0.5")
    
    show() 
    

    enter image description here

    Edit2

    In case the distribution is heterogeneous, i.e., most values are small with few very large values, the above zooming procedure might not work, as it will zoom both the x as well as y axis. In that case, it is better to change the scale of y-axis to log.

    from pylab import *
    
    # fake up some data
    spread = rand(50) * 1
    center = ones(25) * .5
    flier_high = rand(10) * 100 + 100
    flier_low = rand(10) * -100
    data = concatenate((spread, center, flier_high, flier_low), 0)
    
    # fake up some more data
    spread = rand(50) * 1
    center = ones(25) * .4
    flier_high = rand(10) * 100 + 100
    flier_low = rand(10) * -100
    d2 = concatenate( (spread, center, flier_high, flier_low), 0 )
    data.shape = (-1, 1)
    d2.shape = (-1, 1)
    data = [data, d2, d2[::2,0]]
    
    # multiple box plots on one figure
    fig = plt.figure(1, [5,4]) # Figure Size
    ax = fig.add_subplot(111)  # Only 1 subplot 
    ax.boxplot(data)
    ax.set_xlim(0.5,5)
    ax.set_ylim(.1,300)
    ax.set_yscale('log')
    
    show()
    

    enter image description here

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

Sidebar

Related Questions

I have few html pages from my old site that I want to place
I have few pages that I show from my main page inside iframe .
I have few XSLT files, index stylesheet imports layout using xsl:import. VS says that
I have few types that derive from simplified Base as shown below. I am
I have few DB tables, witch are build using inheritance from one table witch
I have few long commands that I will be using on a day to
I have few links displayed in JEditorPane ex: http://www.google.com/finance?q=NYSE:C http://www.google.com/finance?q=NASDAQ:MSFT I want that I
I have few fonts that I am using in my web projects. Everything goes
I have few 'so' files that I need to load in maven java project
I have few custom controls (image views) added programmatically to table cell. I want

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.