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.

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