Skip to content

Instantly share code, notes, and snippets.

@afvincent
Created March 18, 2017 09:50
Show Gist options
  • Save afvincent/9c944f3f4df5ccb68af1dc889fcd127f to your computer and use it in GitHub Desktop.
Save afvincent/9c944f3f4df5ccb68af1dc889fcd127f to your computer and use it in GitHub Desktop.
Pcolor demo with LogNorm in a case with outliers, for different colormaps
"""
Based on the pcolor_log example:
http://matplotlib.org/examples/pylab_examples/pcolor_log.html
More about colormaps here:
http://matplotlib.org/users/colormaps.html
"""
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import LogNorm
from matplotlib.mlab import bivariate_normal # to create dummy data
from matplotlib.ticker import NullLocator # for axes cosmeticks
# Dummy data
N = 100
X, Y = np.mgrid[-3:3:N*1j, -2:2:N*1j]
Z = (bivariate_normal(X, Y, 0.1, 0.2, 1.0, 1.0) +
0.1 * bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0))
# Make outliers of some random points
np.random.seed(123456)
outliers = np.random.randint(low=0, high=N, size=(25, 2))
Z[outliers[:, 0], outliers[:, 1]] *= 1000
fig, axs = plt.subplots(nrows=2, ncols=2, figsize=(9.6, 6.4))
for (ax, cmap) in zip(axs.flat, ('viridis', 'gnuplot2', 'cubehelix', 'brg')):
# Display each matrix element with a color (in a log. scale)
im = ax.pcolor(X, Y, Z, norm=LogNorm(vmin=Z.min(), vmax=Z.max()),
cmap=cmap)
plt.colorbar(im, ax=ax)
# Remove the axes ticks for my eyes pleasure
ax.xaxis.set_major_locator(NullLocator())
ax.yaxis.set_major_locator(NullLocator())
plt.tight_layout()
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment