12

I am trying to have a single data point on a plot legend by following the suggestions here and it does not seem to work:

from pylab import scatter
import pylab
import matplotlib.pyplot as plt

fig = plt.figure()
ax = plt.gca()
ax.scatter(1,2,c = 'blue', marker = 'x')
ax.scatter(2,3, c= 'red', marker = 'o')
ax.legend(('1','2'), loc = 2, numpoints = 1)
plt.show()

code output

Am I doing something completely stupid here? Some additional information:

In [147]:  import matplotlib 
           print matplotlib.__version__

Out [147]: 1.1.1rc   
Community
  • 1
  • 1
nikosd
  • 919
  • 3
  • 16
  • 26

1 Answers1

14

For scatterplots, use the scatterpoints parameter:

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.scatter(1, 2, c='blue', marker='x')
ax.scatter(2, 3, c='red', marker='o')
ax.legend(('1', '2'), loc=2, scatterpoints=1)
plt.show()

enter image description here

unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
  • 4
    Great answer, would've never guessed this. Using two different parameters to do pretty much the same thing seems quite pointless. – Gabriel Mar 02 '15 at 01:32
  • 1
    Axes can contain both line plots and scatter plots, so having two parameters, `scatterpoints` and `numpoints`, allows you to control the look of those legend items independently. – unutbu Aug 12 '16 at 18:30