Memecoins have never been taken seriously since the beginning. Nevertheless, the landscape has evolved, and these meme-inspired cryptocurrencies have gained prominence. Pepecoin (PEPE) and Dogecoin (D
Cardano has kicked off voting for its Project Catalyst Fund 11. Meanwhile, both Ethereum and Pullix have set timelines for major upcoming developments. While Ethereum has set a date for its Dencun tes
including 4 ways to make them and 2 ways to style them
example plot with four inset axes
Inset axes are a powerful data visualization technique to highlight specific plot regions or add detailed subplots. They are a great way to make effective use of otherwise-emtpy figure space.
This tutorial shows 4 methods to create inset axes in matplotlib, which let you position insets relative to an axes, to an overall, figure, in absolute units (i.e., inches), or using a grid system — the latter useful in particular when working with multiple insets.
We’ll also cover 2 ways to style zoom insets: with classic leader lines and with color-coded overlays.
inset axes with color-coded overlays
At the end of this tutorial, you will be able to determine which approach best meets your needs —and have some code you can copy/paste to make it happen.
manual placement with axes-level coordinates: Axes.inset_axes, and
manual placement with figure-level coordinates: Figure.add_axes;
multi-inset auto-layout:Axes.inset_axes with outset.layout_corner_insets.
Adding zoom indicators:
5. leader lines: Axes.indicate_inset_zoom and
6. color-coded overlays: OutsetGrid.marqueeplot.
Sections 4 and 6 make use new tools from the open source outset library for multi-scale data visualization, which I recently released and am excited to share with the community.
Method 1: Using `mpl_toolkits.axes_grid1.inset_axes`
This function simplifies adding insets. Here’s how to use it, including an explanation of the `loc` parameter for positioning:
import matplotlib.pyplot as plt from mpl_toolkits.axes_grid1.inset_locator import inset_axes
fig, ax = plt.subplots(); ax.set_box_aspect(0.5) # main figure and axes ax.plot([0, 9], [0, 9]) # example plot
# create inset axes & plot on them inset_ax = inset_axes(ax, width="30%", height=1.2, loc="upper left") inset_ax.plot([9, 0], [0, 9], color="r") plt.xticks([]); plt.yticks([]) # strip ticks, which collide w/ main ax
Note that axes size can be specified relative to parent axes or in inches, as shown here with width and height, respectively.
Matplotlib’s Axes class provides the inset_axes member function, which is a straightforward way to create insets relative to the parent axes:
import matplotlib.pyplot as plt
fig, ax = plt.subplots(); ax.set_box_aspect(0.5) # main figure and axes ax.plot([0, 9], [0, 9]) # example plot
# create inset axes & plot on them ins_ax = ax.inset_axes([.6, .15, .3, .3]) # [x, y, width, height] w.r.t. ax ins_ax.plot([9, 0], [0, 9], color="r")
Coordinates are specified relative to the parent axes, so — for example — (0, 0, 0.5, 0.2) will create an axes in the lower left-hand corner with width that takes up half of axes width and height that takes up 0.2 of axes height.
To position an inset relative to a parent axes ax in terms of inches, we must first calculate the size of the parent axes in inches.
Matplotlib’s Figure class provides an analogous add_axes member function, which lets you position insets relative to the overall figure.
import matplotlib.pyplot as plt
fig, ax = plt.subplots(); ax.set_box_aspect(0.5) # main figure and axes ax.plot([0, 9], [0, 9]) # example plot
# create inset axes & plot on them ins_ax = fig.add_axes([.2, .5, .2, .2]) # [x, y, width, height] w.r.t. fig ins_ax.plot([9, 0], [0, 9], color="r")
Similarly to before, coordinates will be specified relative to the parent axes, so — for example — (0.5, 0.5, 0.3, 0.2) will create an axes 2/10ths the height of the overall figure and 3/10ths the width with the lower left corner centered within the figure.
Method 4: `Axes.inset_axes` with `outset.layout_corner_insets`
For this next example, we will use the outset library, which provides specialized tools for working with inset axes in matplotlib. It can be installed as python3 -m pip install outset.
The outset library provides the flexible outset.util.layout_corner_insets utility to position multiple inset axes within a specified corner of a main axes. Here’s how to use it to pick positions for calls to Axes.inset_axes.
import matplotlib.pyplot as plt import outset
fig, ax = plt.subplots(); ax.set_box_aspect(0.5) # main figure and axes ax.plot([0, 9], [0, 9]) # example plot
# ------ pick inset axes positions: 3 in upper left, one in lower right inset_positions = outset.util.layout_corner_insets( # upper left positions 3, "NW", # number insets and corner to position in # optional layout tweaks... inset_pad_ratio=(.2,.35), inset_grid_size=(.6,.65), inset_margin_size=.05) inset_positions.append( # generate lower right position & tack on to list outset.util.layout_corner_insets(1, "SE", inset_grid_size=.4))
# ----- create inset axes & plot on them inset_axes = [*map(ax.inset_axes, inset_positions)] # create inset axes for iax in inset_axes: # example plot iax.plot([9, 0], [0, 9], color="r")
Note the optional customizations to inset positioning made through keyword arguments to outset.util.layout_corner_insets. Here, “pad” refers to spacing between insets, “margin” refers to space between the insets and the main axes, and “grid size” refers to the overall fraction of axes space that insets are stacked into.
That covers it for techniques to place inset axes!
A common use case for inset axes is to provide magnification of an area of interest within the main plot. Next, we’ll look at two ways to visually correspond magnifying insets with a region of the main plot.
Method 5: Zoom Indicator Leaders
A classic approach depicting zoom relationships draws connecting lines between the corners of the inset axes and the region it is magnifying. This works especially well when plotting small numbers of insets.
Matplotlib’s Axes includes the indicate_inset_zoom member function for this purpose. Here’s how to use it.
from math import isclose; import matplotlib.pyplot as plt
# set up main fig/axes fig, main_ax = plt.subplots(); main_ax.set_box_aspect(0.5) inset_ax = main_ax.inset_axes( [0.05, 0.65, 0.3, 0.3], # [x, y, width, height] w.r.t. axes xlim=[4, 5], ylim=[4, 5], # sets viewport & tells relation to main axes xticklabels=[], yticklabels=[] )
# add plot content for ax in main_ax, inset_ax: ax.plot([0, 9], [0, 9]) # first example line ax.plot([0, 9], [1, 8]) # second example line
# careful! warn if aspect ratio of inset axes doesn't match main axes if not isclose(inset_ax._get_aspect_ratio(), main_ax._get_aspect_ratio()): print("chosen inset x/ylim & width/height skew aspect w.r.t. main axes!")
Note that to use Axes.indicate_inset_zoom, inset axes must be created using Axes.inset_axes.
Method 6: Zoom Indicator Overlays
When working with larger numbers of insets, it may work better to correspond regions with numbered, color-coded highlights instead of leader lines.
The outset library’s OutsetGrid provides a marqueeplot member function for this purpose. Under this scheme, inset positioning is handled by outset.inset_outsets. Here’s how to create a zoom plot with color-coded position overlays.
from matplotlib import pyplot as plt import numpy as np import outset
# example adapted from https://matplotlib.org/stable/gallery/ i, a, b, c, d = np.arange(0.0, 2 * np.pi, 0.01), 1, 7, 3, 11
# 3 axes grid: source plot and two zoom frames grid = outset.OutsetGrid([ # data coords for zoom sections... (-10, 8, -8, 9), (-1.6, 5, -0.5, 3)]) # ...as (x0, y0, x1, y1) grid.broadcast(plt.plot, # run plotter over all axes # subsequent kwargs go to the plotter np.sin(i * a) * np.cos(i * b) * 20, # line coords np.sin(i * c) * np.cos(i * d) * 20, # line coords c="mediumblue", zorder=-1, # line styling )
# position insets over the source plot into lower/left ("SW") corner outset.inset_outsets(grid, insets="SW")
grid.marqueeplot() # render overlay annotations
Note that inset positioning can be more finely controlled via outset.util.layout_corner_insets, as used for Method 4 above:
... # as before
customized_placements = outset.util.layout_corner_insets( 2, "SW", # 2 insets into the lower left corner inset_margin_size=0.05, inset_grid_size=(0.8, 0.55) # layout params ) outset.inset_outsets(grid, insets=customized_placements) grid.marqueeplot() # render overlay annotations
Inset placements can also be manually specified to outset.inset_outsetsusing axes-relative coordinates, too:
And, finally, to use bigger, side-by-side magnification panels instead of insets, just omit the call to outset.inset_outsets.
Conclusion
Inset axes are a great way to take your matplotlib visualizations to the next level, whether you’re looking to highlight specific data regions or add detailed subplots.
Here, we’ve covered the variety of approaches at your disposal, both built in to matplotlib and using the outset library. Each method offers unique advantages, ensuring a good fit for nearly any inset plotting scenario.
Now go make something informative and beautiful! Happy plotting 🙂
Further Information
matplotlib has some excellent inset example materials that are well worth checking out. In particular,
Formal argument-by-argument listings for all code covered here can be found in the API documentation pages for outset and matplotlib.
Both projects are open source on GitHub, matplotlib under a PSF license at matplotlib/matplotlib and outset under the MIT License at mmore500/outset — outset is a new project, consider leaving a ⭐️!
Joseph Early also has an excellent medium article on inset axes in matplotlib, which you can read here.
I currently serve as a postdoctoral scholar at the University of Michigan, where my work is supported by the Eric and Wendy Schmidt AI in Science Postdoctoral Fellowship, a Schmidt Futures program.
My appointment is split between the university’s Ecology and Evolutionary Biology Department, the Center for the Study of Complexity, and the Michigan Institute for Data Science.
disclosure: I am the author of the outset library.
Citations
J. D. Hunter, “Matplotlib: A 2D Graphics Environment”, Computing in Science & Engineering, vol. 9, no. 3, pp. 90–95, 2007. https://doi.org/10.1109/MCSE.2007.55
Apple has long provided the ability to create desktop aliases in macOS by Command-Option dragging. Here’s how to do it from the macOS Dock.
macOS Dock
Given most macOS desktop items such as files, apps, disk volumes, and folders, Apple has long provided the ability to create desktop aliases of those items using the mouse and keyboard.
By holding down Command-Option on the Mac keyboard, then clicking and dragging a desktop item to another location on the desktop, you create an alias to the item or items dragged.
Vision Pro is an incredible $4,000 developer kit most people should not buy. But, you should definitely try it at an Apple Store or take advantage of the two-week return policy.
Have you ever manually chosen between Azure and AWS when browsing X – or picked a payment processor to reroute your online Apple Pay transactions to? Do you check the SSL certificate of AliExpress or Amazon to make your shopping more secure? Great technology is invisible. Steve Jobs had it right years ago when he […]
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept”, you consent to the use of ALL the cookies.
This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
Cookie
Duration
Description
cookielawinfo-checkbox-analytics
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checkbox-functional
11 months
The cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checkbox-necessary
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-others
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-performance
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policy
11 months
The cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.
Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.
Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.