The code below has been prepared to a ridgeline plot for monthly data. Somehow, I am not able to align the month labels with the density plots. See below the code and output. Any advise on how to update the code to do the alignment better?
# 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(-15, 0.02, 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.1)
# 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.show()
Current plot:
For another example, the code did nice alignment, but now with a different dataset it is off. Adjusting the hspace
did not help.
In ax.text(-15, 0.02, ...)
change the y=0.02
argument to the average y density value. I think it's too large at the moment, causing the label to be moved far up the y axis. You might first need to delete the line g.set(yticks=[])
in order to see what the actual y density values are, and then add it back in when you're done.
Alternatively, as per the suggestion below by @JohanC (referencing this seaborn ridge plot example), you can specify y=
in axes coordinates as a value between 0 (bottom) and 1 (top). It saves you having to set y based on the data value.
#Place label at left (x=0) and half-way up the y axis (y=0.5)
ax.text(x=0, y=0.5, transform=ax.transAxes, s=<text string>, ...)