0

I am looking to label the contours lines similar to this guide: https://matplotlib.org/stable/gallery/images_contours_and_fields/contour_demo.html

However, when I try to replicate it, the command clabel fails with the following error TypeError: 'QuadContourSet' object is not iterable

Heres my code. I am looking to plot multiple levels for a 2D function and label each line by its value. In a similar style found in the guide I linked above.

### plot lines of constant lamba_D, and N_D ###

# first, make a grid to calcuate the lambda_Dvalues everywhere 
ne = np.logspace(6,25,1000,base=10) ## makes array of numbers spaced evenly in log
kT = np.logspace(-2,5,1000,base=10) 
kTGrid, neGrid = np.meshgrid(kT,ne)

lambda_D = np.sqrt( eps*kTGrid*eTJ / (neGrid * e**2) ) ## Eq(1.4) Boyd & Sanderson

lambda_Dlevels = [1e-6,1e-4,1e-2,1e0,1e2,1e4]

c2 = ax.contour(kTGrid,neGrid,lambda_D, levels=lambda_Dlevels, colors='black')
c2.clabel(c2,inline=True)
Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
patrick7
  • 366
  • 1
  • 11

1 Answers1

2

You didn't share enough of your code that I can run it myself, but I believe the issue is because you did c2.clabel(c2, inline=True) instead of ax.clabel(c2, inline=True).

jared
  • 4,165
  • 1
  • 8
  • 31
  • Wow, my apologies, such a silly mistake on my end. Thanks for your time and help! – patrick7 Aug 18 '23 at 15:24
  • Issues caused by a **typo** should be called out in comments, not an answer, and flagged to **Not reproducible or was caused by typos**. Reference [Should you answer questions where the cause of the problem is a typo?](https://meta.stackoverflow.com/a/366141/7758804) & [this meta answer](https://meta.stackexchange.com/a/123743/596550). These questions are downvoted, closed and deleted, as they are *to localized*, and of no benefit to the community. – Trenton McKinney Aug 18 '23 at 17:05
  • While I typically would only respond with a comment (and flag to close), I thought it would be beneficial to properly answer this since I didn't see any questions with a similar error message and I believe other people might make a similar mistake. – jared Aug 18 '23 at 17:25
  • There are **hundreds** of examples on SO showing the correct way to access an axes level method. There's also the [matplotlib Contour Label Demo](https://matplotlib.org/stable/gallery/images_contours_and_fields/contour_label_demo.html) and [`matplotlib.axes.Axes.clabel`](https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.clabel.html#matplotlib.axes.Axes.clabel) – Trenton McKinney Aug 18 '23 at 17:37