Add midline with Seaborn - python

Asked

Viewed 364 times

0

As I include a vertical line referring to the average of each of the classes in the histograms, using Abor?

import numpy as np
import pandas as pd
import seaborn as sns
from sklearn import datasets
from sklearn.datasets import load_wine
wine = load_wine()
features = pd.DataFrame(data=wine['data'],columns=wine['feature_names'])
df_wine = features
df_wine['target']=wine['target']
df_wine['class']=df_wine['target'].map(lambda ind: wine['target_names'][ind])



fig = plt.figure(figsize=(10,8))
title = fig.suptitle("Alcohol", fontsize=14)
fig.subplots_adjust(top=0.93, wspace=0.3)

ax = fig.add_subplot(1,1,1)
ax.set_xlabel("Alcohol")
ax.set_ylabel("Frequency") 

g = sns.FacetGrid(data=df_wine, 
                  hue='class', 
                  palette={"class_0": "r", "class_1": "y", "class_2": "b"})

g.map(sns.distplot, 'alcohol', 
      kde=True, bins=15, ax=ax)

ax.legend(title='class')
plt.close(2)

saída do gráfico sem a linha da média

1 answer

1


There is a function of matplotlib which inserts a vertical line into the graph, is the .axvline(x=0, ymin=0, ymax=1, **kwargs), that has the documentation here or here.

In your case, just calculate the average of each class and insert it into the chart. One way to do this is to:

color_dict = {"class_0": "r", "class_1": "y", "class_2": "b"}
for target in df_wine['class'].unique():
    ax.axvline(df_wine[df_wine['class']==target]['alcohol'].mean(), 0, 1, color=color_dict[target])

Browser other questions tagged

You are not signed in. Login or sign up in order to post.