The Python Oracle

matplotlib Legend Markers Only Once

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

Track title: CC F Haydns String Quartet No 53 in D

--

Chapters
00:00 Question
00:39 Accepted answer (Score 255)
01:10 Answer 2 (Score 24)
01:42 Thank you

--

Full question
https://stackoverflow.com/questions/6146...

Accepted answer links:
[Link to API docs]: http://matplotlib.org/api/axes_api.html#...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #matplotlib

#avk47



ACCEPTED ANSWER

Score 254


This should work:

legend(numpoints=1)

BTW, if you add the line

legend.numpoints     : 1      # the number of points in the legend line

to your matplotlibrc file, then this will be the new default.

[See also scatterpoints, depending on your plot.]

API: Link to API docs




ANSWER 2

Score 24


I like to change my matplotlib rc parameters dynamically in every python script. To achieve this goal I simply use somthing like that at the beginning of my python files.

from pylab import *
rcParams['legend.numpoints'] = 1

This will apply to all plots generated from my python file.

EDIT: For those who do not like to import pylab, the long answer is

import matplotlib as mpl
mpl.rcParams['legend.numpoints'] = 1