<p>UPDATE Sep 13 2018:</p>
The PR has been merged and you can just install with pip install cmaputil
without having to pull the branch.
</div>
PNNL’s cmaputil
library can be downloaded from their github page or installed via pip install cmaputil
. However, for the use cases I’m showing here, that won’t quite work. This PR shows why. If that PR has been merged, or an alternative fix merged, you should be able to use the library from the source. If not, here is a quick work around:
git clone https://github.com/tdj28/cmaputil
git checkout -b patch-1
git pull
cd cmaputil
python setup.py install
Assuming that works, you can now use the camputil library to use optimized Cividis colormaps.
Using Cividis
We would start by importing the key libraries:
import cmaputil as cmu
import cmaputil.cvdutil as cvu
import scipy
Next, add the following section to your code. It modifies
the viridis colormap that comes with matplotlib and runs
some optimizations on it.
# Input colormap nam
cmap = 'viridis'
# Optimize
rgb1, jab1 = cmu.get_rgb_jab(cmap) # Original colormap
rgb2, jab2 = cmu.get_rgb_jab(cvu.get_cvd(rgb1)) # CVD colormap
jab3 = cmu.make_linear(jab2) # Uniformize hue (a' vs. b')
#print(jab3)
_, jab4 = cmu.correct_J(jab3) # Linearize J'
# Convert back to sRGB
rgb4 = cmu.convert(jab4, cmu.CSPACE2, cmu.CSPACE1)
rgb4 = np.clip(rgb4, 0, 1)
# Resimulate CVD in case corrections took the map outside CVD-safe space
rgb4 = cvu.get_cvd(rgb4)
# Resimulate CVD in case corrections took the map outside CVD-safe space
rgb4 = cvu.get_cvd(rgb4)
colors = []
for j in range(len(rgb4[0])):
colors.append( (rgb4[0][j],
rgb4[1][j],
rgb4[2][j]) )
# Convert to matplotlib colormap
cividis = mpl.colors.LinearSegmentedColormap.from_list("", colors)
Finally, you can easily incorporate it into your matplotlib plots as in the following example:
plt.clf()
fig = plt.figure(figsize=(9, 9))
plt.title('Cividis Heatmap')
plt.ylabel('y')
plt.xlabel('x')
im = plt.imshow(heatmap, extent=extent, cmap=cividis, origin='lower')
fig.colorbar(im)
plt.savefig('Cividis Heatmap')
We can compare the rainbow version of the above plot:
with the Cividis version:
It certainly may be a matter of preference for most folks. I find the Cividis version more appealing, for example, because although the dark reds pop to me and pinpoint “hot spots” perhaps a little more quickly, they appear to be almost detached from the rest of the heat blob. In the Cividis mapping, as in other mappings which have only two colors, the spectrum is clearer. Given that using Cividis ensures that any color blind readers will be getting the same information as I see seals the deal for me and makes Cividis my go to choice for colormaps going forward.
Conclusions
Cividis is an optimized version of the viridis colormap which optimizes for both the ability to discern patterns for those with normal vision and the ability of the color blind to see the exact same thing.
Cividis works in Python and they also have an R library. This quick blog note shows how to use it right now, but I anticipate and hope that it will become obsolete and matplotlib will incorporate Cividis and PNNL’s work on this. COMSOL Multiphysics, a finite element analysis software package, for example, will be doing so.
You can find a standalone python script which I used to create these plots using Cividis here