diff --git a/README.md b/README.md index c039945..53e364c 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,10 @@ # Scientific color maps +## Blog post + +[Scicomap Medium blog post (free)](https://towardsdatascience.com/your-colour-map-is-bad-heres-how-to-fix-it-lessons-learnt-from-the-event-horizon-telescope-b82523f09469) + ## Installation ```shell @@ -97,7 +101,7 @@ import scicomap as sc import matplotlib.pyplot as plt # the thing that should not be -ugly_jet = plt.get_cmap("YlOrBr") +ugly_jet = plt.get_cmap("jet") sc_map = sc.ScicoMiscellaneous(cmap=ugly_jet) f=sc_map.assess_cmap(figsize=(22,10)) ``` @@ -219,6 +223,74 @@ which render as +# Use with matplotlib + +## Use a corrected colormap in a matplotlib figure + +```python +import matplotlib.pyplot as plt +import matplotlib as mpl +import scicomap as sc +from scicomap.utils import _fn_with_roots + +# load the color map +div_map = sc.ScicoDiverging(cmap='watermelon') + +# correct the colormap +div_map.unif_sym_cmap(lift=15, + bitonic=False, + diffuse=True) + +# get the fixed color map +fixed_cmap = div_map.get_mpl_color_map() +print(type(fixed_cmap)) + +# use it as you like +im = _fn_with_roots() +norm = mpl.colors.CenteredNorm() +divnorm = mpl.colors.TwoSlopeNorm(vmin=-1, vcenter=0, vmax=1.25) +fig = plt.figure(figsize=(3,3), facecolor="white") +ax = fig.add_subplot(1, 1, 1, facecolor="white") +pos = ax.imshow(im, cmap=fixed_cmap, aspect="auto", norm=divnorm) +fig.colorbar(pos, ax=ax); +``` + + +## Correct a matplotlib colormap + + + +```python +import matplotlib.pyplot as plt +import matplotlib as mpl +import scicomap as sc +from scicomap.utils import _fn_with_roots + +# load the color map +mpl_cmap_obj = plt.get_cmap("PRGn") +div_map = sc.ScicoDiverging(cmap=mpl_cmap_obj) + +# correct the colormap +div_map.unif_sym_cmap(lift=None, + bitonic=False, + diffuse=True) + +# get the fixed color map +fixed_cmap = div_map.get_mpl_color_map() +print(type(fixed_cmap)) + +# use it as you like +im = _fn_with_roots() +norm = mpl.colors.CenteredNorm() +divnorm = mpl.colors.TwoSlopeNorm(vmin=-1, vcenter=0, vmax=1.25) +fig = plt.figure(figsize=(3,3), facecolor="white") +ax = fig.add_subplot(1, 1, 1, facecolor="white") +pos = ax.imshow(im, cmap=fixed_cmap, aspect="auto", norm=divnorm) +fig.colorbar(pos, ax=ax); +``` + + + # Comparing color maps You can easily compare, raw or corrected, color maps using a picture of your choice @@ -353,6 +425,11 @@ sc.plot_colormap(ctype='qualitative', # Changes log +### 0.3 + + - Add a section "how to use with matplotlib" + - [Bug] Center diverging color map in examples + ### 0.2 - [Bug] Fix typo in chart titles diff --git a/pics/mpl.png b/pics/mpl.png new file mode 100644 index 0000000..c735bbb Binary files /dev/null and b/pics/mpl.png differ diff --git a/pics/mpl2.png b/pics/mpl2.png new file mode 100644 index 0000000..c58a0b6 Binary files /dev/null and b/pics/mpl2.png differ diff --git a/scicomap/__init__.py b/scicomap/__init__.py index 144390f..af3fe56 100644 --- a/scicomap/__init__.py +++ b/scicomap/__init__.py @@ -3,7 +3,7 @@ from . import cmath from . import cblind -__version__ = "0.2" +__version__ = "0.3" __all__ = ["scicomap", "datasets", "cmath", "cblind", "utils"] # bound to upper level diff --git a/scicomap/scicomap.py b/scicomap/scicomap.py index 17f2a4c..b5ac477 100644 --- a/scicomap/scicomap.py +++ b/scicomap/scicomap.py @@ -577,7 +577,8 @@ def draw_example(self, facecolor="black", figsize=(20, 20)): arr_3d=[(xpyr, ypyr, zpyr), (per_x, per_z, per_z)], figsize=figsize, facecolor=facecolor, - cname=self.cname) + cname=self.cname, + norm=True) return fig @@ -747,7 +748,8 @@ def draw_example(self, facecolor="black", figsize=(20, 20)): arr_3d=[(xpyr, ypyr, zpyr), (per_x, per_z, per_z)], figsize=figsize, facecolor=facecolor, - cname=self.cname) + cname=self.cname, + norm=True) return fig diff --git a/scicomap/utils.py b/scicomap/utils.py index 92c950e..799fa49 100644 --- a/scicomap/utils.py +++ b/scicomap/utils.py @@ -1,5 +1,6 @@ import itertools import numpy as np +import matplotlib as mpl import matplotlib.pyplot as plt import matplotlib.image as mpimg from matplotlib.colors import ListedColormap @@ -151,7 +152,7 @@ def _plot_e_field(ax, cmap, n_neg_charges=2, facecolor="black", title=False): return ax -def _plot_examples(color_map, images, arr_3d, figsize, facecolor, cname, cblind=True): +def _plot_examples(color_map, images, arr_3d, figsize, facecolor, cname, cblind=True, norm=False): """Create the figure based on the provided images, continuous colormaps""" fig = plt.figure(figsize=figsize, facecolor=facecolor) n_images = len(images) @@ -167,6 +168,7 @@ def _plot_examples(color_map, images, arr_3d, figsize, facecolor, cname, cblind= idx_3d = 0 n_rows = len(c_maps) n_cols = len(images) + for c_map, im in itertools.product(c_maps, images): if isinstance(im, str) and ('3D' in im): @@ -186,8 +188,9 @@ def _plot_examples(color_map, images, arr_3d, figsize, facecolor, cname, cblind= ax = fig.add_subplot(n_rows, n_cols, axi, facecolor=facecolor) ax = _plot_complex_arg(ax, c_map, facecolor, title=axi <= n_cols) else: + cmap_norm = mpl.colors.CenteredNorm() if norm else None ax = fig.add_subplot(n_rows, n_cols, axi, facecolor=facecolor) - ax.imshow(im, cmap=c_map, aspect="auto") + ax.imshow(im, cmap=c_map, aspect="auto", norm=cmap_norm) ax.get_xaxis().set_visible(False) ax.get_yaxis().set_visible(False) ax.set_title(sub_title[axi - 1], color=title_color, fontsize=16, loc="left") diff --git a/setup.py b/setup.py index 59e7adf..609616b 100644 --- a/setup.py +++ b/setup.py @@ -27,7 +27,7 @@ setup( name="scicomap", - version="0.2", + version="0.3", description="Scientific color maps", long_description=README, long_description_content_type="text/markdown",