Matplotlib legend: how to assign multiple scatterpoints values
I'm using the matplotlib library in python to generate publication-quality xy scatter plots. I ran into a problem regarding the markers in the legend. I'm plotting 2 different xy-scatter series; one is a set of xy points that forms a curve, and the other is a single xy point.
I would like the legend to show 3 markers for the "curve", and 1 marker for the single point. The only way I know开发者_高级运维 how to change the number of legend markers is using the "scatterpoints" argument when declaring the legend. However, this argument sets the number of markers for all series in the legend, and I'm not sure how to change each legend entry individually.
Sadly I can't post pictures as a new user, but hopefully this description is sufficient. Is there a way to set scatterpoints values individually for each legend entry using matplotlib?
EDIT: Here are links showing images with different values for scatterpoints.
scatterpoints = 3: http://imgur.com/8ONAT
scatterpoints = 1: http://imgur.com/TFcYV
Hopefully this makes the issue a bit more clear.
you can get the line in legend, and change it yourself:
import numpy as np
import pylab as pl
x = np.linspace(0, 2*np.pi, 100)
pl.plot(x, np.sin(x), "-x", label=u"sin")
pl.plot(x, np.random.standard_normal(len(x)), 'o', label=u"rand")
leg = pl.legend(numpoints=3)
l = leg.legendHandles[1]
l._legmarker.set_xdata(l._legmarker.get_xdata()[1:2])
l._legmarker.set_ydata(l._legmarker.get_ydata()[1:2])
##or
#l._legmarker.set_markevery(3)
pl.show()
Legend.legendHandles is a list of all the lines in legend, and the _legmarker attribute of the line is the marks.
You can call set_markevery(3) or set_xdata() & set_ydata() to change the number of marks.
精彩评论