在matplotlib图例中设置numpoints不起作用

时间:2014-04-23 01:35:09

标签: python matplotlib legend

我正在尝试按照建议here在地块图例上设置一个数据点,但它似乎不起作用:

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

我在做一些完全愚蠢的事情吗?一些其他信息:

In [147]:  import matplotlib 
           print matplotlib.__version__

Out [147]: 1.1.1rc   

1 个答案:

答案 0 :(得分:14)

对于散点图,请使用scatterpoints参数:

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