As the question says, I have a contour plot and I would like show a legend for if.
I'm using the contour plot style that uses:
dashed lines for negative levels
solid lines for positive values
I would like to have a legend for them (dashed == negative and solid == positive).
I tried the approaches found here and here. However, as can be seen below, this doesn't show the correct result.
# Draw the scalar field level curves
div_field = plt.contour(x, y, div_scalar_field, colors='white')
rot_field = plt.contour(x, y, rot_scalar_field, colors='lightgoldenrodyellow')
labels = ['Div Neg', 'Div Pos', 'Rot Neg', 'Rot Pos']
div_field.collections[0].set_label(labels[0])
div_field.collections[-1].set_label(labels[1])
rot_field.collections[0].set_label(labels[2])
rot_field.collections[-1].set_label(labels[3])
As I for the div scalar field I just have positive levels, I got two labels with the same line style.
I'm wondering how could I achieve what I want properly.
Thank you in advance.
Something like the following works for me - this complete hack is to use a labelled dummy point, fetch its colour, apply that to the contours and then just plot the legend in the usual way:
Hope that makes sense.
I could solve this manually setting the legend (which I don't know if it's the best approach):