I'm working on a project about my Spotify playlists, which output is 6 ditribution graphs with 3 lines each. Something like this:
I would like for every curve to plot a vertical line reaching the top of each curve, and to put the x value on the x axis. Something like this:
I have found a solution here. But after having trying a lot of work around it seems that this doesn't work for me because I use sub plot. I have done a simplier/usable code if you want to work with.
from pylab import *
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
input1 = np.random.normal(loc=0.2, scale=1.0, size=25)
output1 = np.random.normal(loc=0.3, scale=1.0, size=25)
input2 = np.random.normal(loc=0.1, scale=1.0, size=25)
output2 = np.random.normal(loc=0.15, scale=1.0, size=25)
inputs = [input1 , input2]
outputs = [output1 , output2]
my_alpha = 0.03
my_linewidth = 0.7
for i in range(len(inputs)):
fig = subplot(1,2,i+1)
ax1 = sns.kdeplot(inputs[i], shade=True, alpha=my_alpha, linewidth=my_linewidth, label = 'Input Playlist', color = '#2cc758')
ax2 = sns.kdeplot(outputs[i], shade=True, alpha=my_alpha, linewidth=my_linewidth, label = 'Recommandations Made', color = '#dbb818')
myLegend = plt.legend(loc='lower left', bbox_to_anchor=(-1.2,1.05), ncol=3) #legend location is set from the last ploted graph
myLegend.set_title("Tracks Set")
plt.setp(myLegend.get_title())
plt.show()
If you have some ideas I'll be glad to read you.
Thanks to the community
For this to work with the latest seaborn versions,
fill=False
is needed to obtain a line. Then, that line can be used forfill_between
and to extract the highest point. When there is more than one kdeplot onto the same subplot,ax.lines[-1]
gives the last added curve.It helps to use
for
loops without a counter to make the code easier to maintain.