0

Look at the second figure here, I'm currently working with a scatterplot and when I plot the legend I get two "sample points" (in the link they are the two blue stars). How can I show just one of them? Why does matplotlib plot 2 of them?

The sort of code that I'm using is

plt.legend(bbox_to_anchor=(0., 1.02, 1., .102), mode="expand", borderaxespad=0.)

I'm new to this so I still don't use 'legend handles'.

Thanks in advance

Vladimir Vargas
  • 1,744
  • 4
  • 24
  • 48
  • I just noticed, this question already has an answer here: http://stackoverflow.com/a/17412294/3005167 – MB-F Jan 28 '15 at 16:50

1 Answers1

1

For scatter plots you need to set scatterpoints.

import matplotlib.lines as mlines
import matplotlib.pyplot as plt

plt.figure(figsize=(6, 2))

plt.subplot(1, 2, 1)
plt.scatter([1, 2, 3, 4, 5], [1, 2, 5, 3, 4])
plt.legend(['text'])

plt.subplot(1, 2, 2)
plt.scatter([1, 2, 3, 4, 5], [1, 2, 5, 3, 4])
plt.legend(['text'], scatterpoints=1)

plt.show()

enter image description here

MB-F
  • 22,770
  • 4
  • 61
  • 116
  • It doesn't work because the objects that are plotted are lines instead of points. numpoints just adds points to the line. – Vladimir Vargas Jan 28 '15 at 16:30
  • I have edited my answer. If that is not what you asked please reconsider rephrasing your question. – MB-F Jan 28 '15 at 16:35
  • I think the question is ok, I have a scatterplot and the legend visually looks like the one in the second example, I want just one point instead of two. The solution isn't working :( – Vladimir Vargas Jan 28 '15 at 16:41
  • 1
    You are right, it doesn't work for scatterplots. I've been confused by the example you referred to, which does not have a scatterplot. – MB-F Jan 28 '15 at 16:43
  • 1
    @VladimirVargas: Answer updated to work with scatter plots. – MB-F Jan 28 '15 at 16:49