|
| 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) |
0 commit comments