Adding labels in x y scatter plot with seaborn

前端 未结 3 623
孤独总比滥情好
孤独总比滥情好 2020-11-30 07:36

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

impor         


        
3条回答
  •  生来不讨喜
    2020-11-30 08:26

    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')
    

提交回复
热议问题