Browse code

convert kwant-red colormap when plotly backend is selected

Kelvin Loh authored on 06/12/2018 11:08:46
Showing 3 changed files
... ...
@@ -7,8 +7,6 @@
7 7
 # http://kwant-project.org/authors.
8 8
 
9 9
 import numpy as np
10
-from matplotlib.colors import ListedColormap
11
-
12 10
 
13 11
 kr_data = [[ 0.98916316, 0.98474381, 0.99210697],
14 12
            [ 0.98723538, 0.98138853, 0.98740721],
... ...
@@ -271,4 +269,4 @@ kr_data = [[ 0.98916316, 0.98474381, 0.99210697],
271 269
 kr_data = np.array(kr_data)
272 270
 kr_data = np.clip(kr_data / kr_data[0], 0, 1)
273 271
 
274
-kwant_red = ListedColormap(kr_data, name="kwant red")
272
+kwant_red = kr_data
... ...
@@ -35,7 +35,10 @@ try:
35 35
     from matplotlib.figure import Figure
36 36
     from matplotlib import collections
37 37
     from . import _colormaps
38
+    from matplotlib.colors import ListedColormap
38 39
     mpl_available = True
40
+    kwant_red_matplotlib = ListedColormap(_colormaps.kwant_red,
41
+                                          name="kwant red")
39 42
     try:
40 43
         from mpl_toolkits import mplot3d
41 44
         has3d = True
... ...
@@ -53,7 +56,13 @@ try:
53 56
     import plotly.offline as plotly_module
54 57
     import plotly.graph_objs as plotly_graph_objs
55 58
     init_notebook_mode_set = False
59
+    from . import _colormaps
56 60
     plotly_available = True
61
+
62
+    _cmap_plotly = 255 * _colormaps.kwant_red
63
+    _cmap_levels = np.linspace(0, 1, len(_cmap_plotly))
64
+    kwant_red_plotly = [(level, 'rgb({},{},{})'.format(*rgb))
65
+                        for level, rgb in zip(_cmap_levels, _cmap_plotly)]
57 66
 except ImportError:
58 67
     warnings.warn("plotly is not available, if other backends are unavailable,"
59 68
                   " only iterator-providing functions will work",
... ...
@@ -353,3 +362,14 @@ if mpl_available:
353 362
                     self.set_linewidths(self.linewidths_orig2 * factor)
354 363
 
355 364
                 super().draw(renderer)
365
+
366
+if plotly_available:
367
+    def matplotlib_to_plotly_cmap(cmap, pl_entries):
368
+        h = 1.0/(pl_entries-1)
369
+        pl_colorscale = []
370
+
371
+        for k in range(pl_entries):
372
+            C = map(np.uint8, np.array(cmap(k*h)[:3])*255)
373
+            pl_colorscale.append([k*h, 'rgb'+str((C[0], C[1], C[2]))])
374
+
375
+        return pl_colorscale
... ...
@@ -1367,7 +1367,7 @@ def map(sys, value, colorbar=True, cmap=None, vmin=None, vmax=None, a=None,
1367 1367
         fig = None
1368 1368
 
1369 1369
     if cmap is None:
1370
-        cmap = _p._colormaps.kwant_red
1370
+        cmap = _p.kwant_red_matplotlib
1371 1371
 
1372 1372
     # Calculate the min/max bounds for the colormap.
1373 1373
     # User-provided values take precedence.
... ...
@@ -2197,7 +2197,7 @@ def streamplot(field, box, cmap=None, bgcolor=None, linecolor='k',
2197 2197
 
2198 2198
     if bgcolor is None:
2199 2199
         if cmap is None:
2200
-            cmap = _p._colormaps.kwant_red
2200
+            cmap = _p.kwant_red_matplotlib
2201 2201
         cmap = _p.matplotlib.cm.get_cmap(cmap)
2202 2202
         bgcolor = cmap(0)[:3]
2203 2203
     elif cmap is not None:
... ...
@@ -2307,7 +2307,7 @@ def scalarplot(field, box,
2307 2307
         raise ValueError("Only 2D field can be plotted.")
2308 2308
 
2309 2309
     if cmap is None:
2310
-        cmap = _p._colormaps.kwant_red
2310
+        cmap = _p.kwant_red_matplotlib
2311 2311
     cmap = _p.matplotlib.cm.get_cmap(cmap)
2312 2312
 
2313 2313
     if ax is None: