Setting numpoints in matplotlib legend does not work

孤人 提交于 2019-12-10 12:33:55

问题


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()

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

In [147]:  import matplotlib 
           print matplotlib.__version__

Out [147]: 1.1.1rc   

回答1:


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()



来源:https://stackoverflow.com/questions/23233290/setting-numpoints-in-matplotlib-legend-does-not-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!