Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting numpoints in matplotlib legend does not work

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   
like image 909
nikosd Avatar asked Apr 23 '14 01:04

nikosd


People also ask

How do I fix the legend position in Matplotlib?

To change the position of a legend in Matplotlib, you can use the plt. legend() function. The default location is “best” – which is where Matplotlib automatically finds a location for the legend based on where it avoids covering any data points.

How do I change the legend box size in Matplotlib?

To place a legend on the figure and to adjust the size of legend box, use borderpad=2 in legend() method. To display the figure, use show() method.


1 Answers

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

like image 69
unutbu Avatar answered Oct 07 '22 01:10

unutbu