Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding labels in x y scatter plot with seaborn

I've spent hours on trying to do what I thought was a simple task, which is to add labels onto an XY plot while using seaborn.

Here's my code

import seaborn as sns import matplotlib.pyplot as plt %matplotlib inline  df_iris=sns.load_dataset("iris")   sns.lmplot('sepal_length', # Horizontal axis            'sepal_width', # Vertical axis            data=df_iris, # Data source            fit_reg=False, # Don't fix a regression line            size = 8,            aspect =2 ) # size and dimension  plt.title('Example Plot') # Set x-axis label plt.xlabel('Sepal Length') # Set y-axis label plt.ylabel('Sepal Width') 

I would like to add to each dot on the plot the text in "species" column.

I've seen many examples using matplotlib but not using seaborn.

Any ideas? Thank you.

like image 293
Trexion Kameha Avatar asked Sep 03 '17 20:09

Trexion Kameha


People also ask

How do you add labels to a scatter plot?

Do add the data labels to the scatter chart, select the chart, click on the plus icon on the right, and then check the data labels option. This will add the data labels that will show the Y-axis value for each data point in the scatter graph.

How do I add a title to a scatterplot in Seaborn?

To add a title to a single seaborn plot, you can use the . set() function. To add an overall title to a seaborn facet plot, you can use the . suptitle() function.


2 Answers

One way you can do this is as follows:

import seaborn as sns import matplotlib.pyplot as plt import pandas as pd %matplotlib inline  df_iris=sns.load_dataset("iris")   ax = sns.lmplot('sepal_length', # Horizontal axis            'sepal_width', # Vertical axis            data=df_iris, # Data source            fit_reg=False, # Don't fix a regression line            size = 10,            aspect =2 ) # size and dimension  plt.title('Example Plot') # Set x-axis label plt.xlabel('Sepal Length') # Set y-axis label plt.ylabel('Sepal Width')   def label_point(x, y, val, ax):     a = pd.concat({'x': x, 'y': y, 'val': val}, axis=1)     for i, point in a.iterrows():         ax.text(point['x']+.02, point['y'], str(point['val']))  label_point(df_iris.sepal_length, df_iris.sepal_width, df_iris.species, plt.gca())   

enter image description here

like image 129
Scott Boston Avatar answered Sep 23 '22 09:09

Scott Boston


Here's a more up-to-date answer that doesn't suffer from the string issue described in the comments.

import seaborn as sns import matplotlib.pyplot as plt %matplotlib inline  df_iris=sns.load_dataset("iris")   plt.figure(figsize=(20,10)) p1 = sns.scatterplot('sepal_length', # Horizontal axis        'sepal_width', # Vertical axis        data=df_iris, # Data source        size = 8,        legend=False)    for line in range(0,df_iris.shape[0]):      p1.text(df_iris.sepal_length[line]+0.01, df_iris.sepal_width[line],       df_iris.species[line], horizontalalignment='left',       size='medium', color='black', weight='semibold')  plt.title('Example Plot') # Set x-axis label plt.xlabel('Sepal Length') # Set y-axis label plt.ylabel('Sepal Width') 

enter image description here

like image 29
compBio Avatar answered Sep 25 '22 09:09

compBio