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.

[BUG]: Weirdness with `center` when using non-default colormaps

See original GitHub issue

I have code that looks like this:

import matplotlib
import seaborn as sns

A = np.eye(2)

fig, ax = plt.subplots(figsize=(10,10))
cmap = matplotlib.colors.ListedColormap(["white", "black"])
ax = sns.heatmap(A, cmap=cmap, ax=ax, center=0, cbar=False)

This code produces a heatmap that looks like this: Screen Shot 2021-03-27 at 11 44 35 AM

However, when I change center to None, I correctly get the heatmap that I want: Screen Shot 2021-03-27 at 11 45 12 AM

And, interestingly, when center is 0 and cmap = matplotlib.colors.ListedColormap(["white", "white", "black"]), I get the correct heatmap again.

I’m not familiar enough with the heatmap API to know whether this is a bug, but it seems like it might be?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
mwaskomcommented, Mar 28, 2021

Good to know. So I was wrong, there isn’t a problem interpolating a discrete colormap (good!) and the “weirdness” arises from using center with binary (i.e. 0, and 1) data.

I think we can consider this resolved, but @loftusa if you think better documentation of the center parameter would have avoided this confusion I’d be happy to consider them.

0reactions
jhnclscommented, Mar 27, 2021

Here is some example code showing how center works. It can work perfectly with a bicolored colormap, although if can be harder to understand.

import seaborn as sns
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap

x = np.linspace(-np.pi / 2, np.pi / 2, 100)
y = np.linspace(-np.pi / 2, np.pi / 2, 100)
x, y = np.meshgrid(x, y)
A = np.sin(x ** 2 + y ** 2) ** 2
# cmap = ListedColormap(['white', 'black'])
cmap = 'coolwarm'

fig, axes = plt.subplots(1, 4, figsize=(16, 4))
for ax, center in zip(axes.flat, [0, 0.2, 0.5, 0.9]):
    sns.heatmap(A, cmap=cmap, ax=ax, center=center, cbar=True, cbar_kws={'ticks': np.arange(0, 1, 0.1)})
    ax.axis('off')
    ax.set_title(f'center={center:g}')
plt.tight_layout()
plt.show()

image

With only two colors:

image

So, setting center=0 gets what you asked for.

Read more comments on GitHub >

github_iconTop Results From Across the Web

row_colors/col_colors not working when a non-default ...
Basically, I am trying to compute a clustermap using both row_colors and a colorbar normalization which is different from the default one. Let's ......
Read more >
Problems Repaired for Version 6.1
There is a bug if a 2D Bridge has 100 or more cells/faces along the centerline of the bridge. This may show up...
Read more >
View and set current colormap - MATLAB colormap - MathWorks
This MATLAB function sets the colormap for the current figure to one of the predefined colormaps.
Read more >
Using MATLAB Graphics
bugs @mathworks.com ... Indexed Color Surfaces — Direct and Scaled Colormapping 11-16 ... Factors to consider when using a nondefault print driver,.
Read more >
What's new in each version — seaborn 0.11.2 documentation
Fix In histplot() , fixed a bug where using shrink with non-discrete bins shifted ... this may cause plots to look different with...
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