Simple Axis Pad

PythonPythonBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

This lab will teach you how to use the add_floating_axis function in Matplotlib to add floating axes to a plot, which can be used to display additional information about the plot. Specifically, you will learn how to adjust the padding of tick labels and axis labels, as well as how to adjust the position of ticks on the floating axes.

VM Tips

After the VM startup is done, click the top left corner to switch to the Notebook tab to access Jupyter Notebook for practice.

Sometimes, you may need to wait a few seconds for Jupyter Notebook to finish loading. The validation of operations cannot be automated because of limitations in Jupyter Notebook.

If you face issues during learning, feel free to ask Labby. Provide feedback after the session, and we will promptly resolve the problem for you.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/PythonStandardLibraryGroup(["`Python Standard Library`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") matplotlib/BasicConceptsGroup -.-> matplotlib/importing_matplotlib("`Importing Matplotlib`") matplotlib/BasicConceptsGroup -.-> matplotlib/figures_axes("`Understanding Figures and Axes`") python/BasicConceptsGroup -.-> python/variables_data_types("`Variables and Data Types`") python/BasicConceptsGroup -.-> python/booleans("`Booleans`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/DataStructuresGroup -.-> python/sets("`Sets`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/PythonStandardLibraryGroup -.-> python/data_collections("`Data Collections`") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("`Numerical Computing`") python/DataScienceandMachineLearningGroup -.-> python/data_visualization("`Data Visualization`") subgraph Lab Skills python/comments -.-> lab-71152{{"`Simple Axis Pad`"}} matplotlib/importing_matplotlib -.-> lab-71152{{"`Simple Axis Pad`"}} matplotlib/figures_axes -.-> lab-71152{{"`Simple Axis Pad`"}} python/variables_data_types -.-> lab-71152{{"`Simple Axis Pad`"}} python/booleans -.-> lab-71152{{"`Simple Axis Pad`"}} python/lists -.-> lab-71152{{"`Simple Axis Pad`"}} python/tuples -.-> lab-71152{{"`Simple Axis Pad`"}} python/sets -.-> lab-71152{{"`Simple Axis Pad`"}} python/function_definition -.-> lab-71152{{"`Simple Axis Pad`"}} python/importing_modules -.-> lab-71152{{"`Simple Axis Pad`"}} python/data_collections -.-> lab-71152{{"`Simple Axis Pad`"}} python/numerical_computing -.-> lab-71152{{"`Simple Axis Pad`"}} python/data_visualization -.-> lab-71152{{"`Simple Axis Pad`"}} end

Import Libraries

First, import the necessary libraries, including matplotlib.pyplot, numpy, and mpl_toolkits.axisartist.

import matplotlib.pyplot as plt
import numpy as np
import mpl_toolkits.axisartist as axisartist

Define Setup Axes Function

Next, define the setup_axes() function, which sets up the polar projection of the plot. This function uses a GridHelperCurveLinear to create a polar projection in a rectangular box. It also sets the limits of the plot and returns the ax1 object.

def setup_axes(fig, rect):
    ## Define the PolarAxes transform and the extreme finder
    tr = Affine2D().scale(np.pi/180., 1.) + PolarAxes.PolarTransform()
    extreme_finder = angle_helper.ExtremeFinderCycle(20, 20, lon_cycle=360, lat_cycle=None, lon_minmax=None, lat_minmax=(0, np.inf))

    ## Define the grid locators and formatters
    grid_locator1 = angle_helper.LocatorDMS(12)
    grid_locator2 = grid_finder.MaxNLocator(5)
    tick_formatter1 = angle_helper.FormatterDMS()

    ## Define the GridHelperCurveLinear
    grid_helper = GridHelperCurveLinear(tr, extreme_finder=extreme_finder, grid_locator1=grid_locator1, grid_locator2=grid_locator2, tick_formatter1=tick_formatter1)

    ## Create the axis object and set its limits
    ax1 = fig.add_subplot(rect, axes_class=axisartist.Axes, grid_helper=grid_helper)
    ax1.axis[:].set_visible(False)
    ax1.set_aspect(1.)
    ax1.set_xlim(-5, 12)
    ax1.set_ylim(-5, 10)

    return ax1

Define Add Floating Axis Function

Define the add_floating_axis function, which adds a floating axis to the plot. This function takes in the ax1 object as an argument and returns the axis object.

def add_floating_axis(ax1):
    ## Define the floating axis
    ax1.axis["lat"] = axis = ax1.new_floating_axis(0, 30)
    axis.label.set_text(r"$\theta = 30^{\circ}$")
    axis.label.set_visible(True)

    return axis

Add Padding to Tick Labels

In this step, add padding to the tick labels on the floating axis. This can be done by setting the pad attribute of the major_ticklabels object to the desired padding value.

## Add Padding to Tick Labels
fig = plt.figure(figsize=(9, 3.))
fig.subplots_adjust(left=0.01, right=0.99, bottom=0.01, top=0.99, wspace=0.01, hspace=0.01)

ax1 = setup_axes(fig, rect=121)
axis = add_floating_axis(ax1)

ax1 = setup_axes(fig, rect=122)
axis = add_floating_axis(ax1)
axis.major_ticklabels.set_pad(10)

plt.show()

Adjust Axis Label Padding

In this step, adjust the padding of the axis label on the floating axis. This can be done by setting the pad attribute of the label object to the desired padding value.

## Adjust Axis Label Padding
fig = plt.figure(figsize=(9, 3.))
fig.subplots_adjust(left=0.01, right=0.99, bottom=0.01, top=0.99, wspace=0.01, hspace=0.01)

ax1 = setup_axes(fig, rect=121)
axis = add_floating_axis(ax1)

ax1 = setup_axes(fig, rect=122)
axis = add_floating_axis(ax1)
axis.label.set_pad(20)

plt.show()

Adjust Tick Position

In this step, adjust the position of the ticks on the floating axis. This can be done by setting the tick_out attribute of the major_ticks object to True.

## Adjust Tick Position
fig = plt.figure(figsize=(9, 3.))
fig.subplots_adjust(left=0.01, right=0.99, bottom=0.01, top=0.99, wspace=0.01, hspace=0.01)

ax1 = setup_axes(fig, rect=121)
axis = add_floating_axis(ax1)

ax1 = setup_axes(fig, rect=122)
axis = add_floating_axis(ax1)
axis.major_ticks.set_tick_out(True)

plt.show()

Summary

In this lab, you learned how to use the add_floating_axis function in Matplotlib to add floating axes to a plot. You also learned how to adjust the padding of tick labels and axis labels, as well as how to adjust the position of ticks on the floating axes. By the end of this lab, you should be able to create customized plots with floating axes that display additional information about the plot.

Other Python Tutorials you may like