y = x * 2
ax.plot(x, y, '-', color='black', linewidth=5)
ax.plot(x, y, '--', color='lawngreen', linewidth=5)
dotted_line1 = lines.Line2D([], [], linewidth=5, linestyle='--', dashes=(10,
    1), color='lawngreen')
dotted_line2 = lines.Line2D([], [], linewidth=5, linestyle='-', dashes=(5, 
    4), color='black')
plt.legend([(dotted_line1, dotted_line2)], ['My Line'])
plt.show()
How to get alternating colours in dashed line using matplotlib?