This repository has been archived by the owner on Nov 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Tried to match 2D matplotlib colormaps to 3D mayavi color lookup tables
- Loading branch information
milosmicik
committed
Jun 16, 2022
1 parent
fbea1a4
commit b0714e7
Showing
5 changed files
with
80 additions
and
18 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
from matplotlib import pyplot as plt | ||
import numpy as np | ||
from matplotlib.colors import LinearSegmentedColormap, ListedColormap, TwoSlopeNorm, BoundaryNorm, Normalize | ||
from matplotlib.cm import ScalarMappable | ||
|
||
|
||
class DEMScalarMappable(ScalarMappable): | ||
colors_sea = plt.cm.terrain(np.linspace(0, 0.17, 256)) | ||
colors_land = plt.cm.terrain(np.linspace(0.25, 1, 256)) | ||
all_colors = np.vstack((colors_sea, colors_land)) | ||
|
||
def __init__(self, vmin, vmax): | ||
if vmin > 0: | ||
norm = Normalize(vmin, vmax, clip=True) | ||
cmap = LinearSegmentedColormap.from_list("terrain_map", self.colors_land) | ||
elif vmin == 0: | ||
norm = Normalize(vmin, vmax, clip=True) | ||
cmap = LinearSegmentedColormap.from_list( | ||
"terrain_map", np.vstack((self.colors_sea[-1], self.colors_land[1:])) | ||
) | ||
elif vmax <= 0: | ||
norm = Normalize(vmin, vmax, clip=True) | ||
cmap = LinearSegmentedColormap.from_list("terrain_map", self.colors_sea) | ||
else: | ||
cmap = LinearSegmentedColormap.from_list("terrain_map", self.all_colors) | ||
norm = TwoSlopeNorm(vmin=vmin, vcenter=0, vmax=vmax) | ||
super().__init__(norm, cmap) | ||
|
||
def segmented(self, bin_bounds, kind): | ||
if kind == "bottom": | ||
norm = BoundaryNorm(bin_bounds, bin_bounds.size - 1, clip=True) | ||
cmap = LinearSegmentedColormap.from_list( | ||
"terrain_map_segmented", self.to_rgba(bin_bounds[:-1]), N=bin_bounds.size - 1 | ||
) | ||
return ScalarMappable(norm, cmap) | ||
elif kind == "middle": | ||
norm = BoundaryNorm( | ||
[ | ||
1.5 * bin_bounds[0] - 0.5 * bin_bounds[1], | ||
*(bin_bounds[0:-1] + bin_bounds[1:]) / 2, | ||
1.5 * bin_bounds[-1] - 0.5 * bin_bounds[-2], | ||
], | ||
bin_bounds.size, | ||
) | ||
cmap = LinearSegmentedColormap.from_list( | ||
"terrain_map_segmented", self.to_rgba(bin_bounds), N=bin_bounds.size | ||
) | ||
return ScalarMappable(norm, cmap) | ||
elif kind == "top": | ||
norm = BoundaryNorm(bin_bounds, bin_bounds.size - 1, clip=True) | ||
cmap = LinearSegmentedColormap.from_list( | ||
"terrain_map_segmented", self.to_rgba(bin_bounds[1:]), N=bin_bounds.size - 1 | ||
) | ||
return ScalarMappable(norm, cmap) | ||
else: | ||
raise TypeError("Type can only be one of 'bottom', 'middle' or 'top'.") | ||
|
||
@property | ||
def lut(self): | ||
return self.__get_lut(self) | ||
|
||
def segmented_lut(self, bin_bounds, kind): | ||
return self.__get_lut(self.segmented(bin_bounds, kind)) | ||
|
||
@staticmethod | ||
def __get_lut(scalarmappable: ScalarMappable): | ||
return ( | ||
scalarmappable.to_rgba(np.linspace(scalarmappable.get_clim()[0], scalarmappable.get_clim()[1], 256)) * 255 | ||
) |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.