Skip to content
Advertisement

How to get alternating colours in dashed line using matplotlib?

In matplotlib, I want to make a line using matplotlib.pyplot which is alternating black and yellow dashes, and then I want to include that line on the legend. How do I do that?

I could do something like:

JavaScript

but then the line on the legend would appear as a solid black line, rather than as black-and-yellow dashes.

I did look at matplotlib.path_effects but I can’t work out whether it’s possible to achieve what I want; I can outline or shadow the line, but I’m not sure I can overlay a differently-coloured dashed line.

Advertisement

Answer

Try this.

JavaScript

i increased the line width so it is clearly visible. As yellow was not that clear in a white background; changed it to green. Sorry about that. You can change colors any time any way :) output

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement