Created
May 31, 2018 03:19
-
-
Save pangyuteng/f34860534b49da9a146dbe04d4a02502 to your computer and use it in GitHub Desktop.
custom matplotlib colormap and convert to vtk colormap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
_BrBG_data = ( | |
(0.32941176470588235, 0.18823529411764706, 0.0196078431372549 ), | |
(0.5490196078431373 , 0.31764705882352939, 0.0392156862745098 ), | |
(0.74901960784313726, 0.50588235294117645, 0.17647058823529413), | |
(0.87450980392156863, 0.76078431372549016, 0.49019607843137253), | |
(0.96470588235294119, 0.90980392156862744, 0.76470588235294112), | |
(0.96078431372549022, 0.96078431372549022, 0.96078431372549022), | |
(0.7803921568627451 , 0.91764705882352937, 0.89803921568627454), | |
(0.50196078431372548, 0.80392156862745101, 0.75686274509803919), | |
(0.20784313725490197, 0.59215686274509804, 0.5607843137254902 ), | |
(0.00392156862745098, 0.4 , 0.36862745098039218), | |
(0.0 , 0.23529411764705882, 0.18823529411764706) | |
) | |
_RdYlBu_data = ( | |
(0.6470588235294118 , 0.0 , 0.14901960784313725), | |
(0.84313725490196079, 0.18823529411764706 , 0.15294117647058825), | |
(0.95686274509803926, 0.42745098039215684 , 0.2627450980392157 ), | |
(0.99215686274509807, 0.68235294117647061 , 0.38039215686274508), | |
(0.99607843137254903, 0.8784313725490196 , 0.56470588235294117), | |
(1.0 , 1.0 , 0.74901960784313726), | |
(0.8784313725490196 , 0.95294117647058818 , 0.97254901960784312), | |
(0.6705882352941176 , 0.85098039215686272 , 0.9137254901960784 ), | |
(0.45490196078431372, 0.67843137254901964 , 0.81960784313725488), | |
(0.27058823529411763, 0.45882352941176469 , 0.70588235294117652), | |
(0.19215686274509805, 0.21176470588235294 , 0.58431372549019611) | |
) | |
_BrYlBu_data = ( | |
(0.32941176470588235, 0.18823529411764706, 0.0196078431372549 ), | |
(0.5490196078431373 , 0.31764705882352939, 0.0392156862745098 ), | |
(0.74901960784313726, 0.50588235294117645, 0.17647058823529413), | |
(0.87450980392156863, 0.76078431372549016, 0.49019607843137253), | |
(0.96470588235294119, 0.90980392156862744, 0.76470588235294112), | |
(1.0 , 1.0 , 0.74901960784313726), | |
(0.8784313725490196 , 0.95294117647058818 , 0.97254901960784312), | |
(0.6705882352941176 , 0.85098039215686272 , 0.9137254901960784 ), | |
(0.45490196078431372, 0.67843137254901964 , 0.81960784313725488), | |
(0.27058823529411763, 0.45882352941176469 , 0.70588235294117652), | |
(0.19215686274509805, 0.21176470588235294 , 0.58431372549019611) | |
) | |
MYCMAP = 'BrYlBu' | |
import numpy as np | |
import matplotlib.colors as colors | |
import matplotlib.pyplot as plt | |
from matplotlib import cm | |
mycmap_inst = colors.LinearSegmentedColormap.from_list(MYCMAP, _BrYlBu_data, cm.LUTSIZE) | |
cm.register_cmap(name=MYCMAP,cmap=mycmap_inst) | |
# Make plot with vertical (default) colorbar | |
fig, ax = plt.subplots() | |
d = [np.arange(0,1.1,0.1)] | |
mycmap_inst = cm.get_cmap(MYCMAP) | |
mycmap_inst.set_bad(color='white') | |
cax = ax.imshow(d, interpolation='nearest', cmap=mycmap_inst) | |
fig.colorbar(cax) | |
plt.savefig(MYCMAP+'.png') | |
#### convert to VTK colormap | |
import vtk | |
# scale 0,1 to 0,100 unsigned short | |
vmax = 1 | |
maxval = 100*vmax | |
scaleval = 100*vmax | |
cmap = cm.get_cmap(MYCMAP) | |
# Transfer Functions | |
opacity_tf = vtk.vtkPiecewiseFunction() | |
color_tf = vtk.vtkColorTransferFunction() | |
tf = [] | |
for val in np.linspace(0,1,11): | |
alpha = val*0.1 | |
r = cmap(val)[0] | |
g = cmap(val)[1] | |
b = cmap(val)[2] | |
tf.append([val*maxval,alpha,r,g,b]) | |
for p in tf: | |
color_tf.AddRGBPoint(p[0], p[2], p[3], p[4]) | |
opacity_tf.AddPoint(p[0], p[1]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment