0

I use a Seaborn facetgrid with four variables and try to visualize it with hist2d heatmaps.

Here is my code:

def heatmap_grid_hour_duration_user_type(x, y, **kwargs):
    """ Quick hack for creating heat maps with seaborn's PairGrid. """
    palette = kwargs.pop("color")
    bins_x = np.arange(0, df_main["start_hour"].max(), 1)
    bins_y = np.arange(0, log_trans(2000), 0.1)
    plt.hist2d(x, y, bins=[bins_x, bins_y], cmap=palette, cmin=0.5)
    plt.xticks(
        [0, 4, 8, 12, 16, 20], [0, 4, 8, 12, 16, 20],
    )
    plt.yticks(log_trans(np.array(labels)), labels)
    plt.colorbar(label="Count")


g = sns.FacetGrid(
    data=df_main,
    col="start_day_of_week",
    row="user_type",
    row_order=user_type_order,
    height=2.5,
    margin_titles=True,
)
g.map(
    heatmap_grid_hour_duration_user_type,
    "start_hour",
    "log_duration_min",
    color="inferno_r",
)
g.set_xlabels("Start Hour")
g.set_ylabels("Logarithmic Duration (min)");

That produces this image: enter image description here

How can I create only one colorbar for each row with the same maximum and minimum scaling for each chart in the row? In other words, each row should have an independent colorbar for itself.

Also, the colorbar overwrites the variable names. enter image description here

I have already tried different variants with vmin, vmax, sharex and sharey, but I did not get the desired result. Other posts with similar approaches unfortunately did not have the row characteristics or have differed too much and so I was not able to understand these solution approaches using axes.

bjoeas
  • 1
  • 1
  • This is a slightly simpler question but I think you should be able to adapt the answer: https://stackoverflow.com/questions/49875722/seaborn-facetgrid-place-single-colorbar-after-last-subplot – mwaskom Nov 01 '22 at 10:43

0 Answers0