Skip to content

Commit fb36a14

Browse files
committed
Add colormap() and contourf() functions
1 parent 22318ea commit fb36a14

File tree

6 files changed

+99
-1
lines changed

6 files changed

+99
-1
lines changed

examples/wavelet/plot_wavelet.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
import numpy as np
1919
from matplotlib import pyplot as plt
2020

21+
import pymodalib
22+
2123
os.chdir(os.path.abspath(os.path.dirname(__file__)))
2224

2325
print("Plotting...")
@@ -39,7 +41,7 @@
3941

4042
fig, (ax1, ax2) = plt.subplots(1, 2, gridspec_kw={"width_ratios": [3, 1,]}, sharey=True)
4143

42-
ax1.contourf(mesh1, mesh2, wt_power)
44+
pymodalib.contourf(ax1, mesh1, mesh2, wt_power)
4345
ax1.set_title(f"WT power ({impl} implementation)")
4446
ax1.set_xlabel("Time (s)")
4547

pymodalib/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from pymodalib.algorithms.signals import resampl_flow as downsample
2323
from pymodalib.algorithms.wavelet import wavelet_transform
2424
from pymodalib.algorithms.windowed_fourier import windowed_fourier_transform
25+
from pymodalib.utils.plotting import colormap, contourf
2526
from pymodalib.utils.cache import cachedarray, cleanup
2627

2728
# PyMODAlib version.
@@ -42,6 +43,8 @@
4243
ridge_extraction,
4344
ecurve,
4445
rectfr,
46+
colormap,
47+
contourf,
4548
)
4649

4750
# Useful aliases for functions.

pymodalib/utils/colormap.mat

833 Bytes
Binary file not shown.

pymodalib/utils/plotting.py

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
# PyMODAlib, a Python implementation of the algorithms from MODA (Multiscale Oscillatory Dynamics Analysis).
2+
# Copyright (C) 2020 Lancaster University
3+
#
4+
# This program is free software: you can redistribute it and/or modify
5+
# it under the terms of the GNU General Public License as published by
6+
# the Free Software Foundation, either version 3 of the License, or
7+
# (at your option) any later version.
8+
#
9+
# This program is distributed in the hope that it will be useful,
10+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
# GNU General Public License for more details.
13+
#
14+
# You should have received a copy of the GNU General Public License
15+
# along with this program. If not, see <https://www.gnu.org/licenses/>.
16+
import warnings
17+
18+
import numpy as np
19+
from numpy import ndarray
20+
21+
22+
def contourf(
23+
axes, x: ndarray, y: ndarray, z: ndarray, levels: int = 256, *args, **kwargs
24+
) -> None:
25+
"""
26+
Plots a contour plot in PyMODA style. Useful for easily plotting a wavelet transform, etc.
27+
28+
This function is a Wrapper around matplotlib's 'contourf'.
29+
30+
Parameters
31+
----------
32+
axes
33+
The Axes object to plot on.
34+
x, y : ndarray
35+
The coordinates of the values in Z.
36+
37+
X and Y must both be 2-D with the same shape as Z (e.g. created via numpy.meshgrid),
38+
or they must both be 1-D such that len(X) == M is the number of columns in Z and
39+
len(Y) == N is the number of rows in Z.
40+
z : ndarray
41+
The height values over which the contour is drawn.
42+
levels : int
43+
Determines the number and positions of the contour lines / regions.
44+
45+
If an int n, use n data intervals; i.e. draw n+1 contour lines. The level heights are automatically chosen.
46+
47+
If array-like, draw contour lines at the specified levels. The values must be in increasing order.
48+
49+
.. note::
50+
Documentation copied from the relevant matplotlib function, `matplotlib.pyplot.contourf`.
51+
"""
52+
axes.contourf(
53+
x,
54+
y,
55+
z,
56+
levels,
57+
vmin=np.nanmin(z),
58+
vmax=np.nanmax(z),
59+
cmap=colormap(),
60+
*args,
61+
**kwargs
62+
)
63+
64+
65+
def colormap() -> "LinearSegmentedColormap":
66+
"""
67+
Loads the colormap used by PyMODA.
68+
69+
Returns
70+
-------
71+
LinearSegmentedColormap
72+
The colormap, as an object which can be passed to matplotlib functions.
73+
"""
74+
from matplotlib.colors import LinearSegmentedColormap
75+
from scipy.io import loadmat
76+
import os
77+
78+
here = os.path.abspath(os.path.dirname(__file__))
79+
filename = os.path.join(here, "colormap.mat")
80+
cmap = loadmat(filename).get("cmap")
81+
82+
if cmap is None:
83+
warnings.warn(
84+
"Could not load colormap. The colormap data is not supplied with "
85+
"source-distributions of PyMODAlib.",
86+
RuntimeWarning,
87+
)
88+
return "jet"
89+
90+
return LinearSegmentedColormap.from_list("colours", cmap, N=len(cmap), gamma=1.0)

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
AsyncProcessScheduler>=0.5.0b1
22
numpy>=1.18.1
33
scipy>=1.4.1
4+
matplotlib>=3.1.1

setup.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ def find_packages() -> List[str]:
5555
name="PyMODAlib",
5656
version=version,
5757
packages=find_packages(),
58+
include_package_data=True,
59+
package_data={"": ["*.mat"]},
5860
python_requires=">=3.6",
5961
install_requires=requirements,
6062
description="Library providing Python implementations of MODA's algorithms.",

0 commit comments

Comments
 (0)