1

There are two main things I would like to do for my scatterplot in seaborn.

  1. Change the Legend Title and remove the series name from the legend. So remove the petal_length that appears in legend
  2. Change the numeric output for the legend. Change to the range [0,4,8]

How do I do that?

import seaborn as sns
cmap = sns.light_palette("green", as_cmap=True)
iris = sns.load_dataset("iris")
p=sns.scatterplot("sepal_length", "sepal_width",data=iris
                  ,hue="petal_length"
                  ,palette = cmap
                  )
p.set(xlabel='Sepal Len', ylabel='Sepal Width')
p.legend(title="Petal Length")
plt.show()

enter image description here

Jack Armstrong
  • 1,182
  • 4
  • 26
  • 59

2 Answers2

0

Adding title to the plot How to add title to seaborn boxplot

Removing legend title: Remove seaborn lineplot legend title

Changing how the legend 'buckets': You have set hue to a column of the data. I would bucket the length into the groups I want in a new column, then set the hue to the new column.

cyneo
  • 876
  • 7
  • 9
  • I should have clarified. I am trying to remove the legend title 'petal_length' not add a Plot Title. I have changed the question to reflect that. – Jack Armstrong Aug 14 '20 at 15:55
  • 2
    please read the accepted answer, especially the part with this code: handles, labels = ax.get_legend_handles_labels() ax.legend(handles=handles[1:], labels=labels[1:]) – cyneo Aug 14 '20 at 16:01
0

With get_legend_handles_labels() you can get a list of the handles and the labels meant to go in the legend. Then, you can create the legend skipping every other element of both the handles and the labels.

from matplotlib import pyplot as plt
import seaborn as sns

cmap = sns.light_palette("green", as_cmap=True)
iris = sns.load_dataset("iris")
ax = sns.scatterplot("sepal_length", "sepal_width", data=iris, hue="petal_length", palette=cmap)
ax.set_xlabel('Sepal Len'),
ax.set_ylabel('Sepal Width')
handles, labels = ax.get_legend_handles_labels()
ax.legend(handles[1::2], labels[1::2], title="Petal Length")
plt.show()

example plot

JohanC
  • 71,591
  • 8
  • 33
  • 66