Add Colorbar to Ridgline plot

31 Views Asked by At

I want to add a color bar to my ridgeline plot. Only when I add it, it shows only at the last line of my ridgeline plot, while I would it to be on the right of all the line (over the complete height and have the same lengths for all the plots).

My current plotting code for this part is as per below:

# Provide inputs for colorbar
norm = plt.Normalize(wind['Normalized'].min(), wind['Normalized'].max())
sm = plt.cm.ScalarMappable(cmap="coolwarm", norm=norm)
plt.colorbar(sm, ax=ax)

I might have to work with cax, but I struggle to define this properly. The full plotting code is as per below:

# Set color pallete
pal = sns.color_palette(palette='coolwarm', n_colors=12)

# Apply sns.FacetGrid class, with the 'hue' argument to represented colors by 'palette'
g = sns.FacetGrid(wind, row='month', hue='mean_month', aspect=15, height=0.75, palette=pal, xlim=(0,100))

# Add the densities kdeplots for each month
g.map(sns.kdeplot, 'Normalized',
  bw_adjust=1, clip=(0,100),
  fill=True, alpha=1, linewidth=1.5)

# Add a horizontal line for each plot
g.map(plt.axhline, y=0,
  lw=2, clip_on=False)

# Loop over the FacetGrid figure axes (g.axes.flat) and add the month as text with the right color
for i, ax in enumerate(g.axes.flat):
ax.text(-13, 0.0, month_dict[i+1],
        fontweight='bold', fontsize=14,
        color=ax.lines[-1].get_color())

# Use matplotlib.Figure.subplots_adjust() function for subplots to overlap
g.figure.subplots_adjust(hspace=-0.25)

# Provide inputs for colorbar
norm = plt.Normalize(wind['Normalized'].min(), wind['Normalized'].max())
sm = plt.cm.ScalarMappable(cmap="coolwarm", norm=norm)

# Remove axes titles, yticks, etc. and add labels
g.set_titles("")
g.set(yticks=[])
g.set(ylabel=None)
g.despine(bottom=True, left=True)

plt.setp(ax.get_xticklabels(), fontsize=15, fontweight='bold')
plt.xlabel('Wind output [%]', fontweight='bold', fontsize=15)
g.figure.suptitle('Dutch Offshore wind per month (2021-2022)',
           ha='right',
           fontsize=20,
           fontweight=20)
plt.colorbar(sm, ax=ax)

plt.show()

Currently the figure works out as follows: Current output with colorbar only at last month

Tried to play with adjusting ax input or cax, but this also impact my x-ax label, and still I did not manage to get it in the right position. Also most solution work with fig, ax = plt.subplots() or alike, but this plot is set-up differently.

0

There are 0 best solutions below