Jens Rantil's blog

Posted Tis 28 Maj 2013

Line styles in matplotlib

I'm a big fan of matplotlib and tend to use it every time I'd like to plot something. A while ago I had a line plot with many lines and ran out of default colors. When that happens, matplotlib will reuse the previously used colors.

I used Python's itertools overcome this:

look = itertools.cycle(itertools.product(['-', '--', '.-', ':', ','], 'bgrcmykw'))
look = it.imap(lambda x: string.join(x, ''), look)
for item in data.items():
    p.plot(item.X, item.Y, next(look), label=item.label)

I hope this little piece of code will come in handy for someone else!

Category: misc
Dude, did you like this blog post? Feel free to drop me some bitcoins at 1Q79HGDF3ZjfCwXLAW6e7gX4DeDu2GtZ7v.

Comments