Last active
June 7, 2021 14:23
-
-
Save kingjr/bd047b95f44ea5e6e7ae4a82c3924a66 to your computer and use it in GitHub Desktop.
ice_fire cmap
This file contains hidden or 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
from matplotlib.colors import ListedColormap | |
import colorcet | |
import numpy as np | |
ice_fire = np.vstack([ | |
colorcet.cm.fire(np.linspace(1, 0, 127))[:, [2, 1, 0, 3]], | |
colorcet.cm.fire(np.linspace(0, 1, 127)) | |
]) | |
ice_fire = ListedColormap(ice_fire) | |
fire = colorcet.cm.fire(np.linspace(0, 1, 127)) | |
ice = colorcet.cm.fire(np.linspace(1, 0, 127))[:, [2, 1, 0, 3]] | |
ice_fire = ListedColormap(np.vstack([ice, fire])) | |
ice_fire_r = ListedColormap(np.vstack([ice[::-1], np.ones(4), fire[::-1]])) | |
cmap = plt.get_cmap('RdBu_r') | |
def trans(c1, c2, n=10): | |
c1 = np.asarray(c1) | |
c2 = np.asarray(c2) | |
a = np.linspace(1, 0, n) | |
return (a[:, None] * c1[None, :] + (1-a[:, None]) * c2[None, :]) | |
from matplotlib.colors import ListedColormap | |
import colorcet | |
import matplotlib.pyplot as plt | |
import numpy as np | |
ice_fire = np.vstack([ | |
colorcet.cm.fire(np.linspace(1, 0, 127))[:, [2, 1, 0, 3]], | |
colorcet.cm.fire(np.linspace(0, 1, 127)) | |
]) | |
ice_fire = ListedColormap(ice_fire) | |
fire = colorcet.cm.fire(np.linspace(0, 1, 127)) | |
ice = colorcet.cm.fire(np.linspace(1, 0, 127))[:, [2, 1, 0, 3]] | |
ice_fire = ListedColormap(np.vstack([ice, fire])) | |
ice_fire_r = ListedColormap(np.vstack([ice[::-1], np.ones(4), fire[::-1]])) | |
cmap = plt.get_cmap('RdBu_r') | |
def trans(c1, c2, n=10): | |
c1 = np.asarray(c1) | |
c2 = np.asarray(c2) | |
a = np.linspace(1, 0, n) | |
return (a[:, None] * c1[None, :] + (1-a[:, None]) * c2[None, :]) | |
def contrast_cmap(cmap, middle=20, edge=10, inverted=False): | |
if isinstance(cmap, str): | |
cmap = plt.get_cmap(cmap) | |
if inverted: | |
edge_color = np.ones(4) | |
middle_color = np.array([0, 0, 0, 1]) | |
else: | |
middle_color = np.ones(4) | |
edge_color = np.array([0, 0, 0, 1]) | |
return ListedColormap(np.vstack([ | |
trans(edge_color, cmap(0), edge), | |
cmap(np.linspace(0, .45, 100)), | |
trans(cmap(.45), middle_color, middle), | |
trans(middle_color, cmap(.55), middle), | |
cmap(np.linspace(.55, 1., 100)), | |
trans(cmap(1.), edge_color, edge) | |
])) | |
ice_fire = contrast_cmap(ice_fire, inverted=True) | |
plt.matshow(np.random.rand(100, 100), cmap=ice_fire) | |
plt.colorbar() | |
RdBu_r = contrast_cmap('RdBu_r') | |
def fill_between_cmap(x, y, cmap='RdBu_r', vmin=None, vmax=None, ax=None, sym_alpha=True, **kwargs): | |
""" | |
Plot a line with a linear alpha gradient filled beneath it. | |
Parameters | |
---------- | |
x, y : array-like | |
The data values of the line. | |
cmap : str | array | |
cmap | |
ax : a matplotlib Axes instance | |
The axes to plot on. If None, the current pyplot axes will be used. | |
Additional arguments are passed on to matplotlib's ``plot`` function. | |
Returns | |
------- | |
line : a Line2D instance | |
The line plotted. | |
im : an AxesImage instance | |
The transparent gradient clipped to just the area beneath the curve. | |
""" | |
import numpy as np | |
import matplotlib.pyplot as plt | |
import matplotlib.colors as mcolors | |
from matplotlib.patches import Polygon | |
if ax is None: | |
ax = plt.gca() | |
line, = ax.plot(x, y, **kwargs) | |
zorder = line.get_zorder() | |
z = np.ones((100, 1, 4), dtype=float) | |
if isinstance(cmap, str): | |
cmap = plt.get_cmap(cmap) | |
xmin, xmax, ymin, ymax = x.min(), x.max(), y.min(), y.max() | |
if vmin is None: | |
vmin = ymin | |
if vmax is None: | |
vmax = ymax | |
values = np.linspace(ymin, ymax, 100) | |
values = (values - vmin) / (vmax-vmin) | |
values = np.clip(values, 0, 1) | |
z[:,:,:3] = cmap(values)[:, None, :3] | |
#z[:,:,-1] = np.linspace(-1, 1, 100)[:,None]**.5 | |
im = ax.imshow(z, aspect='auto', extent=[xmin, xmax, ymin, ymax], | |
origin='lower', zorder=-2) | |
xy = np.column_stack([x, y]) | |
xy = np.vstack([[xmin, 0], xy, [xmax, 0], [xmin, 0]]) | |
clip_path = Polygon(xy, facecolor='none', edgecolor='none', closed=True) | |
ax.add_patch(clip_path) | |
im.set_clip_path(clip_path) | |
ax.autoscale(True) | |
return line, im |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment