Is there a way to save a custom matplotlib matrix template for use elsewhere?

Is there a way to save custom maplotlibcolourmap ( matplotlib.cm) as a file (for example, a color palette table ( .cpt) file , as used in MATLAB ) for sharing and then use later in other programs? (e.g. Panopoly , MATLAB ...)

Example

Below, the new is LinearSegmentedColormapdone by changing the existing color map (by truncating, shown in another question related here ).

import matplotlib
import matplotlib.pyplot as plt
import numpy as np

# Get an existing colorbar
cb = 'CMRmap'
cmap = plt.get_cmap( cb )

# Variables to modify (truncate) the colormap with
minval = 0.15 
maxval = 0.95
npoints = 100 

# Now modify (truncate) the colorbar
cmap = matplotlib.colors.LinearSegmentedColormap.from_list( 
    'trunc({n},{a:.2f},{b:.2f})'.format(n=cmap.name, a=minval,
    b=maxval), cmap(np.linspace(minval, maxval, npoints)))

# Now the data can be extracted as a dictionary
cdict = cmap._segmentdata

# e.g. variables ('blue', 'alpha', 'green', 'red')
print( cdict.keys() )

# Now, is it possible to save to this as a .cpt?

More details

I am aware of ways to load external colormaps into matplotlib (e.g. shown here and here ).

NASA GISS Panoply:

(CPT) , Generic Mapping Tools. / , .

+4
1

, , (vmin vmax) cpt .

import matplotlib.pyplot as plt
import numpy as np

def export_cmap_to_cpt(cmap, vmin=0,vmax=1, N=255, filename="test.cpt",**kwargs):
    # create string for upper, lower colors
    b = np.array(kwargs.get("B", cmap(0.)))
    f = np.array(kwargs.get("F", cmap(1.)))
    na = np.array(kwargs.get("N", (0,0,0))).astype(float)
    ext = (np.c_[b[:3],f[:3],na[:3]].T*255).astype(int)
    extstr = "B {:3d} {:3d} {:3d}\nF {:3d} {:3d} {:3d}\nN {:3d} {:3d} {:3d}"
    ex = extstr.format(*list(ext.flatten()))
    #create colormap
    cols = (cmap(np.linspace(0.,1.,N))[:,:3]*255).astype(int)
    vals = np.linspace(vmin,vmax,N)
    arr = np.c_[vals[:-1],cols[:-1],vals[1:],cols[1:]]
    # save to file
    fmt = "%e %3d %3d %3d %e %3d %3d %3d"
    np.savetxt(filename, arr, fmt=fmt, 
               header="# COLOR_MODEL = RGB",
               footer = ex, comments="")

# test case: create cpt file from RdYlBu colormap
cmap = plt.get_cmap("RdYlBu",255)
# you may create your colormap differently, as in the question

export_cmap_to_cpt(cmap, vmin=0,vmax=1,N=20)

# COLOR_MODEL = RGB
0.000000e+00 165   0  38 5.263158e-02 190  24  38
5.263158e-02 190  24  38 1.052632e-01 215  49  39
1.052632e-01 215  49  39 1.578947e-01 231  83  55
1.578947e-01 231  83  55 2.105263e-01 244 114  69
2.105263e-01 244 114  69 2.631579e-01 249 150  86
2.631579e-01 249 150  86 3.157895e-01 253 181 104
3.157895e-01 253 181 104 3.684211e-01 253 207 128
3.684211e-01 253 207 128 4.210526e-01 254 230 153
4.210526e-01 254 230 153 4.736842e-01 254 246 178
4.736842e-01 254 246 178 5.263158e-01 246 251 206
5.263158e-01 246 251 206 5.789474e-01 230 245 235
5.789474e-01 230 245 235 6.315789e-01 206 234 242
6.315789e-01 206 234 242 6.842105e-01 178 220 235
6.842105e-01 178 220 235 7.368421e-01 151 201 224
7.368421e-01 151 201 224 7.894737e-01 120 176 211
7.894737e-01 120 176 211 8.421053e-01  96 149 196
8.421053e-01  96 149 196 8.947368e-01  70 118 180
8.947368e-01  70 118 180 9.473684e-01  59  86 164
9.473684e-01  59  86 164 1.000000e+00  49  54 149
B 165   0  38
F  49  54 149
N   0   0   0

.

+2

Source: https://habr.com/ru/post/1692371/


All Articles