Python Matplotlib Unit Conversions

PythonPythonBeginner
Practice Now

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

Introduction

This lab will guide you through a step-by-step tutorial on how to perform unit conversions over masked arrays using Python Matplotlib.

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`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/PlottingDataGroup(["`Plotting Data`"]) 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/BasicConceptsGroup -.-> python/comments("`Comments`") matplotlib/BasicConceptsGroup -.-> matplotlib/importing_matplotlib("`Importing Matplotlib`") matplotlib/BasicConceptsGroup -.-> matplotlib/figures_axes("`Understanding Figures and Axes`") matplotlib/PlottingDataGroup -.-> matplotlib/scatter_plots("`Scatter Plots`") python/BasicConceptsGroup -.-> python/variables_data_types("`Variables and Data Types`") python/BasicConceptsGroup -.-> python/numeric_types("`Numeric Types`") python/BasicConceptsGroup -.-> python/booleans("`Booleans`") 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/numerical_computing("`Numerical Computing`") python/DataScienceandMachineLearningGroup -.-> python/data_visualization("`Data Visualization`") subgraph Lab Skills python/comments -.-> lab-49018{{"`Python Matplotlib Unit Conversions`"}} matplotlib/importing_matplotlib -.-> lab-49018{{"`Python Matplotlib Unit Conversions`"}} matplotlib/figures_axes -.-> lab-49018{{"`Python Matplotlib Unit Conversions`"}} matplotlib/scatter_plots -.-> lab-49018{{"`Python Matplotlib Unit Conversions`"}} python/variables_data_types -.-> lab-49018{{"`Python Matplotlib Unit Conversions`"}} python/numeric_types -.-> lab-49018{{"`Python Matplotlib Unit Conversions`"}} python/booleans -.-> lab-49018{{"`Python Matplotlib Unit Conversions`"}} python/tuples -.-> lab-49018{{"`Python Matplotlib Unit Conversions`"}} python/importing_modules -.-> lab-49018{{"`Python Matplotlib Unit Conversions`"}} python/using_packages -.-> lab-49018{{"`Python Matplotlib Unit Conversions`"}} python/data_collections -.-> lab-49018{{"`Python Matplotlib Unit Conversions`"}} python/numerical_computing -.-> lab-49018{{"`Python Matplotlib Unit Conversions`"}} python/data_visualization -.-> lab-49018{{"`Python Matplotlib Unit Conversions`"}} end

Import Libraries

In this step, we will import the necessary libraries to perform the unit conversions and plotting.

import matplotlib.pyplot as plt
import numpy as np
from basic_units import hertz, minutes, secs

Create Masked Array

In this step, we will create a masked array and apply the mask to the data.

## create masked array
data = (1, 2, 3, 4, 5, 6, 7, 8)
mask = (1, 0, 1, 0, 0, 0, 1, 0)
xsecs = secs * np.ma.MaskedArray(data, mask, float)

Create Plots

In this step, we will create three plots using the masked array with different units.

## create subplots
fig, (ax1, ax2, ax3) = plt.subplots(nrows=3, sharex=True)

## plot 1
ax1.scatter(xsecs, xsecs)
ax1.yaxis.set_units(secs)

## plot 2
ax2.scatter(xsecs, xsecs, yunits=hertz)

## plot 3
ax3.scatter(xsecs, xsecs, yunits=minutes)

## set labels
ax1.set_ylabel('Seconds')
ax2.set_ylabel('Hertz')
ax3.set_ylabel('Minutes')
ax3.set_xlabel('Time')

Display Plots

In this step, we will display the plots that were created in the previous step.

## display plot
plt.show()

Summary

In this lab, we have learned how to perform unit conversions over masked arrays using Python Matplotlib. We have created a masked array and applied a mask to the data. We have also created three plots using the masked array with different units and displayed them.

Other Python Tutorials you may like