RaynDrop RaynDrop - 2 months ago 13
Python Question

Missing levels in python contour plot

I am trying to plot contours with specified levels of a simulated velocity field, my values are in the range of [75,150], and I specified my levels to be

levels=[75,95,115,135,150]
,but it's only giving me the 95,115,135 lines. I checked my 2d array and I do have points with values of 75 and 150 lying on straight lines, but the plot is just not showing them. I am wondering why would that be the case. Thanks a lot!!
Here is my code:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Ellipse

#some parameters...does not matter
i=60*np.pi/180 #inclination
r=100 #radius
vc=150
vr=0

x=np.arange(-100,100,1)
y=np.arange(-100,100,1)

xx,yy=np.meshgrid(x,y)

#simulate velocity fields....does not matter either
def projv((x, y),v_c, v_r, inc):
projvy = v_c*x*np.cos(inc)/np.sqrt(x**2+y**2) + v_r*y*np.cos(inc)/np.sqrt(x**2+y**2)
projvx = v_c*y/np.sqrt(x**2+y**2) + v_r*x/np.sqrt(x**2+y**2)
v = np.sqrt(projvx**2 + projvy**2)
return v.ravel()

#here is my 2d array
vel = projv((xx,yy),vc, vr, i).reshape(200,200)

#levels I specified
levels=[75,95,115,135,150]
cs=plt.contour(x,y,vel,levels)
plt.clabel(cs,inline=1,fontsize=9)
plt.show()


Then I got this:This is not showing the 75 and 150 contour levels that I specified. Why?

Answer

You are missing the 75 and 150 contours because those values are never crossed in the array. The value 150 exists, and the value 75(.000000000000014) exist, but those are the min and max values. Contours describe a line/surface boundary.

#levels modified
levels=[76,95,115,135,149]
cs=plt.contour(x,y,vel,levels)
plt.clabel(cs,inline=1,fontsize=9)

with modified levels