pymodalib.plotting package

Module contents

Functions for plotting the results of PyMODAlib algorithms.

colormap() → LinearSegmentedColormap

Loads the colormap used by PyMODA.

"""
Example usage of 'colormap()'.

Assume that the data from a wavelet transform has already been calculated.
"""
import pymodalib
from matplotlib import pyplot as plt

cmap = pymodalib.colormap()

# Pass the colormap to a matplotlib function using the 'cmap' keyword argument.
# Note that this is matplotlib's 'contourf', not PyMODAlib's 'contourf'.
plt.contourf(mesh1, mesh2, amp_wt, cmap=cmap)
Returns:The colormap, as an object which can be passed to matplotlib functions.
Return type:LinearSegmentedColormap
contourf(axes, x: numpy.ndarray, y: numpy.ndarray, z: numpy.ndarray, levels: int = 256, vmin=None, vmax=None, cmap=None, subsample: bool = True, subsample_width: int = 3840, log=False, *args, **kwargs) → matplotlib.contour.QuadContourSet

Plots a contour plot in PyMODA style. Useful for easily plotting a wavelet transform.

This function is a wrapper around matplotlib’s ‘contourf’.

Note

Most of this documentation was copied from the relevant matplotlib function, matplotlib.pyplot.contourf.

"""
Example of plotting the wavelet transform of a signal using this function.
"""
import numpy as np
from matplotlib import pyplot as plt
import pymodalib

# Load the signal from a data file.
signal = np.load("some_data_file.npy")

# Sampling frequency of 10Hz.
fs = 10

# Time values for the signal.
times = pymodalib.generate_times(signal, fs)

# Calculate the wavelet transform.
wt, freq = pymodalib.wavelet_transform(signal, fs)

# Amplitude of the wavelet transform.
amp = np.abs(wt)

# Create the Axes object.
fig, ax = plt.subplots()

# Plot the wavelet transform.
mesh1, mesh2 = np.meshgrid(times, freq)
pymodalib.contourf(ax, mesh1, mesh2, amp)

# Set log scale, labels etc.
ax.set_yscale("log")
ax.set_xlabel("Time (s)")
ax.set_ylabel("Frequency (Hz)")
ax.set_title("Amplitude of wavelet transform")

# Show the plot.
plt.show()
Parameters:
  • axes – The Axes object to plot on.

  • x, y (ndarray) – The coordinates of the values in Z.

    X and Y must both be 2-D with the same shape as Z (e.g. created via numpy.meshgrid), or they must both be 1-D such that len(X) == M is the number of columns in Z and len(Y) == N is the number of rows in Z.

  • z (ndarray) – The height values over which the contour is drawn.

  • levels (int) – Determines the number and positions of the contour lines / regions.

    If an int n, use n data intervals; i.e. draw n+1 contour lines. The level heights are automatically chosen.

    If array-like, draw contour lines at the specified levels. The values must be in increasing order.

  • vmin (float, None) – The minimum value, used to calibrate the colormap. If None, the minimum value of the array will be used.

  • vmax (float, None) – The maximum value, used to calibrate the colormap. If None, the maximum value of the array will be used.

  • cmap (str, Colormap, None) – The colormap to use. If left to None, the PyMODAlib colormap will be used.

  • subsample (bool) – (Default = True) Whether to subsample the data, greatly improving plotting performance.

  • subsample_width (int) – (Default = 3840) The target width of the subsampled data. If this width is more than the width of the screen in pixels, the effect of subsampling will be negligible.

  • log (bool) – (Default = False) Whether to use a logarithmic scale on the y-axis. This is useful when plotting a wavelet transform.

  • *args (optional) – Arguments to pass to matplotlib’s contourf function.

  • *kwargs (optional) – Keyword arguments to pass to matplotlib’s contourf function.

Returns:

The value returned by matplotlib’s contourf.

Return type:

matplotlib.contour.QuadContourSet