In [1]:
####################
#Plotting Example
#Ben Trabing
#September 20, 2018
####################
#
#First import useful packages
import numpy as np                        #Usefull for data structures and array manipulation
from matplotlib import pyplot as plt      #Plotting functions
In [2]:
#Lets create some fake data

x = np.arange(-100,102,2) #Creates an array of values from -100 to 100.
y = np.arange(-50,51) #If you leave out the last number numpy assumes 1.

#Now lets make a function
def crazy_curve(X,Y):
    Z = (X+30.)**2 + (Y-10.)**2    
    return Z
In [3]:
#Lets plot our data

#First we need a grid to plot the data, we will use the x,y grid
x_2d, y_2d = np.meshgrid(x,y)

#Now lets apply the function to the grid
z = crazy_curve(x_2d,y_2d)

##########################################
fig = plt.figure(figsize=(8,8)) #Create a figure
ax = plt.subplot(111)    #create subplot

#Note that the grid and values have the same shape
print(x_2d.shape,y_2d.shape,z.shape)


im = ax.contourf(x,y,z,cmap=plt.cm.jet) #make the filled contour plot
#You can call additonal arguments like contour levels (clev=np.arange(0,100,10) etc.)


#Create a colorbar using
plt.colorbar(im)



#You Can manipulate the plot using the following 
ax.set_xlim(-100,100) #Set limits for x axis
ax.set_ylim(-100,100) #Set limits for y axis
ax.set_xlabel('x',fontsize=12)
ax.set_ylabel('y',fontsize=12)
ax.set_title('z',fontsize=12)




plt.show()   #Show the plot
((101, 101), (101, 101), (101, 101))
In [4]:
#Lets plot our data

#First we need a grid to plot the data, we will use the x,y grid
x_2d, y_2d = np.meshgrid(x,y)

#Now lets apply the function to the grid
z = crazy_curve(x_2d,y_2d)

##########################################
fig = plt.figure(figsize=(8,8)) #Create a figure
ax = plt.subplot(111)    #creates 1 subplot


clevs = np.arange(0,1.1,.1)
im = ax.contour(x,y,z/np.max(z),clev=clevs,colors='k',linewidths=7) #make a normal contour plot
#You can call additonal arguments like contour levels (clev=np.arange(0,100,10),colormap, etc.)


#Create a colorbar using
plt.clabel(im)



#You Can manipulate the plot using the following 
ax.set_xlim(-100,100) #Set limits for x axis
ax.set_ylim(-100,100) #Set limits for y axis
ax.set_xlabel('x',fontsize=12) #set labels and fontsize
ax.set_ylabel('y',fontsize=12)
ax.set_title('z',fontsize=12)




plt.show()   #Show the plot