Skip to content

Commit

Permalink
Merge pull request #168 from ykempf/colormap_construction_fix
Browse files Browse the repository at this point in the history
Fix to construction of some colormaps.
  • Loading branch information
markusbattarbee authored Apr 6, 2022
2 parents 5e6ffa3 + 345a0b7 commit 39f63b7
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
23 changes: 12 additions & 11 deletions pyPlots/colormaps.py
Original file line number Diff line number Diff line change
Expand Up @@ -1107,40 +1107,41 @@
[0.9661, 0.9514428571, 0.0755333333],
[0.9763, 0.9831, 0.0538]]

from matplotlib.colors import ListedColormap
from matplotlib.colors import LinearSegmentedColormap
cmaps = {}
for (name, data) in (('magma', _magma_data),
('inferno', _inferno_data),
('plasma', _plasma_data),
('viridis', _viridis_data),
('parula', _parula_data)):
('parula', _parula_data),
('parula_r', _parula_data[::-1])):

cmaps[name] = ListedColormap(data, name=name)
cmaps[name] = LinearSegmentedColormap.from_list(name, data)
# Some of these are already included in newer versions of matplotlib
magma = cmaps['magma']
inferno = cmaps['inferno']
plasma = cmaps['plasma']
viridis = cmaps['viridis']
parula = cmaps['parula']
parula_r = cmaps['parula_r']

# Themis colormap, as extracted from the themis tools' IDL file
import matplotlib
hot_desaturated_colors=[(71./255.,71./255.,219./255.),(0,0,91./255.),(0,1,1),(0,.5,0),(1,1,0),(1,96./255,0),(107./255,0,0),(224./255,76./255,76./255)]
hot_desaturated_colormap = matplotlib.colors.LinearSegmentedColormap.from_list("hot_desaturated",hot_desaturated_colors)
hot_desaturated_colormap_r = matplotlib.colors.LinearSegmentedColormap.from_list("hot_desaturated_r",hot_desaturated_colors[::-1])
hot_desaturated_colormap = LinearSegmentedColormap.from_list("hot_desaturated",hot_desaturated_colors)
hot_desaturated_colormap_r = LinearSegmentedColormap.from_list("hot_desaturated_r",hot_desaturated_colors[::-1])

# Pale version of hot_desaturated
pale_desaturated_colors=[[0.5 + j * 0.5 for j in i] for i in hot_desaturated_colors]
pale_desaturated_colormap = matplotlib.colors.LinearSegmentedColormap.from_list("pale_desaturated",pale_desaturated_colors)
pale_desaturated_colormap_r = matplotlib.colors.LinearSegmentedColormap.from_list("pale_desaturated_r",pale_desaturated_colors[::-1])
pale_desaturated_colormap = LinearSegmentedColormap.from_list("pale_desaturated",pale_desaturated_colors)
pale_desaturated_colormap_r = LinearSegmentedColormap.from_list("pale_desaturated_r",pale_desaturated_colors[::-1])

# Warhol, by Otto Hannuksela
warhol_cdict = {'red': [(0,1,1), (0.111111,0,0), (0.222222,64./255.,64./255.), (0.333333,0,0), (0.444444,79./255.,79./255.),(0.555556,70./255.,70./255.), (0.666667,1,1), (0.777778,1,1), (0.888889,81./255.,81./255.), (1,1,1)],
'green': [(0,1,1), (0.111111,66./255.,66./255.), (0.222222,1,1), (0.333333,0,0), (0.444444,1,1), (0.555556,16./255.,16./255.), (0.666667,1,1), (0.777778,57./255.,57./255.), (0.888889,0,0), (1,150./255.,150./255.)],
'blue': [(0,1,1), (0.111111,0,0), (0.222222,81./255.,81./255.), (0.333333,55./255.,55./255.), (0.444444,1,1), (0.555556,70./255.,70./255.), (0.666667,0,0), (0.777778,0,0), (0.888889,0,0), (1,150./255.,150./255.)] }
warhol_colormap = matplotlib.colors.LinearSegmentedColormap("warhol",warhol_cdict)
warhol_colormap = LinearSegmentedColormap("warhol",warhol_cdict)
#>0 255 255 255 255 0.111111 0 66 0 255 0.222222 64 255 81 255 0.333333 0 0 55 255 0.444444 79 255 255 255 0.555556 70 16 70 255 0.666667 255 255 0 255 0.777778 255 57 0 255 0.888889 81 0 0 255 1 255 150 150 255 <
#matplotlib.colors.LinearSegmentedColormap(name, segmentdata, N=256, gamma=1.0)[source]
#LinearSegmentedColormap(name, segmentdata, N=256, gamma=1.0)[source]


#Read in Scientific Colormaps 7 from subdirectory
Expand All @@ -1154,7 +1155,7 @@
(_dummypath, _cm_name) = os.path.split(_f)
_cm_name = _cm_name[:-4]
_cm_data = np.loadtxt(_f)
_cm = matplotlib.colors.LinearSegmentedColormap.from_list(_cm_name, _cm_data)
_cm = LinearSegmentedColormap.from_list(_cm_name, _cm_data)
plt.register_cmap(cmap=_cm)
plt.register_cmap(cmap=_cm.reversed())

2 changes: 1 addition & 1 deletion pyPlots/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@

# Register custom colourmaps
plt.register_cmap(name='parula', cmap=cmaps.parula)
plt.register_cmap(name='parula_r', cmap=matplotlib.colors.ListedColormap(cmaps.parula.colors[::-1]))
plt.register_cmap(name='parula_r', cmap=cmaps.parula_r)
plt.register_cmap(name='hot_desaturated', cmap=cmaps.hot_desaturated_colormap)
plt.register_cmap(name='hot_desaturated_r', cmap=cmaps.hot_desaturated_colormap_r) # Listed colormap requires making reversed version at earlier step
plt.register_cmap(name='pale_desaturated', cmap=cmaps.pale_desaturated_colormap)
Expand Down

0 comments on commit 39f63b7

Please sign in to comment.