0% found this document useful (0 votes)
40 views

Experiment 10

The document discusses using matplotlib in Python to: 1) Plot chi-square distributions with varying degrees of freedom 2) Create an animated 3D plot of sine and cosine values over time using NumPy and matplotlib.functions. It provides background on statistical Python packages and animation capabilities in matplotlib. The procedure and sample Python code to generate the desired plots are also included.

Uploaded by

Ananya Kumari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

Experiment 10

The document discusses using matplotlib in Python to: 1) Plot chi-square distributions with varying degrees of freedom 2) Create an animated 3D plot of sine and cosine values over time using NumPy and matplotlib.functions. It provides background on statistical Python packages and animation capabilities in matplotlib. The procedure and sample Python code to generate the desired plots are also included.

Uploaded by

Ananya Kumari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

10.

Python programming using matplotlib

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

Statistical functions (scipy.stats)


This module contains a large number of probability distributions, summary and
frequency statistics, correlation functions and statistical tests, masked statistics, kernel
density estimation, quasi-Monte Carlo functionality, etc
Some of the important functions are
 statsmodels: regression, linear models, time series analysis,
 Pandas: tabular data, time series functionality, interfaces to other statistical languages.
 PyMC: Bayesian statistical modeling, probabilistic machine learning.
 scikit-learn: classification, regression, model selection.
 Seaborn: statistical data visualization.
 rpy2: Python to R bridge.

Animations

Matplotlib library of Python is a plotting tool used to plot graphs of functions or


figures. It can also be used as an animation tool too. The plotted graphs when added with
animations gives a more powerful visualization and helps the presenter to catch a larger
number of audience. Matplotlib can also easily connect with Pandas to create even more
sophisticated animations.

The easiest way to make a live animation in Matplotlib is to use one of


the Animation classes. Its main classifications are Artist Animation and Func Animation

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

Jupyter Note book

10.4 Prelab Questions

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’

2. In Jupyter Notebook click on New icon and then click on ‘python 3’

3. Type your program to get the desired output

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

Thus python programming to


a) plot graphs for chisquare distribution using matplotlib
b) create a 3D animated plot with sine and cosine values (use numpy) in X and Y axis
with user defined function for plot specifications

has been written and executed.

You might also like