question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Automatically apply diverging colormaps and add `robust=True` option

See original GitHub issue

Is there any way to have an automatic scaling of the colorbar when 0 is present, like the default behavior of Cartopy that center the colorbar on 0 and automatically use a diverging colormap?

And in addition, is that possible to use the option robust=True of Cartopy? When I try to use it, it tells me that it is not recognized.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:10 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
lukelbdcommented, Aug 18, 2021

These features are finally on master. Below is an example of the robust=True feature. You can also pass a number e.g. robust=90 to select colormap limits based on the middle 90 percentile range, or pass a tuple e.g. robust=(0, 90) to use that particular percentile range. As in xarray, the default percentile range implied by robust=True is 96. The keyword choice is maybe a bit clunky here since robust can now accept these other inputs but I’d rather stay consistent with xarray.

import proplot as pplt
import numpy as np
fig, axs = pplt.subplots(ncols=2)
N = 10
data = np.random.rand(N, N)
data.flat[0] = 100.0
for i, ax in enumerate(axs):
    ax.pcolor(data, robust=bool(i))
    ax.format(title='Robust ' + ('on' if i else 'off'))

test

And here’s an example of the automatic diverging colormap selection. A diverging colormap is picked as follows:

  • If discrete=True and levels are being used, then levels must have more than one each positive and negative values.
  • If discrete=False and only vmin and vmax are being used, then vmin and vmax must have the opposite sign.

I’ve also added a whole system for selecting default “sequential”, “diverging”, “qualitative”, and “cyclic” colormaps with corresponding rc settings and keyword arguments that can be passed to plot commands (e.g., rc['cmap.cyclic'] = cmap or to change the default cyclic colormap and ax.pcolor(..., cyclic=True) to select the default cyclic colormap). Details are in the changelog on master (about to put these in a big release).

import proplot as pplt
import numpy as np
fig, axs = pplt.subplots(ncols=2)
N = 10
data = np.random.rand(N, N) * 10
for i, ax in enumerate(axs):
    ax.pcolor(data - i * 5, colorbar='b')
    ax.format(title='Diverging ' + ('on' if i else 'off'))

div

1reaction
lukelbdcommented, Dec 4, 2019

Btw proplot has a plot.arange(...) function that’s great for generating lists of ticks, colorbar levels, etc. Behavior is identical to np.arange except it is endpoint inclusive. So your example would be plot.arange(-45, 45, 5) rather than np.arange(-45, 50, 5) which IMO is more intuitive.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Automatically apply diverging colormaps and add robust=True ...
contourf() , Xarray/Cartopy makes a symmetric color bar with a diverging colormap. Then I can use the option robust=True in my plot option...
Read more >
Choosing Colormaps in Matplotlib
For many applications, a perceptually uniform colormap is the best choice; ... Diverging: change in lightness and possibly saturation of two different ...
Read more >
Plotting 2D data — ProPlot documentation - Read the Docs
ProPlot adds several new features to matplotlib's plotting commands using the ... automatically applies the default diverging colormap rc['cmap.diverging'] ...
Read more >
Beautiful custom colormaps with Matplotlib | by Kerry Halupka
The second chart uses a diverging colormap. With more colours it's easier to tell the difference between similar values, but it also suggests...
Read more >
xarray.plot.imshow
Passing robust=True infers vmin and vmax in the usual way. Additionally the y-axis is not ... Setting both values prevents use of a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found