Matplotlib Data Visualization Techniques

PythonPythonBeginner
Practice Now

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

Introduction

Matplotlib is a powerful data visualization library in Python. It provides a variety of tools for creating a wide range of graphs, charts, and plots. One of the most powerful features of Matplotlib is its ability to scale data. This lab will introduce you to the AsinhScale, which is a transformation that allows for plotting quantities that cover a very wide dynamic range that includes both positive and negative values.

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`"]) linux(("`Linux`")) -.-> linux/PackagesandSoftwaresGroup(["`Packages and Softwares`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/BasicConceptsGroup(["`Basic Concepts`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/PlottingDataGroup(["`Plotting Data`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/PlotCustomizationGroup(["`Plot Customization`"]) 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`") linux/PackagesandSoftwaresGroup -.-> linux/pip("`Python Package Installing`") matplotlib/BasicConceptsGroup -.-> matplotlib/importing_matplotlib("`Importing Matplotlib`") matplotlib/BasicConceptsGroup -.-> matplotlib/figures_axes("`Understanding Figures and Axes`") matplotlib/PlottingDataGroup -.-> matplotlib/line_plots("`Line Plots`") matplotlib/PlottingDataGroup -.-> matplotlib/scatter_plots("`Scatter Plots`") matplotlib/PlotCustomizationGroup -.-> matplotlib/legend_config("`Legend Configuration`") matplotlib/PlotCustomizationGroup -.-> matplotlib/grid_config("`Grid Configuration`") python/BasicConceptsGroup -.-> python/booleans("`Booleans`") python/ControlFlowGroup -.-> python/for_loops("`For Loops`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/DataStructuresGroup -.-> python/dictionaries("`Dictionaries`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/ModulesandPackagesGroup -.-> python/standard_libraries("`Common Standard Libraries`") python/PythonStandardLibraryGroup -.-> python/math_random("`Math and Random`") 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-48556{{"`Matplotlib Data Visualization Techniques`"}} linux/pip -.-> lab-48556{{"`Matplotlib Data Visualization Techniques`"}} matplotlib/importing_matplotlib -.-> lab-48556{{"`Matplotlib Data Visualization Techniques`"}} matplotlib/figures_axes -.-> lab-48556{{"`Matplotlib Data Visualization Techniques`"}} matplotlib/line_plots -.-> lab-48556{{"`Matplotlib Data Visualization Techniques`"}} matplotlib/scatter_plots -.-> lab-48556{{"`Matplotlib Data Visualization Techniques`"}} matplotlib/legend_config -.-> lab-48556{{"`Matplotlib Data Visualization Techniques`"}} matplotlib/grid_config -.-> lab-48556{{"`Matplotlib Data Visualization Techniques`"}} python/booleans -.-> lab-48556{{"`Matplotlib Data Visualization Techniques`"}} python/for_loops -.-> lab-48556{{"`Matplotlib Data Visualization Techniques`"}} python/tuples -.-> lab-48556{{"`Matplotlib Data Visualization Techniques`"}} python/dictionaries -.-> lab-48556{{"`Matplotlib Data Visualization Techniques`"}} python/importing_modules -.-> lab-48556{{"`Matplotlib Data Visualization Techniques`"}} python/standard_libraries -.-> lab-48556{{"`Matplotlib Data Visualization Techniques`"}} python/math_random -.-> lab-48556{{"`Matplotlib Data Visualization Techniques`"}} python/numerical_computing -.-> lab-48556{{"`Matplotlib Data Visualization Techniques`"}} python/data_visualization -.-> lab-48556{{"`Matplotlib Data Visualization Techniques`"}} python/build_in_functions -.-> lab-48556{{"`Matplotlib Data Visualization Techniques`"}} end

Install Matplotlib

Before starting, make sure that Matplotlib is installed. You can install it using pip command as follows:

pip install matplotlib

Import Required Libraries

To use the AsinhScale, we need to import the Matplotlib library and the numpy library. Numpy is a powerful numerical computing library in Python that is often used in conjunction with Matplotlib.

import matplotlib.pyplot as plt
import numpy as np

Create Sample Data

Before we can plot data using the AsinhScale, we need to create some sample data. We will create a simple line graph using numpy's linspace method.

## Prepare sample values for variations on y=x graph:
x = np.linspace(-3, 6, 500)

Compare "symlog" and "asinh" behaviour on sample y=x graph

We will compare the behaviour of "symlog" and "asinh" on a sample y=x graph. We will plot the same graph twice, once with "symlog" and once with "asinh".

fig1 = plt.figure()
ax0, ax1 = fig1.subplots(1, 2, sharex=True)

ax0.plot(x, x)
ax0.set_yscale('symlog')
ax0.grid()
ax0.set_title('symlog')

ax1.plot(x, x)
ax1.set_yscale('asinh')
ax1.grid()
ax1.set_title('asinh')

Compare "asinh" graphs with different scale parameter "linear_width"

We will now compare "asinh" graphs with different scale parameters "linear_width". We will plot three graphs with different "linear_width" values.

fig2 = plt.figure(layout='constrained')
axs = fig2.subplots(1, 3, sharex=True)
for ax, (a0, base) in zip(axs, ((0.2, 2), (1.0, 0), (5.0, 10))):
    ax.set_title(f'linear_width={a0:.3g}')
    ax.plot(x, x, label='y=x')
    ax.plot(x, 10*x, label='y=10x')
    ax.plot(x, 100*x, label='y=100x')
    ax.set_yscale('asinh', linear_width=a0, base=base)
    ax.grid()
    ax.legend(loc='best', fontsize='small')

Compare "symlog" and "asinh" scalings on 2D Cauchy-distributed random numbers

Finally, we will compare "symlog" and "asinh" scalings on 2D Cauchy-distributed random numbers. We will plot the same graph twice, once with "symlog" and once with "asinh".

fig3 = plt.figure()
ax = fig3.subplots(1, 1)
r = 3 * np.tan(np.random.uniform(-np.pi / 2.02, np.pi / 2.02,
                                 size=(5000,)))
th = np.random.uniform(0, 2*np.pi, size=r.shape)

ax.scatter(r * np.cos(th), r * np.sin(th), s=4, alpha=0.5)
ax.set_xscale('asinh')
ax.set_yscale('symlog')
ax.set_xlabel('asinh')
ax.set_ylabel('symlog')
ax.set_title('2D Cauchy random deviates')
ax.set_xlim(-50, 50)
ax.set_ylim(-50, 50)
ax.grid()

Summary

This lab introduced the AsinhScale in Matplotlib, which is a transformation that allows for plotting quantities that cover a very wide dynamic range that includes both positive and negative values. We learned how to create sample data and how to plot graphs with "symlog" and "asinh". We also learned how to compare "asinh" graphs with different scale parameters and how to compare "symlog" and "asinh" scalings on 2D Cauchy-distributed random numbers.

Other Python Tutorials you may like