python2dcontour

How do you plot a contour plot of energy gain in for changing epsilon on python?


I've been trying to make a contour plot for varying epsilon_1 and epsilon_2 having energy gain as the contour part but I'm not sure how to code it as I keep getting errors when I try anything with

epsilon_1= np.linspace(1,25,1000)
epsilon_2= np.linspace(1,25,1000)
E=1/2*((epsilon_1)/(epsilon_2)+1)

can anyone tell me how to code this properly to get a contour plot of this please?

I tried plt.contour and plt.tricontourf but they didnt work because my E isnt 2D.


Solution

  • You use numpy.meshgrid to turn them into 2d.

    You can use contourf instead of contour if you want filled, rather than line, contours.

    import numpy as np
    import matplotlib.pyplot as plt
    
    epsilon_1= np.linspace(1,25,1000)
    epsilon_2= np.linspace(1,25,1000)
    
    eps1, eps2 = np.meshgrid( epsilon_1, epsilon_2 )
    E=0.5 * ( eps1 / eps2 + 1 )
    
    plt.contour( eps1, eps2, E, 100 )
    plt.show()
    

    enter image description here