Search code examples
pythonmatplotlibseaborncolorbar

Plot Colorbar for stripplot


I have a stripplot where I use binned data for the coloration of the datapoints. Instead of a legend I'd like to show a colorbar. I've looked at many examples but I'm stuck at how to use this with a Seaborn Stripplot (and my dataset).

I have a dataframe dfBalance which has numerical data in column Balance and the different categories in column Parameter. It also contains time in a column Time which I use for binning for coloration of the datapoints

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

df = pd.DataFrame([[40, 'A',65], [-10, 'A',125], [60, 'B',65], [5, 'B',135], [8, 'B',205], [14, 'B',335]], columns=['Balance', 'Parameter', 'Time'])

bin = np.arange(0,max(df['Time']),60)
df['bin'] = pd.cut(abs(df['Time']),bin,precision=0)

plt.figure()
cpal=sns.color_palette('cmo.balance',n_colors=28,desat=0.8)
plt.style.use("seaborn-dark")
ax = sns.stripplot(x='Balance', y='Parameter', data=df, jitter=0.15, edgecolor='none', alpha=0.4, size=4, hue='bin', palette=cpal)
sns.despine()
ax.set_xlim([-45,45])
plt.title("L/R Balance")
plt.xlabel("Balance % L/R")
plt.ylabel("Parameter")
sns.set_context("notebook", font_scale=1.5, rc={"lines.linewidth": 2.5})
plt.legend(loc=1)

plt.show()

This produces a plot as follows: example plot

Is there any way to replace the legend with a colorbar?


Solution

  • In the end, I think the solution should be exactly the same as the one proposed in this recent question, and the current question could/should be marked as a duplicate.

    I had to modify your code a bit because I could not see the points very well, but hopefully that wont make much of a difference.

    from matplotlib.cm import ScalarMappable
    
    df = pd.DataFrame({'Balance': np.random.normal(loc=0, scale=40, size=(1000,)),
                       'Parameter': [['A','B'][i] for i in np.random.randint(0,2, size=(1000,))],
                       'Time': np.random.uniform(low=0, high=301, size=(1000,))})
    
    bin = np.arange(0,max(df['Time']),60)
    df['bin'] = pd.cut(abs(df['Time']),bin,precision=0)
    
    plt.figure()
    cpal=sns.color_palette('Spectral',n_colors=5,desat=1.)
    plt.style.use("seaborn-dark")
    ax = sns.stripplot(x='Balance', y='Parameter', data=df, jitter=0.15, edgecolor='none', alpha=0.4, size=4, hue='bin', palette=cpal)
    sns.despine()
    ax.set_xlim([-45,45])
    plt.title("L/R Balance")
    plt.xlabel("Balance % L/R")
    plt.ylabel("Parameter")
    sns.set_context("notebook", font_scale=1.5, rc={"lines.linewidth": 2.5})
    
    # This is the part about the colormap
    cmap = plt.get_cmap("Spectral")
    norm = plt.Normalize(0, df['Time'].max())
    sm =  ScalarMappable(norm=norm, cmap=cmap)
    sm.set_array([])
    cbar = fig.colorbar(sm, ax=ax)
    cbar.ax.set_title("\"bins\"")
    
    #remove the legend created by seaborn
    ax.legend_.remove()
    
    plt.show()
    

    enter image description here