2014-04-23 27 views
11

我想有一個圖例的單一數據點通過以下建議here,它似乎並沒有工作:在matplotlib傳奇設置爲NumPoints不起作用

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 

回答

13

對於散點圖,使用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

+0

好極了,謝謝! – nikosd

+3

偉大的答案,將永遠不會猜到這一點。使用兩個不同的參數來做幾乎相同的事情似乎毫無意義。 – Gabriel

+0

軸可以同時包含線圖和散點圖,所以有兩個參數'scatterpoints'和'numpoints',允許您獨立控制這些圖例項目的外觀。 – unutbu