I seem unable to show the color bar for a two dimensional histplot using seaborn FacetGrid. Can someone point me to the missing link please?
Understanding that similar solutions have been discussed I have not been able to adapt to my use case:
- Has the right position and values for color bar but isn't working for histplot
- This proposal is not running at all & is rather dated so I am not sure it is still supposed to work
- Seems to have fixed vmin/vmax and does not work with histplot
Specifically I am looking to extend the code below so that color bar is shown.
import pandas as pd
import numpy as np
import seaborn as sns
df = pd.DataFrame(list(zip([random.randint(0,10) for i in range(1000)], pd.to_datetime(
[d.strftime('%Y-%m-%d') for d in pd.date_range('1800-01-01', periods=250, freq='1d')]+\
[d.strftime('%Y-%m-%d') for d in pd.date_range('1800-01-01', periods=250, freq='1d')]+\
[d.strftime('%Y-%m-%d') for d in pd.date_range('1800-01-01', periods=250, freq='1d')]+\
[d.strftime('%Y-%m-%d') for d in pd.date_range('1800-01-01', periods=250, freq='1d')]),
[random.choice(string.ascii_letters[26:30]) for i in range(1000)])),
columns=["range","date","case_type"])
df["range"][df["case_type"]=="A"] = [random.randint(4562,873645) for i in range(1000)]
df["range"][df["case_type"]=="C"] = [random.random() for i in range(1000)]
fg = sns.FacetGrid(df, col="case_type", col_wrap=2, sharey=False)
fg.map(sns.histplot, "date", "range", stat="count", data=df)
fg.set_xticklabels(rotation=30)
fg.fig.show()
The objective would be to have a color bar on the right side of the facet grid, spanning the entire chart - two rows here but more may be shown. The displayed 2D histogram feature some very different data types so the counts per bin & color are likely very different and it matters to know if "dark blue" is 100 or 1000.
EDIT: For sake of clarity it appears from comments that the problem breaks down into two steps:
- How to normalize the color coding among all plots and
- Display a color bar on the right side of the plot using the normalized color mapping

