buq333 buq333 - 2 months ago 13
Python Question

python matplotlib legend linestyle '---'

I need a way to make matplotlib linestyle '---'. 3's of '-'.

character description
'-' solid line style
'--' dashed line style
'-.' dash-dot line style
':' dotted line style
etc.


I can see '-' and '--' in the list, but on the right up side, my legend comes up like " -- red dotted line" (If I write linestyle = '--'). I want '--- red dotted line' on my legend box

Is there any way I can make the legend show three dashes?
Here's what I'm doing.

import matplotlib.pyplot as mpt

def main():
mpt.title("hi")
mpt.xlabel("x axis")
mpt.ylim([0,50])
mpt.xlim([0,10])
mpt.ylabel("y axis")
mpt.plot([1,2,3,4],[2,3,4,5],'r', linestyle = '???????')
mpt.legend(["red dotted line"])
mpt.show()

main()

Answer

Use mpt.legend(handlelength=3) and linestyle='--'

mpt.plot([1,2,3,4],[2,3,4,5],'r', linestyle='--')
mpt.legend(["red dotted line"], handlelength=3)