Skip to content

Commit e2e2b2c

Browse files
committed
Colormaps demo, fix cyclically shifted cmaps
1 parent 952d4cb commit e2e2b2c

File tree

2 files changed

+28
-13
lines changed

2 files changed

+28
-13
lines changed

docs/colormaps.ipynb

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@
229229
"ProPlot allows you to create modified versions of *existing* colormaps using the `~proplot.styletools.Colormap` constructor and the new `~proplot.styletools.LinearSegmentedColormap` and `~proplot.styletools.ListedColormap` classes, which are used to replace the native matplotlib colormap classes. They can be modified in the following ways:\n",
230230
"\n",
231231
"* To remove colors from the left or right ends of a colormap, pass `left` or `right` to `~proplot.styletools.Colormap`. This calls the `~proplot.styletools.LinearSegmentedColormap.truncated` method, and can be useful when you want to use colormaps as :ref:`Color cycles` and need to remove the \"white\" part so that your lines stand out against the background.\n",
232-
"* To remove central colors from a diverging colormap, pass `cut` to `~proplot.styletools.Colormap`. This calls the `~proplot.styletools.LinearSegmentedColormap.punched` method, and can be useful when you want a sharper cutoff between negative and positive values.\n",
232+
"* To remove central colors from a diverging colormap, pass `cut` to `~proplot.styletools.Colormap`. This calls the `~proplot.styletools.LinearSegmentedColormap.punched` method, and can be used to create a sharper cutoff between negative and positive values. This should generally be used *without* a central level.\n",
233233
"* To rotate a cyclic colormap, pass `shift` to `~proplot.styletools.Colormap`. This calls the `~proplot.styletools.LinearSegmentedColormap.shifted` method. ProPlot ensures the colors at the ends of \"shifted\" colormaps are *distinct* so that levels never blur together.\n",
234234
"* To change the transparency of an entire colormap, pass `alpha` to `~proplot.styletools.Colormap`. This calls the `~proplot.styletools.LinearSegmentedColormap.set_alpha` method, and can be useful when *layering* filled contour or mesh elements.\n",
235235
"* To change the \"gamma\" of a `~proplot.styletools.PerceptuallyUniformColormap`, pass `gamma` to `~proplot.styletools.Colormap`. This calls the `~proplot.styletools.PerceptuallyUniformColormap.set_gamma` method, and controls how the luminance and saturation channels vary between colormap segments. ``gamma > 1`` emphasizes high luminance, low saturation colors, while ``gamma < 1`` emphasizes low luminance, high saturation colors."
@@ -243,23 +243,37 @@
243243
"source": [
244244
"import proplot as plot\n",
245245
"import numpy as np\n",
246-
"f, axs = plot.subplots([[1, 1, 2, 2, 3, 3], [0, 4, 4, 5, 5, 0]], axwidth=1.7)\n",
246+
"f, axs = plot.subplots([[1, 1, 2, 2, 3, 3, 4, 4], [0, 5, 5, 6, 6, 7, 7, 0]],\n",
247+
" axwidth=1.7, share=0, span=False)\n",
247248
"state = np.random.RandomState(51423)\n",
248-
"data = state.rand(50, 50).cumsum(axis=0) - 50\n",
249+
"data = state.rand(40, 40).cumsum(axis=0) - 12\n",
249250
"# Cutting central colors\n",
250-
"for ax, cut in zip(axs[:3], (0, 0.1, 0.2)):\n",
251-
" m = ax.contourf(data, cmap='Div', cmap_kw={'cut': cut}, levels=13)\n",
252-
" ax.format(xlabel='xlabel', ylabel='ylabel', title=f'cut = {cut}',\n",
253-
" suptitle='Slicing diverging and sequential colormaps')\n",
251+
"levels = plot.arange(-10, 10, 2)\n",
252+
"for i, (ax, cut) in enumerate(zip(axs[:4], (None, None, 0.1, 0.2))):\n",
253+
" if i == 0:\n",
254+
" title = 'With central level'\n",
255+
" levels = plot.edges(plot.arange(-10, 10, 2))\n",
256+
" else:\n",
257+
" title = 'Without central level'\n",
258+
" levels = plot.arange(-10, 10, 2)\n",
259+
" if cut is not None:\n",
260+
" title = f'cut = {cut}'\n",
261+
" m = ax.contourf(data, cmap='Div', cmap_kw={'cut': cut},\n",
262+
" extend='both', levels=levels)\n",
263+
" ax.format(xlabel='xlabel', ylabel='ylabel', title=title,\n",
264+
" suptitle='Truncating diverging and sequential colormaps')\n",
254265
" ax.colorbar(m, loc='b', locator='null')\n",
255266
"# Cutting left and right\n",
256-
"for ax, cut in zip(axs[3:], (0.2, 0.8)):\n",
257-
" if cut < 0.5:\n",
258-
" title, cmap, cmap_kw = f'left={cut}', 'grays', {'left': cut}\n",
267+
"for ax, coord in zip(axs[4:], (None, 0.3, 0.7)):\n",
268+
" cmap = 'grays'\n",
269+
" if coord is None:\n",
270+
" title, cmap_kw = 'Original', {}\n",
271+
" elif coord < 0.5:\n",
272+
" title, cmap_kw = f'left={coord}', {'left': coord}\n",
259273
" else:\n",
260-
" title, cmap, cmap_kw = f'right={cut}', 'grays', {'right': cut}\n",
274+
" title, cmap_kw = f'right={coord}', {'right': coord}\n",
261275
" ax.pcolormesh(data, cmap=cmap, cmap_kw=cmap_kw,\n",
262-
" colorbar='b', colorbar_kw={'locator': 'null'})\n",
276+
" colorbar='b', colorbar_kw={'locator': 'null'})\n",
263277
" ax.format(xlabel='xlabel', ylabel='ylabel', title=title)\n",
264278
"# Rotating cyclic\n",
265279
"f, axs = plot.subplots(ncols=3, axwidth=1.7)\n",

proplot/styletools.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1140,7 +1140,8 @@ def shifted(self, shift=None, name=None, **kwargs):
11401140
# Decompose shift into two truncations followed by concatenation
11411141
cmap_left = self.truncated(shift, 1)
11421142
cmap_right = self.truncated(0, shift)
1143-
return cmap_left.concatenate(cmap_right, name=name)
1143+
return cmap_left.concatenate(
1144+
cmap_right, ratios=(1 - shift, shift), name=name)
11441145

11451146
def truncated(self, left=None, right=None, name=None, **kwargs):
11461147
"""

0 commit comments

Comments
 (0)