Contour plot legend - Matplotlib


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:


<strong>dashed</strong> lines for <strong>negative</strong> levels

<strong>solid</strong> lines for <strong>positive</strong> values


I would like to have a legend for them (dashed == negative and solid == positive).

I tried the approaches found <a href="https://stackoverflow.com/questions/16466674/matplotlib-multiple-legends-for-contour-plot-for-multiple-contour-variables" rel="nofollow">here</a> and <a href="https://stackoverflow.com/questions/10490302/how-do-you-create-a-legend-for-a-contour-plot-in-matplotlib" rel="nofollow">here</a>. 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])

<img alt="enter image description here" class="b-lazy" data-src="https://i.stack.imgur.com/fM3pk.png" data-original="https://i.stack.imgur.com/fM3pk.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" />

As I for the <strong>div scalar</strong> 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.


I could solve this manually setting the legend (which I don't know if it's the best approach):

div_neg = plt.Line2D((0, 1), (0, 0), color='white', linestyle='--', linewidth=2) div_pos = plt.Line2D((0, 1), (0, 0), color='white', linestyle='-', linewidth=2) rot_neg = plt.Line2D((0, 1), (0, 0), color='lightgoldenrodyellow', linestyle='--', linewidth=2) rot_pos = plt.Line2D((0, 1), (0, 0), color='lightgoldenrodyellow', linestyle='-', linewidth=2) plt.legend([rot_max, div_neg, div_pos, rot_neg, rot_pos], ['Rot Max Points', 'Div Neg', 'Div Pos', 'Rot Neg', 'Rot Pos'])

<img alt="enter image description here" class="b-lazy" data-src="https://i.stack.imgur.com/6pqI2.png" data-original="https://i.stack.imgur.com/6pqI2.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" />


  • How to make a circle semi-transparent in pygame?
  • Git rebase conflicts after successful merge?
  • Accessibility in custom drawn UITableViewCell
  • How To Draw More Precise Lines using Core Graphics and CALayer
  • Problems displaying full-screen CAEAGLLayer on Retina iPad
  • Assign different values to cell arrays in MATLAB at once
  • Using a rotation matrix opencv
  • How do I get width and height of my terminal with ioctl?
  • How to personalize a timeline with R?
  • Why does this use of getImageData leak memory
  • How to deal with SpiderWebPlot in JFreeChart?
  • Python sin and cosine giving incorrect values
  • Seemlessly layering transparent sprites in OpenGL ES
  • Rendering a google line chart, curveType not setting and animation not working as expected
  • Exactly what should happen in a CALayer's display/drawRect methods?
  • Displaying pdf files using the PDFKit interface
  • Smiles in output C++
  • WPF Image control clipping
  • How to set an entity field that does not exist on the table but does exists in the raw SQL as an ali
  • Web.config system.webserver errors
  • Moving Android View and preventing onDraw to be called over and over again
  • Blackberry - Custom EditField Cursor
  • Control modification in presentation layer
  • Linq Objects Group By & Sum
  • Update CALayer sublayers immediately
  • Retrieving value from sql ExecuteScalar()
  • Can Jackson SerializationFeature be overridden per field or class?
  • Redux, normalised entities and lodash merge
  • Function pointer “assignment from incompatible pointer type” only when using vararg ellipsis
  • 0x202A in filename: Why?
  • php design question - will a Helper help here?
  • Android Studio and gradle
  • KeystoneJS: Relationships in Admin UI not updating
  • AngularJs get employee from factory
  • Hits per day in Google Big Query
  • File not found error Google Drive API
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Authorize attributes not working in MVC 4
  • Checking variable from a different class in C#
  • How to get NHibernate ISession to cache entity not retrieved by primary key