3

Using this code, I generated the following graph:

g = sns.lmplot('credibility', 'percentWatched', data=data, hue = 'gender', markers = [".", "."], x_jitter = True, y_jitter = True, size=5, palette="Set2", scatter_kws={'alpha': 0.2})
g.set(xlabel = 'Credibility Ranking\n ← Low       High  →', ylabel = 'Percent of Video Watched [%]')
g.set(xlim=(1, 7))

new_title = 'Gender'
g._legend.set_title(new_title)
# replace labels
new_labels = ['Male', 'Female']
for t, l in zip(g._legend.texts, new_labels): t.set_text(l)

enter image description here

As you can see, the legend overlaps with the actual graph. How can I move over the legend more to the right so you can see the full legend?

ImportanceOfBeingErnest
  • 289,005
  • 45
  • 571
  • 615
Oliver G
  • 811
  • 2
  • 8
  • 19

2 Answers2

3

Seaborn is not good at handling legends, if you have this issue, turn off the legend in seaborn (legend = False) and try to override it through matplotlib.

g = sns.lmplot('credibility', 'percentWatched', data=data, legend = False, hue = 'gender', markers = [".", "."], x_jitter = True, y_jitter = True, size=5, palette="Set2", scatter_kws={'alpha': 0.2})
g.set(xlabel = 'Credibility Ranking\n ← Low       High  →', ylabel = 'Percent of Video Watched [%]')
g.set(xlim=(1, 7))

# replace labels
new_labels = ['Male', 'Female']
plt.legend(bbox_to_anchor=(1.05, 0.5), title='Gender', labels = new_labels)

Hope it will help.

Foxpace
  • 114
  • 1
  • 5
1

Just as you set the legend's title after the plot creation, you can also change the legend's position.

g._legend.set_bbox_to_anchor((1.13,.8))
ImportanceOfBeingErnest
  • 289,005
  • 45
  • 571
  • 615