Experiment 10
Experiment 10
10.1 Aim
a) Plot graphs for chisquare distribution using matplotlib for input x as 100 values from
0 to 10 (at equal intervals) and various degrees of freedom (1, 3, 8, 4)
b) Import required functions to create a 3D animated plot with sine and cosine values
(use numpy) in X and Y axis. Construct a user defined function to give the plot
specifications.
10.2 Background
Animations
Artist Animation uses a fixed set of Artist objects. Func Animation class allows you
to make an animation by repeatedly calling a function and saving the output as a frame in the
animation.
10.3 Software Used
1. Write a python program to obtain a line plot of sine function using matplotlib
2. Write a python program to obtain a pie plot for male and female population in percentage.
10.5 Procedure
1. After Installing anaconda navigator, open the anaconda navigator and then select Jupyter
Notebook and click on ‘Launch’
4. To view the output, click on ‘Run’or press ‘Shift+Enter’ to execute the program of selected
cell. In case of error, refer to the error message and do the required changes to get desired
output
10.6 Program
a) #chisquare distribution
from scipy import stats
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 10, 100)
fig,ax = plt.subplots(1,1)
linestyles = [':', '--', '-.', '-']
deg_of_freedom = [1, 3, 8, 4]
for df, ls in zip(deg_of_freedom, linestyles):
ax.plot(x, stats.chi2.pdf(x, df), linestyle=ls)
plt.xlim(0, 10)
plt.ylim(0, 0.5)
plt.xlabel('Value')
plt.ylabel('Frequency')
plt.title('Chi-Square Distribution')
b) import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import animation
# Time Array
t = np.linspace(0, 20, 100)
# Position Arrays
x = np.sin(np.pi/2 * t)
y = np.cos(np.pi/2 * t)
z = np.linspace(0, 100, 100)
# Setting up Data Set for Animation
dataSet = np.array([x, y, z]) # Combining our position coordinates
numDataPoints = len(t)
def animate_func(num):
ax.clear() # Clears the figure to update the line, point,title, and axes
# Updating Trajectory Line (num+1 due to Python indexing)
ax.plot3D(dataSet[0, :num+1], dataSet[1, :num+1],dataSet[2, :num+1], c='blue')
# Updating Point Location
ax.scatter(dataSet[0, num], dataSet[1, num], dataSet[2, num],c='red', marker='o')
# Adding Constant Origin
ax.plot3D(dataSet[0, 0], dataSet[1, 0], dataSet[2, 0],c='black', marker='o')
# Setting Axes Limits
ax.set_xlim3d([-1, 1])
ax.set_ylim3d([-1, 1])
ax.set_zlim3d([0, 100])
# Adding Figure Labels
ax.set_title('Trajectory \nTime = ' + str(np.round(t[num],decimals=2)) + ' sec')
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
# Plotting the Animation
%matplotlib notebook
fig = plt.figure()
ax = plt.axes(projection='3d')
line_ani = animation.FuncAnimation(fig, animate_func, interval=100,frames=numDataPoints)
10.7 Post lab question
1) Write a simple python code to obtain the given plot using add_axes in matplotlib
10.8 Output
10.9 Result