Numpy Session 5 Code

Numy Session 5 Code For Video Click Fahad Hussain CS


import numpy as np 

  

x_axis = np.linspace(-4, 4, 9) 

y_axis = np.linspace(-5, 5, 11) 

print(x_axis)

print(y_axis)

x_axis_1, y_axis_1 = np.meshgrid(x_axis, y_axis) 

  

print("x_axis_1 = ") 

print(x_axis_1) 

print("y_axis_1 = ") 

print(y_axis_1) 


import numpy as np  

na, nb = (5, 3)  

a = np.linspace(1, 2, na)  

b = np.linspace(1, 2, nb)  

xa, xb = np.meshgrid(a, b)  

print(xa)  

print(xb) 


import numpy as np  

na, nb = (5, 3)  

a = np.linspace(1, 2, na)  

b = np.linspace(1, 2, nb)  

xa, xb = np.meshgrid(a, b, sparse=True)  

print(xa)  

print(xb)  


import matplotlib.pyplot as plt

xx, yy = np.meshgrid(x_axis, y_axis, sparse=True)

ellipse = xx * 2 + 4 * yy**2

plt.contourf(x_1, y_1, ellipse, cmap = 'jet') 

plt.colorbar() 

plt.show() 


import numpy as np

import matplotlib.pyplot as plt

import numpy as np

a = np.arange(-4, 4, 0.1)

b = np.arange(-4, 4, 0.1)

#aa, bb = np.meshgrid(a, b, sparse=True)

c = np.sin(aa**2) / (a 2)

m = plt.contourf(a,b,c)

plt.show()



random_data = np.random.random((11, 9))

print(random_data) 

plt.contourf(x_1, y_1, random_data, cmap = 'jet') 

plt.colorbar() 

plt.show() 



sine = (np.sin(x_1**2 + y_1**2))/(x_1**2 + y_1**2) 

plt.contourf(x_1, y_1, sine, cmap = 'jet') 


plt.colorbar() 

plt.show() 



# Sample code for generation of Matrix indexing 

import numpy as np 



x = np.linspace(-4, 4, 9) 

# numpy.linspace creates an array 

# of 9 linearly placed elements between 

# -4 and 4, both inclusive 

y = np.linspace(-5, 5, 11) 


# The meshgrid function returns 

# two 2-dimensional arrays 

x_1, y_1 = np.meshgrid(x, y) 



x_2, y_2 = np.meshgrid(x, y, indexing = 'ij') 


# The following 2 lines check if x_2 and y_2 are the 

# transposes of x_1 and y_1 respectively 

print("x_2 = ") 

print(x_2) 

print("y_2 = ") 

print(y_2) 


# np.all is Boolean and operator; 

# returns true if all holds true. 

print(np.all(x_2 == x_1.T)) 

print(np.all(y_2 == y_1.T)) 



import numpy as np  

import matplotlib.pyplot as plt  

a = np.arange(-10, 10, 0.1)  

b = np.arange(-10, 10, 0.1)  

xa, xb = np.meshgrid(a, b, sparse=True)  

z = np.sin(xa**2 + xb**2) / (xa**2 + xb**2)  

h = plt.contourf(a,b,z)  

plt.show()  


import numpy as np  

import matplotlib.pyplot as plt  

a = np.linspace(-5, 5, 5)  

b = np.linspace(-5, 5, 11)  

random_data = np.random.random((11, 5))  

xa, xb = np.meshgrid(a, b)  

plt.contourf(xa, xb, random_data, cmap = 'jet')  

plt.colorbar()  

plt.show()  


import numpy as np  

import matplotlib.pyplot as plt  

a = np.linspace(-5, 5, 5)  

b = np.linspace(-5, 5, 11)  

random_data = np.random.random((11, 5))  

xa, xb = np.meshgrid(a, b)  

sine = (np.sin(xa**2 + xb**2))/(xa**2 + xb**2)  

plt.contourf(xa, xb, sine, cmap = 'jet')  

plt.colorbar()  

plt.show()  


from pylab import *

from mpl_toolkits.mplot3d import Axes3D


fig = figure()

ax = Axes3D(fig)

X = np.arange(-4, 4, 0.25)

Y = np.arange(-4, 4, 0.25)

X, Y = np.meshgrid(X, Y)

R = np.sqrt(X**2 + Y**2)

Z = np.sin(R)

ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap='hot')

show()


No comments:

Post a Comment

Fell free to write your query in comment. Your Comments will be fully encouraged.