#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright (c) 2016 Jérémie DECOCK (http://www.jdhp.org)
# This script is provided under the terms and conditions of the MIT license:
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
__all__ = ['mpl_save',
'plot']
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
COLOR_MAP = cm.gnuplot2
[docs]def mpl_save(img, output_file_path, title=""):
"""
img should be a 2D numpy array.
"""
fig = plt.figure(figsize=(8.0, 8.0))
ax = fig.add_subplot(111)
ax.set_title(title, fontsize=24)
#im = ax.imshow(img,
# origin='lower',
# interpolation='nearest',
# vmin=min(img.min(), 0),
# cmap=COLOR_MAP)
# Manage NaN values (see http://stackoverflow.com/questions/2578752/how-can-i-plot-nan-values-as-a-special-color-with-imshow-in-matplotlib and http://stackoverflow.com/questions/38800532/plot-color-nan-values)
masked = np.ma.masked_where(np.isnan(img), img)
cmap = COLOR_MAP
cmap.set_bad('black')
im = ax.imshow(masked,
origin='lower',
interpolation='nearest',
cmap=cmap)
plt.colorbar(im) # draw the colorbar
plt.savefig(output_file_path, bbox_inches='tight')
plt.close('all')
[docs]def plot(img, title=""):
"""
img should be a 2D numpy array.
"""
fig = plt.figure(figsize=(8.0, 8.0))
ax = fig.add_subplot(111)
ax.set_title(title)
#im = ax.imshow(img,
# origin='lower',
# interpolation='nearest',
# vmin=min(img.min(), 0),
# cmap=COLOR_MAP)
# Manage NaN values (see http://stackoverflow.com/questions/2578752/how-can-i-plot-nan-values-as-a-special-color-with-imshow-in-matplotlib and http://stackoverflow.com/questions/38800532/plot-color-nan-values)
masked = np.ma.masked_where(np.isnan(img), img)
cmap = COLOR_MAP
cmap.set_bad('black')
im = ax.imshow(masked,
origin='lower',
interpolation='nearest',
cmap=cmap)
plt.colorbar(im) # draw the colorbar
plt.show()
def plot_hist(img, num_bins=50, logx=False, logy=False, x_max=None, title=""):
"""
"""
# Flatten + remove NaN values
flat_img = img[np.isfinite(img)]
fig = plt.figure(figsize=(8.0, 8.0))
ax = fig.add_subplot(111)
ax.set_title(title)
if logx:
# Setup the logarithmic scale on the X axis
vmin = np.log10(flat_img.min())
vmax = np.log10(flat_img.max())
bins = np.logspace(vmin, vmax, num_bins) # Make a range from 10**vmin to 10**vmax
else:
bins = num_bins
if x_max is not None:
ax.set_xlim(xmax=x_max)
res_tuple = ax.hist(flat_img,
bins=bins,
log=logy, # Set log scale on the Y axis
histtype='bar',
alpha=1)
if logx:
ax.set_xscale("log") # Activate log scale on X axis
plt.show()
def _plot_list(img_list,
title_list=None,
highlight_mask_list=None,
main_title=None):
"""Plot several images at once."""
num_imgs = len(img_list)
fig, ax_tuple = plt.subplots(nrows=1, ncols=num_imgs, figsize=(num_imgs*6, 6))
if title_list is None:
title_list = [None for i in img_list]
if highlight_mask_list is None:
highlight_mask_list = [None for i in img_list]
for ax, img, title in zip(ax_tuple, img_list, title_list):
masked = np.ma.masked_where(np.isnan(img), img)
cmap = COLOR_MAP
cmap.set_bad('black')
im = ax.imshow(masked,
origin='lower',
interpolation='nearest',
cmap=cmap)
ax.set_title(title)
plt.colorbar(im, ax=ax) # draw the colorbar
if main_title is not None:
fig.suptitle(main_title, fontsize=18)
plt.subplots_adjust(top=0.85)
def plot_list(img_list,
title_list=None,
highlight_mask_list=None,
metadata_dict=None):
"""Plot several images at once.
Parameters
----------
img_list
A list of 2D numpy array to plot.
"""
# Main title
main_title = None
_plot_list(img_list,
title_list=title_list,
highlight_mask_list=highlight_mask_list,
main_title=main_title)
plt.show()
def mpl_save_list(img_list,
output_file_path,
title_list=None,
highlight_mask_list=None,
metadata_dict=None):
"""Plot several images at once.
Parameters
----------
img_list
A list of 2D numpy array to plot.
"""
# Main title
main_title = None
_plot_list(img_list,
title_list=title_list,
highlight_mask_list=highlight_mask_list,
main_title=main_title)
plt.savefig(output_file_path, bbox_inches='tight')
plt.close('all')