Question

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   
Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top