简体   繁体   中英

How to get the max x value of a seaborn distribution (and plot the corresponding line)?

I'm working on a project about my Spotify playlists, which output is 6 ditribution graphs with 3 lines each. Something like this:

enter image description here

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:

enter image description here

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 for fill_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.

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]

fig, axs = plt.subplots(ncols=len(inputs), figsize=(15, 5))
for ax, input_i, output_i in zip(axs, inputs, outputs):
    for data, label, color in zip([input_i, output_i],
                                  ['Input Playlist', 'Recommandations Made'],
                                  ['#2cc758', '#dbb818']):
        sns.kdeplot(data, fill=False, linewidth=0.7, label=label, color=color, ax=ax)
        xs, ys = ax.lines[-1].get_data()
        ax.fill_between(xs, ys, color=color, alpha=0.05)
        mode_idx = np.argmax(ys)
        ax.vlines(xs[mode_idx], 0, ys[mode_idx], ls='--', color=color)
        ax.text(xs[mode_idx], -0.1, f'{xs[mode_idx]:.2f}', color=color, ha='center', transform=ax.get_xaxis_transform())

myLegend = axs[0].legend(loc='lower left', bbox_to_anchor=(0, 1.02), ncol=3)
myLegend.set_title("Tracks Set")
plt.tight_layout()
plt.show()

sns.kdeplot 指示模式

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM