Search code examples
pythonmatplotlibseaborncolorbar

Horizontal and vertical colorbars on a single plot in python


I have KDE plot generated using seaborn on which I overlay some scattered points with some size. I need to have two colorbars to represent the data of KDE and the scattered values's size (as shown below).

I tried to use the answer provided here at Stackoverflow, however, it is not generating the desired results as shown in the screenshot below

enter image description here

Question: How can I correct the horizontal colorbar too so that there is some vertical gap between colorbar and the main plot? Also, the length of the horizontal bar is a bit too much and needs to have the same length as the width of the main plot.

MWE:

import matplotlib.pyplot as plt
from mpl_toolkits import axes_grid1
import seaborn as sns
import numpy as np

def add_colorbar(im, aspect=20, pad_fraction=0.5, orientation='vertical', **kwargs):
    """Add a color bar to an image plot with an option for horizontal or vertical colorbars."""
    divider = axes_grid1.make_axes_locatable(im.axes)
    
    if orientation == 'horizontal':
        width = axes_grid1.axes_size.AxesX(im.axes, aspect=1./aspect)  # Horizontal colorbar
        pad = axes_grid1.axes_size.Fraction(pad_fraction, width)
        cax = divider.append_axes("bottom", size=width, pad=pad)  # Place at the bottom
    else:
        width = axes_grid1.axes_size.AxesY(im.axes, aspect=1./aspect)  # Default vertical colorbar
        pad = axes_grid1.axes_size.Fraction(pad_fraction, width)
        cax = divider.append_axes("right", size=width, pad=pad)  # Place at the right
    
    return im.axes.figure.colorbar(im, cax=cax, orientation=orientation, **kwargs)

# Dummy data 
x_values = np.random.rand(100)
y_values = np.random.rand(100)
size_values = np.random.rand(100)
kd = np.random.rand(100, 2)
kde_params_x = np.mean(kd[:, 0])
kde_params_y = np.mean(kd[:, 1])

# Create a plot
fig, ax = plt.subplots(figsize=(8, 8))

# Plot the multidimensional KDE for the winning data
kde = sns.kdeplot(x=kd[:, 0], y=kd[:, 1], fill=True, cmap='crest', bw_adjust=0.5, alpha=0.7, ax=ax)

# colorbar for the KDE plot
add_colorbar(kde.collections[0], orientation='horizontal')  # orientation to 'horizontal'

# Overlay the empirical scatter plot with contrasting colors
scatter = ax.scatter(x_values, y_values, s=2,   c=size_values, cmap='plasma', alpha=0.8, edgecolor='black', linewidth=0.5, label='Empirical Data')

# Add a colorbar for the scatter plot (vertical, as before)
add_colorbar(scatter, orientation='vertical')  # vertical orientation

plt.show()

Solution

  • If you replace your add_colorbar function calls by this:

    plt.colorbar(kde.collections[0], shrink=0.8, orientation='horizontal', location='bottom', pad=0.05, anchor=(0.0, 0.5))
    plt.colorbar(scatter, shrink=1.0, orientation='vertical', location='right', pad=0.05, anchor=(0.0, 0.5))
    

    You will get:

    res