August Zhu
August Zhu

Reputation: 21

How to change the colormap of Seabron 2D histogram?

I'm using Seaborn to make a 2D histogram. However, I cannot change the colormap. The existing parameters palette and color are not designed for 2D histogram. I'm looking for a parameter like cmap in plt.hist2d().

And I have tried using plt.hist2d() as well. However, one dimension of my data is categorical, while the other is numerical. Thus, the plt.hist2d() reports error saying:

TypeError: ufunc 'isfinite' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

Any suggestions?

Upvotes: 2

Views: 4337

Answers (1)

JohanC
JohanC

Reputation: 80329

You can either change the base color via color=.... Or set a colormap via cmap=....

Here is an example using the tips dataset:

import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import numpy as np

tips = sns.load_dataset('tips')

fig, (ax1, ax2) = plt.subplots(ncols=2, figsize=(12, 5))

sns.histplot(data=tips, x='tip', y='day', color='purple', ax=ax1)
ax1.set_title("using 'color=...'")

sns.histplot(data=tips, x='tip', y='day', cmap='inferno', ax=ax2)
ax2.set_title("using 'cmap=...'")

plt.tight_layout()
plt.show()

sns.histplot with cmap

PS: In sns.histplot's documentation, cmap is referenced indirectly as

Other keyword arguments are passed to one of the following matplotlib functions:
     ...
     matplotlib.axes.Axes.pcolormesh() (bivariate)

Upvotes: 2

Related Questions