Skip to content
Advertisement

Plotting a particular set of contour line at desired point or location

I want a contour plot showing contour levels corresponding to a particular set of x,y. I tried increasing the number of contour lines but it doesn’t give the contour line near the required point. enter image description here

I want to get a contour line to suppose around (0.1,0.1) but am not able to do so, I tried increasing the number of contours but matplotlib doesn’t plot it near the required point nor do I know the level of contour near that point.

khmax = np.arange(0,0.5,0.001)
Ncmax = np.arange(0,0.5,0.001)
[X, Y] = np.meshgrid(Ncmax,khmax)
fig, ax = plt.subplots()

contour = plt.contour(X,Y,VgN,50)
ax.set_title('magnitude of VgN/c')
ax.set_xlabel('Ncmax')
ax.set_ylabel('khmax')
ax.clabel(contour, inline= True, inline_spacing = -1,fmt = '%1.8f',fontsize=8)
plt.show()

It is not the complete code. Any kind of help or hint is highly appreciated.

Advertisement

Answer

You could use a not evenly spaced number of levels for the contour:

VgN_min = VgN.min()
VgN_max = VgN.max()
number_of_contours = 21
power = 2
levels = np.linspace(VgN_min**(1/power), VgN_max**(1/power), number_of_contours)**power

Then you can use this parameter to plot the contour:

fig, ax = plt.subplots()

contour = plt.contour(X,Y,VgN, levels = levels)
ax.set_title('magnitude of VgN/c')
ax.set_xlabel('Ncmax')
ax.set_ylabel('khmax')
ax.clabel(contour, inline= True, inline_spacing = -1,fmt = '%1.8f',fontsize=8)

plt.show()

You can tune the power value in order to change the skewness of the contour levels according to your needs:

  • power = 1

    enter image description here

  • power = 3

    enter image description here

Complete Code

import numpy as np
from matplotlib import pyplot as plt


khmax = np.arange(0,0.5,0.001)
Ncmax = np.arange(0,0.5,0.001)
[X, Y] = np.meshgrid(Ncmax,khmax)
VgN = X*Y

VgN_min = VgN.min()
VgN_max = VgN.max()
number_of_contours = 21
power = 3
levels = np.linspace(VgN_min**(1/power), VgN_max**(1/power), number_of_contours)**power


fig, ax = plt.subplots()

contour = plt.contour(X,Y,VgN, levels = levels)
ax.set_title('magnitude of VgN/c')
ax.set_xlabel('Ncmax')
ax.set_ylabel('khmax')
ax.clabel(contour, inline= True, inline_spacing = -1,fmt = '%1.8f',fontsize=8)

plt.show()

Note

In your code you didn’t reported the expression of VgN, so I supposed it to be something like VgN = X*Y in the code above and so the above images represent this equation. Change it according to your expression of VgN.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement