Unfortunately, there is currently no simple way to create a gradient colored line in matplotlib
. In this post, there are plenty of nice solutions to this problem, but they essentially all rely on using LineCollections
(or pcolormesh
, however such an artist is not supported by the legend). In and of itself, this is not a problem, but if you add the LineCollection
to the legend, its key will only have a single color, instead of a gradient (e.g. using unutbu's solution as an example here):
Now I know you can create a custom handler, e.g. as explained here, but I have no idea how to do this for such a case. If I look at the source code of the HandlerLineCollection
(which I thought would be an obvious starting point to figure out how to modify it) I noticed that it is based on Line2D
, which cannot handle color gradients. So I don't know how to proceed from here.
How can I add a color gradient line to the legend, where its key shows (at least part of) the lines color gradient as well?