mantidproject / plotting-evaluation

Contains prototypes for evaluating future plotting packages in Mantid
0 stars 0 forks source link

2D contour plot with NaN regions #12

Open martyngigg opened 8 years ago

martyngigg commented 8 years ago

How do 2D colour-fill plots with NaN regions look in matplolib?

Something like the output of an SofQW calculation would be a good test case.

quantumsteve commented 8 years ago

By default pcolormesh appears to plot NaN as the lowest value in the colormap

import matplotlib.pyplot as plt
import numpy as np

plt.rcParams['image.cmap'] = 'viridis'
plt.rcParams['figure.figsize'] = 10,10

# make these smaller to increase the resolution
dx, dy = 0.15, 0.05

# generate 2 2d grids for the x & y bounds
y, x = np.mgrid[slice(-3, 3 + dy, dy),
                slice(-3, 3 + dx, dx)]
z = (1 - x / 2. + x ** 5 + y ** 3) * np.exp(-x ** 2 - y ** 2)
#z[5,5] = float('nan')
# x and y are bounds, so z should be the value *inside* those bounds.
# Therefore, remove the last value from the z array.
z = z[:-1, :-1]
z_min, z_max = -np.abs(z).max(), np.abs(z).max()

plt.subplot(2, 1, 1)
plt.pcolormesh(x, y, z, vmin=z_min, vmax=z_max)
plt.title('pcolormesh')
# set the limits of the plot to the limits of the data
plt.axis([x.min(), x.max(), y.min(), y.max()])
plt.colorbar()

z[0:10,0:10] = float('NaN')

plt.subplot(2, 1, 2)
plt.pcolormesh(x, y, z, vmin=z_min, vmax=z_max)
plt.title('pcolormesh with NaN')
# set the limits of the plot to the limits of the data
plt.axis([x.min(), x.max(), y.min(), y.max()])
plt.colorbar()

plt.savefig('pcolormesh.png',dpi=400,bbox_inches='tight')

image

NickDraper commented 8 years ago

The colorbars in matplotlib appear to have specific values for over under and bad (masked). These include alpha to allow the area to be transparent.

From the Mantidplot.Color Docs: http://matplotlib.org/api/colors_api.html?highlight=color#module-matplotlib.colors

set_bad(color='k', alpha=None) Set color to be used for masked values.

set_over(color='k', alpha=None) Set color to be used for high out-of-range values. Requires norm.clip = False

set_under(color='k', alpha=None) Set color to be used for low out-of-range values. Requires norm.clip = False