Commit 516db68b authored by Parish, Chad's avatar Parish, Chad
Browse files

Fix small typos

parent 751286b6
%% Cell type:markdown id: tags:
# Choose colormaps carefully
%% Cell type:markdown id: tags:
Modern scientific visualization often requires coloring two- or three-dimensional maps according to some other dimension of the data. An aerodynamic simulation might color the flow of fluid around and a three-dimensional airfoil according to a fourth dimension such as velocity. Here, we'll use examples from analytical electron microscopy, where two- or three-dimensional maps will be colored accoring to additional dimensions, such as intensity of X-ray signals at each pixel, or calculated weight or atom percent of a constituent at each pixel.
<br><br>
Here, we have three datasets derived from a single scanning electron microscope (SEM) X-ray spectrum image. The datasets are: <br>
1. `counts`: Raw number of X-ray counts at each pixel. This is an example of a strictly non-negative, but smoothly varying, dataset.<br>
2. `svd`: A single abundance map derived from singular value decomposition (aka principal component analysis) of the dataset. This contains smoothly varying data with positive and negative values of roughly the same magnitude. (*) <br>
3. `kmeans`: A cluster assignment map dervied from K-means clustering of the data. This is strictly positive data, but mapped to discrete integer values (0, 1, 2...) rather than smoothly varying. <br>
<br>
These three maps are representative, then, of many types of higher-dimensional data presented as false color maps. We will illustrate using four types of colormaps: (1) smoothly varying and perceptually uniform; (2) diverging; (3) discrete or qualitative; and (4) rainbow.
<br>
<br>
(*) Those familiar with SVD might notice the values are too large to be an orthonormal basis set, as SVD requires. This abundance map is actually the relevant left singular vector multiplied by its associated singular value, resulting in the approximate range -7 to +7.
%% Cell type:markdown id: tags:
### Imports:
%% Cell type:code id: tags:
``` python
import numpy as np
import matplotlib.pyplot as plt
import h5py
import string
```
%% Cell type:markdown id: tags:
### Import the data
We have stored the three datasets into an HDF5-type file. HDF5 is the most versatile file format for storing and transferring scientific data and we recommend it for essentially any application that involves storing one or more numeric array.
%% Cell type:code id: tags:
``` python
with h5py.File('colormap_images.h5', 'r') as fh:
print(list(fh.keys()))
svd = np.array(fh['abundances'])
kmeans = np.array(fh['clusters'])
counts = np.array(fh['x-ray_counts'])
```
%% Output
['abundances', 'clusters', 'x-ray_counts']
%% Cell type:markdown id: tags:
### Prepare the data we will need to generate the figure
%% Cell type:code id: tags:
``` python
# Set up the data and human-readable names
data_list = [counts, svd, kmeans]
data_type = ['Smooth positive\nscale', 'Symmetric', 'Discrete scale']
# Set up the colormaps and human-readable names
# NOTE: 'twilight_shifted' is not availbile in Matplotlib versions pre-3.0.0.
# If you get errors on 'twilight-shifted', replace it with 'bwr' or 'seismic'
colormap_list = ['viridis', 'twilight_shifted', 'Paired', 'jet']
colormap_type = ['Perceptually uniform', 'Diverging','Qualitative', 'Rainbow']
# How big a matrix will we generate?
X = len(colormap_list)
Y = len(data_list)
# Get labels for figures
num_figs = X*Y
alphabet = string.ascii_lowercase
alphabet = alphabet[:num_figs]
# Find the color scale limits for the three datasets and prepare iterable lists
count_min = 0
count_max = counts.max()
svd_max = np.abs(svd).max()
svd_min = -svd_max
kmeans_min = 0
kmeans_max = kmeans.max()
data_min = [count_min, svd_min, kmeans_min]
data_max = [count_max, svd_max, kmeans_max]
```
%% Cell type:markdown id: tags:
### Create the figure
%% Cell type:code id: tags:
``` python
fs = 20 # figure size
fig, ax = plt.subplots(nrows=Y, ncols=X, figsize=(fs, fs*Y/X), sharex=True, sharey=True)
# set up some renderer parameters
plt.style.use({'font.sans-serif':'Arial', 'svg.fonttype': 'none'})
# 'svg.fonttype': 'none' will make the text editable in, for instance, Adobe Illusrtator
# loop over the data and colormaps to render
for i, x in enumerate(colormap_list):
for j, y in enumerate(data_list):
P = ax[j, i].imshow(y, cmap=x, vmin=data_min[j], vmax=data_max[j]) # plot
C = fig.colorbar(P, ax=ax[j, i], orientation='horizontal') # colorbar
C.ax.tick_params(labelsize=14) # colorbar font size
ax[j, i].tick_params(axis='both', which='both', bottom=False, top=False,
labelbottom=False, right=False, left=False, labelleft=False)
# turn off ticks, but leave axis present so we can use ylabel later
# add (a), (b), etc.
count = 0
for j in range(Y):
for i in range(X):
if alphabet[count] == 'a' or alphabet[count] == 'f' or alphabet[count] == 'k':
clr = 'red'
else:
clr = 'black'
ax[j, i].text(0.98, 0.02, '('+alphabet[count]+')', transform=ax[j,i].transAxes, fontsize=20,
verticalalignment='bottom', horizontalalignment='right', color=clr,
bbox={'facecolor':'white', 'alpha':0.7, 'pad':2, 'edgecolor':'white'})
count += 1
ax[j, i].
# Label the rows and columns
for i, x in enumerate(colormap_type):
ax[0, i].set_title(x, size=24)
for j, y in enumerate(data_type):
ax[j, 0].set_ylabel(y, size=24)
pass
```
%% Output
%% Cell type:markdown id: tags:
### Save the data
%% Cell type:code id: tags:
``` python
fig.savefig('colormaps.png', dpi=300)
fig.savefig('colormaps.svg', dpi=300)
```
%% Cell type:code id: tags:
``` python
```
%% Cell type:code id: tags:
``` python
```
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment