Matplotlib legend: how to assign multiple scatter point values

I am using the matplotlib library in python to create xy markup graphs. I ran into a problem regarding markers in a legend. I plan two different xy-scatter series; one is a set of xy points forming 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 one point. The only way I know how to change the number of legendary markers is to use the "scatter points" argument when declaring a legend. However, this argument sets the number of markers for all episodes in the legend, and I'm not sure how to change each legend entry individually.

Unfortunately, I canโ€™t post photos as a new user, but hopefully this description is enough. Is there a way to set the scatter point values โ€‹โ€‹individually for each legend entry using matplotlib?

EDIT: Here are links showing images with different values โ€‹โ€‹for the scatter points.

scattering points = 3: http://imgur.com/8ONAT

scatterpoints = 1: http://imgur.com/TFcYV

Hope this makes the problem more clear.

+4
source share
1 answer

you can get the string in the 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 the legend, and the _legmarker attribute of the line is the label.

You can call set_markevery (3) or set_xdata () and set_ydata () to change the number of labels.

enter image description here

+4
source

All Articles