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.

histplot 2D ignores hue_norm argument

See original GitHub issue

Description

If I understand correctly, when plotting a 2D histogram with histplot, the hue_norm argument should allow setting the values corresponding to data values corresponding to the extremes of the colormap, or a normalization object from matplotlib.colors such as LogNorm.

However, it seems that the hue_norm argument is being ignored. Even providing objects of some unreasonable type the same plot is produced, with no errors or complains.

Versions

seaborn 0.11.1 matplotlib 3.4.1

Example

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

x = np.random.randn(1000)
y = np.random.randn(1000)

fig, axs = plt.subplots(ncols=4, figsize=(16,3))
sns.histplot(x=x, y=y, ax=axs[0], cbar=True)
sns.histplot(x=x, y=y, ax=axs[1], cbar=True, hue_norm=(6, 10))
sns.histplot(x=x, y=y, ax=axs[2], cbar=True, hue_norm=LogNorm())
sns.histplot(x=x, y=y, ax=axs[3], cbar=True, hue_norm='I would expect strings to crash')

histplot_hue_norm_being_ignored

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
mgabcommented, Apr 1, 2021

I don’t think hue_norm is especially useful in the histogram context (though perhaps moreso for univariate histograms), but it is part of the core hue mapping API and currently all relevant mapping parameters are included in each function that allows that mapping.

I suspected that. Seems totally reasonable to me! 👌

[…] Beyond the core functionality, each new example that’s relevant to you is an example that’s irrelevant to someone else, but makes it harder for them to find out how to do what they want. It can be hard to know exactly where to draw that line…

Fair enough! I guess it would have saved me (and you, sorry for the noise again) some time. But I totally see your point, so I leave it at your judgement. I would not mind having an example of a 2D histogram with log color scale, but I agree that it might be left for an answer at stackoverflow.

For controlling the histogram color scale, you can use kwargs that are passed through to pcolormesh, so vmin, vmax, and norm. I believe these should override the reparameterization that histplot does.

You’re right, sns.histplot(x=x, y=y, cbar=True, norm=LogNorm(), vmin=None, vmax=None) works. The thing is that at first I had only tried sns.histplot(x=x, y=y, cbar=True, norm=LogNorm()) -using the same data from the first message- but it fails because it conflicts with vmin and vmax set by histplot, not sure why I didn’t try to set the limits to None.

It also throws a MatplotlibDeprecationWarning about getting both norm and vmax/vmin parameters simultaneously, and threatens to become an error at v3.5.

In any case, unless you think something needs to be done about that norm and vmin/vmax conflict, I would say you can close the issue. Thanks a lot for the patience and clarity! 🙏 (and for the awesome library, of course! 😉 )

0reactions
mgabcommented, Apr 12, 2021

(Sorry for the silence, busy days)

The option I can think of would be to avoid passing vmin and vmax to pcolormesh when norm is present/not None. Then, it would be up to the user to manage same/different color norm. The ugly part is that the user providing both vmin/vmax and norm at the same would imply either raising an error (from seaborn or matplotlib) or ignoring either the parameters. And also, I guess that would require adding norm as an explicit parameter for seaborn, as it would modify/interact with other explicit parameters

Trying to set vmin/vmax to the Normalize subclass would work for LogNorm but is way less robust. Not all the subclasses in matplotlib.colors have these attributes.

Tot convinced by any option… 🤔 Do you have any approach in mind?

Read more comments on GitHub >

github_iconTop Results From Across the Web

seaborn.histplot — seaborn 0.12.1 documentation - PyData |
Generic bin parameter that can be the name of a reference rule, the number of bins, or the breaks of the bins. ......
Read more >
matplotlib.pyplot.hist2d — Matplotlib 3.1.2 documentation
Rendering the histogram with a logarithmic color scale is accomplished by passing a colors. LogNorm instance to the norm keyword argument.
Read more >
MATLAB hist3 - Bivariate histogram plot - MathWorks
Similarly, hist3 ignores Inf and –Inf values unless you explicitly specify Inf or –Inf as a bin edge by using the edges input...
Read more >
2D Plots Reporting - ClearML
To report more than one series on the same plot, use same the title argument. For different plots, use different title arguments. Specify...
Read more >
Chapter: Histograms - ROOT
Making a projection from a 2-D or 3-D histogram. Reading a histogram from a ... The second parameter (1000) indicates how many random...
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