Matplotlib Contour Line Visualization

PythonPythonBeginner
Practice Now

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

Introduction

This lab will guide you on how to display your own contour lines and polygons using ContourSet in Matplotlib. Contour lines for each level are a list/tuple of polygons. Filled contours between two levels are also a list/tuple of polygons. Points can be ordered clockwise or anticlockwise.

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/FileHandlingGroup(["`File Handling`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/PythonStandardLibraryGroup(["`Python Standard Library`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") python/FileHandlingGroup -.-> python/with_statement("`Using with Statement`") 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/ControlFlowGroup -.-> python/for_loops("`For Loops`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/ModulesandPackagesGroup -.-> python/using_packages("`Using Packages`") python/PythonStandardLibraryGroup -.-> python/data_collections("`Data Collections`") python/DataScienceandMachineLearningGroup -.-> python/data_visualization("`Data Visualization`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/comments -.-> lab-48622{{"`Matplotlib Contour Line Visualization`"}} python/with_statement -.-> lab-48622{{"`Matplotlib Contour Line Visualization`"}} matplotlib/importing_matplotlib -.-> lab-48622{{"`Matplotlib Contour Line Visualization`"}} matplotlib/figures_axes -.-> lab-48622{{"`Matplotlib Contour Line Visualization`"}} python/variables_data_types -.-> lab-48622{{"`Matplotlib Contour Line Visualization`"}} python/booleans -.-> lab-48622{{"`Matplotlib Contour Line Visualization`"}} python/for_loops -.-> lab-48622{{"`Matplotlib Contour Line Visualization`"}} python/lists -.-> lab-48622{{"`Matplotlib Contour Line Visualization`"}} python/tuples -.-> lab-48622{{"`Matplotlib Contour Line Visualization`"}} python/importing_modules -.-> lab-48622{{"`Matplotlib Contour Line Visualization`"}} python/using_packages -.-> lab-48622{{"`Matplotlib Contour Line Visualization`"}} python/data_collections -.-> lab-48622{{"`Matplotlib Contour Line Visualization`"}} python/data_visualization -.-> lab-48622{{"`Matplotlib Contour Line Visualization`"}} python/build_in_functions -.-> lab-48622{{"`Matplotlib Contour Line Visualization`"}} end

Import the necessary libraries

The first step is to import the necessary libraries. In this lab, we will be using Matplotlib.

import matplotlib.pyplot as plt
import matplotlib.cm as cm
from matplotlib.contour import ContourSet
from matplotlib.path import Path

Define the contour lines and polygons

The next step is to define the contour lines and polygons. In this example, we have lines and filled contours between two levels.

## Contour lines for each level are a list/tuple of polygons.
lines0 = [[[0, 0], [0, 4]]]
lines1 = [[[2, 0], [1, 2], [1, 3]]]
lines2 = [[[3, 0], [3, 2]], [[3, 3], [3, 4]]]  ## Note two lines.

## Filled contours between two levels are also a list/tuple of polygons.
## Points can be ordered clockwise or anticlockwise.
filled01 = [[[0, 0], [0, 4], [1, 3], [1, 2], [2, 0]]]
filled12 = [[[2, 0], [3, 0], [3, 2], [1, 3], [1, 2]],   ## Note two polygons.
            [[1, 4], [3, 4], [3, 3]]]

Create the plot

The next step is to create the plot. This can be done using the ContourSet function.

fig, ax = plt.subplots()

## Filled contours using filled=True.
cs = ContourSet(ax, [0, 1, 2], [filled01, filled12], filled=True, cmap=cm.bone)
cbar = fig.colorbar(cs)

## Contour lines (non-filled).
lines = ContourSet(
    ax, [0, 1, 2], [lines0, lines1, lines2], cmap=cm.cool, linewidths=3)
cbar.add_lines(lines)

ax.set(xlim=(-0.5, 3.5), ylim=(-0.5, 4.5),
       title='User-specified contours')

Create filled contours with holes

Multiple filled contour lines can be specified in a single list of polygon vertices along with a list of vertex kinds (code types) as described in the Path class. This is particularly useful for polygons with holes.

fig, ax = plt.subplots()
filled01 = [[[0, 0], [3, 0], [3, 3], [0, 3], [1, 1], [1, 2], [2, 2], [2, 1]]]
M = Path.MOVETO
L = Path.LINETO
kinds01 = [[M, L, L, L, M, L, L, L]]
cs = ContourSet(ax, [0, 1], [filled01], [kinds01], filled=True)
cbar = fig.colorbar(cs)

ax.set(xlim=(-0.5, 3.5), ylim=(-0.5, 3.5),
       title='User specified filled contours with holes')

Summary

In this lab, we learned how to display our own contour lines and polygons using ContourSet in Matplotlib. We defined the contour lines and polygons, created the plot, and created filled contours with holes.

Other Python Tutorials you may like