How to add more levels to a level curve graph?

Viewed 95

I have the following lines of code to make a level curve graph but I want to add more lines/curves to the graph:

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(-1,1,500)
y = np.linspace(-1,1,500)
X,Y = np.meshgrid(x,y)
b = -(2*X**2 + Y**4)
Z = X*Y*math.e**b

fig = plt.figure(figsize=(5,5))
ax = fig.add_subplot(111,aspect='equal') 
ax.contour(X,Y,Z)
plt.show()

I'm trying to add more lines to the graph that this code produces but when I try to add something into the contour part like ax.contour(X,Y,Z,levels=range(0,40)), the graph disappears except for a few lines down the middle. How do I make it so there are more line on the graph than there otherwise would be without the levels=range(0,40) part?

1 Answers

When you do levels=range(40), it plots the curves at z=0,1,...,40, which is not really what you're after.

Try with levels=40:

fig = plt.figure(figsize=(5,5))
ax = fig.add_subplot(111,aspect='equal') 
ax.contour(X,Y,Z,levels=40)
plt.show()

Output:

enter image description here

Related