Skip to content

Commit

Permalink
fix a bug, extend the readme
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Bury committed Jun 20, 2021
1 parent 651fbf0 commit a15b552
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 7 deletions.
79 changes: 78 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
```
Expand Down Expand Up @@ -219,6 +223,74 @@ which render as
<td align="left"><img src="pics/vanimo-fixed-examples.png" width="1000"/></td>


# 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);
```
<td align="left"><img src="pics/mpl.png" width="250"/></td>

## 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);
```

<td align="left"><img src="pics/mpl2.png" width="250"/></td>

# Comparing color maps

You can easily compare, raw or corrected, color maps using a picture of your choice
Expand Down Expand Up @@ -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
Expand Down
Binary file added pics/mpl.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added pics/mpl2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion scicomap/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions scicomap/scicomap.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down
7 changes: 5 additions & 2 deletions scicomap/utils.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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):
Expand All @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit a15b552

Please sign in to comment.