Matplotlib Shaded Plot Visualization

MatplotlibMatplotlibBeginner
Practice Now

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

Introduction

This lab will walk you through the process of creating shaded plots in Matplotlib using different techniques. You will learn how to display a colorbar for a shaded plot, avoid outliers in a shaded plot, and display different variables through shade and color.

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`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/PlottingDataGroup(["`Plotting Data`"]) python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) 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`") 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/heatmaps("`Heatmaps`") python/BasicConceptsGroup -.-> python/variables_data_types("`Variables and Data Types`") python/ControlFlowGroup -.-> python/for_loops("`For Loops`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/ModulesandPackagesGroup -.-> python/using_packages("`Using Packages`") python/PythonStandardLibraryGroup -.-> python/data_collections("`Data Collections`") 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-48531{{"`Matplotlib Shaded Plot Visualization`"}} python/with_statement -.-> lab-48531{{"`Matplotlib Shaded Plot Visualization`"}} matplotlib/importing_matplotlib -.-> lab-48531{{"`Matplotlib Shaded Plot Visualization`"}} matplotlib/figures_axes -.-> lab-48531{{"`Matplotlib Shaded Plot Visualization`"}} matplotlib/heatmaps -.-> lab-48531{{"`Matplotlib Shaded Plot Visualization`"}} python/variables_data_types -.-> lab-48531{{"`Matplotlib Shaded Plot Visualization`"}} python/for_loops -.-> lab-48531{{"`Matplotlib Shaded Plot Visualization`"}} python/lists -.-> lab-48531{{"`Matplotlib Shaded Plot Visualization`"}} python/tuples -.-> lab-48531{{"`Matplotlib Shaded Plot Visualization`"}} python/function_definition -.-> lab-48531{{"`Matplotlib Shaded Plot Visualization`"}} python/importing_modules -.-> lab-48531{{"`Matplotlib Shaded Plot Visualization`"}} python/using_packages -.-> lab-48531{{"`Matplotlib Shaded Plot Visualization`"}} python/data_collections -.-> lab-48531{{"`Matplotlib Shaded Plot Visualization`"}} python/numerical_computing -.-> lab-48531{{"`Matplotlib Shaded Plot Visualization`"}} python/data_visualization -.-> lab-48531{{"`Matplotlib Shaded Plot Visualization`"}} python/build_in_functions -.-> lab-48531{{"`Matplotlib Shaded Plot Visualization`"}} end

Displaying a Colorbar for a Shaded Plot

In this step, you will learn how to display a correct numeric colorbar for a shaded plot.

import matplotlib.pyplot as plt
import numpy as np

from matplotlib.colors import LightSource, Normalize

def display_colorbar():
    """Display a correct numeric colorbar for a shaded plot."""
    y, x = np.mgrid[-4:2:200j, -4:2:200j]
    z = 10 * np.cos(x**2 + y**2)

    cmap = plt.cm.copper
    ls = LightSource(315, 45)
    rgb = ls.shade(z, cmap)

    fig, ax = plt.subplots()
    ax.imshow(rgb, interpolation='bilinear')

    ## Use a proxy artist for the colorbar...
    im = ax.imshow(z, cmap=cmap)
    im.remove()
    fig.colorbar(im, ax=ax)

    ax.set_title('Using a colorbar with a shaded plot', size='x-large')

Avoiding Outliers in Shaded Plots

In this step, you will learn how to use a custom norm to control the displayed z-range of a shaded plot.

def avoid_outliers():
    """Use a custom norm to control the displayed z-range of a shaded plot."""
    y, x = np.mgrid[-4:2:200j, -4:2:200j]
    z = 10 * np.cos(x**2 + y**2)

    ## Add some outliers...
    z[100, 105] = 2000
    z[120, 110] = -9000

    ls = LightSource(315, 45)
    fig, (ax1, ax2) = plt.subplots(ncols=2, figsize=(8, 4.5))

    rgb = ls.shade(z, plt.cm.copper)
    ax1.imshow(rgb, interpolation='bilinear')
    ax1.set_title('Full range of data')

    rgb = ls.shade(z, plt.cm.copper, vmin=-10, vmax=10)
    ax2.imshow(rgb, interpolation='bilinear')
    ax2.set_title('Manually set range')

    fig.suptitle('Avoiding Outliers in Shaded Plots', size='x-large')

Displaying Different Variables Through Shade and Color

In this step, you will learn how to display different variables through shade and color.

def shade_other_data():
    """Demonstrates displaying different variables through shade and color."""
    y, x = np.mgrid[-4:2:200j, -4:2:200j]
    z1 = np.sin(x**2)  ## Data to hillshade
    z2 = np.cos(x**2 + y**2)  ## Data to color

    norm = Normalize(z2.min(), z2.max())
    cmap = plt.cm.RdBu

    ls = LightSource(315, 45)
    rgb = ls.shade_rgb(cmap(norm(z2)), z1)

    fig, ax = plt.subplots()
    ax.imshow(rgb, interpolation='bilinear')
    ax.set_title('Shade by one variable, color by another', size='x-large')

Summary

In this lab, you learned how to create shaded plots in Matplotlib using different techniques, including displaying a colorbar for a shaded plot, avoiding outliers in a shaded plot, and displaying different variables through shade and color. These techniques can be useful for visualizing and exploring data in a variety of applications.

Other Matplotlib Tutorials you may like