Custom Matplotlib Projection Showcase

PythonPythonBeginner
Practice Now

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

Introduction

In this lab, we will learn how to create a custom projection using Matplotlib. We will be showcasing Hammer projection by alleviating many features of Matplotlib. We will be using Python as our programming language.

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`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python(("`Python`")) -.-> python/FileHandlingGroup(["`File Handling`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/BasicConceptsGroup(["`Basic Concepts`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/PlottingDataGroup(["`Plotting Data`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/PlotCustomizationGroup(["`Plot Customization`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/AdvancedTopicsGroup(["`Advanced Topics`"]) python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/ObjectOrientedProgrammingGroup(["`Object-Oriented Programming`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") python/FunctionsGroup -.-> python/keyword_arguments("`Keyword Arguments`") python/FileHandlingGroup -.-> python/with_statement("`Using with Statement`") matplotlib/BasicConceptsGroup -.-> matplotlib/importing_matplotlib("`Importing Matplotlib`") matplotlib/BasicConceptsGroup -.-> matplotlib/figures_axes("`Understanding Figures and Axes`") matplotlib/PlottingDataGroup -.-> matplotlib/line_plots("`Line Plots`") matplotlib/PlotCustomizationGroup -.-> matplotlib/grid_config("`Grid Configuration`") matplotlib/AdvancedTopicsGroup -.-> matplotlib/matplotlib_config("`Customizing Matplotlib Configurations`") python/BasicConceptsGroup -.-> python/booleans("`Booleans`") python/ControlFlowGroup -.-> python/conditional_statements("`Conditional Statements`") python/ControlFlowGroup -.-> python/for_loops("`For Loops`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/DataStructuresGroup -.-> python/dictionaries("`Dictionaries`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") python/FunctionsGroup -.-> python/default_arguments("`Default Arguments`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/ModulesandPackagesGroup -.-> python/using_packages("`Using Packages`") python/ModulesandPackagesGroup -.-> python/standard_libraries("`Common Standard Libraries`") python/ObjectOrientedProgrammingGroup -.-> python/classes_objects("`Classes and Objects`") python/ObjectOrientedProgrammingGroup -.-> python/constructor("`Constructor`") python/ObjectOrientedProgrammingGroup -.-> python/polymorphism("`Polymorphism`") python/ObjectOrientedProgrammingGroup -.-> python/encapsulation("`Encapsulation`") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("`Numerical Computing`") python/DataScienceandMachineLearningGroup -.-> python/data_visualization("`Data Visualization`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/comments -.-> lab-48642{{"`Custom Matplotlib Projection Showcase`"}} python/keyword_arguments -.-> lab-48642{{"`Custom Matplotlib Projection Showcase`"}} python/with_statement -.-> lab-48642{{"`Custom Matplotlib Projection Showcase`"}} matplotlib/importing_matplotlib -.-> lab-48642{{"`Custom Matplotlib Projection Showcase`"}} matplotlib/figures_axes -.-> lab-48642{{"`Custom Matplotlib Projection Showcase`"}} matplotlib/line_plots -.-> lab-48642{{"`Custom Matplotlib Projection Showcase`"}} matplotlib/grid_config -.-> lab-48642{{"`Custom Matplotlib Projection Showcase`"}} matplotlib/matplotlib_config -.-> lab-48642{{"`Custom Matplotlib Projection Showcase`"}} python/booleans -.-> lab-48642{{"`Custom Matplotlib Projection Showcase`"}} python/conditional_statements -.-> lab-48642{{"`Custom Matplotlib Projection Showcase`"}} python/for_loops -.-> lab-48642{{"`Custom Matplotlib Projection Showcase`"}} python/lists -.-> lab-48642{{"`Custom Matplotlib Projection Showcase`"}} python/tuples -.-> lab-48642{{"`Custom Matplotlib Projection Showcase`"}} python/dictionaries -.-> lab-48642{{"`Custom Matplotlib Projection Showcase`"}} python/function_definition -.-> lab-48642{{"`Custom Matplotlib Projection Showcase`"}} python/default_arguments -.-> lab-48642{{"`Custom Matplotlib Projection Showcase`"}} python/importing_modules -.-> lab-48642{{"`Custom Matplotlib Projection Showcase`"}} python/using_packages -.-> lab-48642{{"`Custom Matplotlib Projection Showcase`"}} python/standard_libraries -.-> lab-48642{{"`Custom Matplotlib Projection Showcase`"}} python/classes_objects -.-> lab-48642{{"`Custom Matplotlib Projection Showcase`"}} python/constructor -.-> lab-48642{{"`Custom Matplotlib Projection Showcase`"}} python/polymorphism -.-> lab-48642{{"`Custom Matplotlib Projection Showcase`"}} python/encapsulation -.-> lab-48642{{"`Custom Matplotlib Projection Showcase`"}} python/numerical_computing -.-> lab-48642{{"`Custom Matplotlib Projection Showcase`"}} python/data_visualization -.-> lab-48642{{"`Custom Matplotlib Projection Showcase`"}} python/build_in_functions -.-> lab-48642{{"`Custom Matplotlib Projection Showcase`"}} end

Import Libraries

First, we will import the necessary libraries to create a custom projection.

import numpy as np
import matplotlib
from matplotlib.axes import Axes
import matplotlib.axis as maxis
from matplotlib.patches import Circle
from matplotlib.path import Path
from matplotlib.projections import register_projection
import matplotlib.spines as mspines
from matplotlib.ticker import FixedLocator, Formatter, NullLocator
from matplotlib.transforms import Affine2D, BboxTransformTo, Transform

Create GeoAxes Class

We will create an abstract base class for geographic projections called GeoAxes.

class GeoAxes(Axes):
    """
    An abstract base class for geographic projections
    """

    class ThetaFormatter(Formatter):
        """
        Used to format the theta tick labels.  Converts the native
        unit of radians into degrees and adds a degree symbol.
        """
        def __init__(self, round_to=1.0):
            self._round_to = round_to

        def __call__(self, x, pos=None):
            degrees = round(np.rad2deg(x) / self._round_to) * self._round_to
            return f"{degrees:0.0f}\N{DEGREE SIGN}"

    RESOLUTION = 75

    def _init_axis(self):
        self.xaxis = maxis.XAxis(self)
        self.yaxis = maxis.YAxis(self)
        ## Do not register xaxis or yaxis with spines -- as done in
        ## Axes._init_axis() -- until GeoAxes.xaxis.clear() works.
        ## self.spines['geo'].register_axis(self.yaxis)

    def clear(self):
        ## docstring inherited
        super().clear()

        self.set_longitude_grid(30)
        self.set_latitude_grid(15)
        self.set_longitude_grid_ends(75)
        self.xaxis.set_minor_locator(NullLocator())
        self.yaxis.set_minor_locator(NullLocator())
        self.xaxis.set_ticks_position('none')
        self.yaxis.set_ticks_position('none')
        self.yaxis.set_tick_params(label1On=True)
        ## Why do we need to turn on yaxis tick labels, but
        ## xaxis tick labels are already on?

        self.grid(rcParams['axes.grid'])

        Axes.set_xlim(self, -np.pi, np.pi)
        Axes.set_ylim(self, -np.pi / 2.0, np.pi / 2.0)

Create HammerAxes Class

We will create a custom class for the Aitoff-Hammer projection, an equal-area map projection called HammerAxes.

class HammerAxes(GeoAxes):
    """
    A custom class for the Aitoff-Hammer projection, an equal-area map
    projection.

    https://en.wikipedia.org/wiki/Hammer_projection
    """

    ## The projection must specify a name. This will be used by the
    ## user to select the projection,
    ## i.e. ``subplot(projection='custom_hammer')``.
    name = 'custom_hammer'

    class HammerTransform(Transform):
        """The base Hammer transform."""
        input_dims = output_dims = 2

        def __init__(self, resolution):
            """
            Create a new Hammer transform.  Resolution is the number of steps
            to interpolate between each input line segment to approximate its
            path in curved Hammer space.
            """
            Transform.__init__(self)
            self._resolution = resolution

        def transform_non_affine(self, ll):
            longitude, latitude = ll.T

            ## Pre-compute some values
            half_long = longitude / 2
            cos_latitude = np.cos(latitude)
            sqrt2 = np.sqrt(2)

            alpha = np.sqrt(1 + cos_latitude * np.cos(half_long))
            x = (2 * sqrt2) * (cos_latitude * np.sin(half_long)) / alpha
            y = (sqrt2 * np.sin(latitude)) / alpha
            return np.column_stack([x, y])

        def transform_path_non_affine(self, path):
            ## vertices = path.vertices
            ipath = path.interpolated(self._resolution)
            return Path(self.transform(ipath.vertices), ipath.codes)

        def inverted(self):
            return HammerAxes.InvertedHammerTransform(self._resolution)

    class InvertedHammerTransform(Transform):
        input_dims = output_dims = 2

        def __init__(self, resolution):
            Transform.__init__(self)
            self._resolution = resolution

        def transform_non_affine(self, xy):
            x, y = xy.T
            z = np.sqrt(1 - (x / 4) ** 2 - (y / 2) ** 2)
            longitude = 2 * np.arctan((z * x) / (2 * (2 * z ** 2 - 1)))
            latitude = np.arcsin(y*z)
            return np.column_stack([longitude, latitude])

        def inverted(self):
            return HammerAxes.HammerTransform(self._resolution)

    def __init__(self, *args, **kwargs):
        self._longitude_cap = np.pi / 2.0
        super().__init__(*args, **kwargs)
        self.set_aspect(0.5, adjustable='box', anchor='C')
        self.clear()

    def _get_core_transform(self, resolution):
        return self.HammerTransform(resolution)

Register Projection

Now we will register the projection with Matplotlib so the user can select it.

register_projection(HammerAxes)

Create Example

Finally, we will create an example using the custom projection.

if __name__ == '__main__':
    import matplotlib.pyplot as plt

    ## Now make a simple example using the custom projection.
    fig, ax = plt.subplots(subplot_kw={'projection': 'custom_hammer'})
    ax.plot([-1, 1, 1], [-1, -1, 1], "o-")
    ax.grid()

    plt.show()

Summary

In this lab, we learned how to create a custom projection using Matplotlib. We created a custom projection called Hammer projection by alleviating many features of Matplotlib. We learned how to create GeoAxes class, HammerAxes class, register the projection, and create an example using the custom projection.

Other Python Tutorials you may like