OBJECTIVE



LAB # 08Scipy and matplotlibOBJECTIVEScientific computing?and technical computing with plotting using SciPy and matplotlib library in python.THEORYIntroduction:SciPy:SciPy is a library of algorithms and mathematical tools built to work with NumPy arrays.SciPy, pronounced as Sigh Pi, is a scientific python open source library to perform Mathematical, Scientific and Engineering Computations.The SciPy library depends on NumPy, which provides convenient and fast N-dimensional array manipulation. The SciPy library is built to work with NumPy arrays and provides many user-friendly and efficient numerical practices such as routines for numerical integration and optimization. Together, they run on all popular operating systems, are quick to install and are free of charge. NumPy and SciPy are easy to use, but powerful enough to depend on by some of the world's leading scientists and engineers.Importing the SciPy module:There are several ways to import SciPy. The standard approach is to use a simple import statement: import scipy However, for large amounts of calls to SciPy functions, it can become tedious to write numpy.X over and over again. Instead, it is common to import under the briefer name sp: import scipy as sp This statement will allow us to access SciPy objects using sp. X instead of scipy X. It is also possible to import SciPy directly into the current namespace so that we don't have to use dot notation at all, but rather simply call the functions as if they were built-in: from scipy import * if you want help regarding SciPy, or anything need to know about SciPy in python’s editor , you may type:help(scipy)help(scipy.zeros) NumPy v/s SciPy:In an ideal world, NumPy would contain nothing but the array data type and the most basic operations: indexing, sorting, reshaping, basic element wise functions et cetera. All numerical code would reside in SciPy. However, one of NumPy’s important goals is compatibility, so NumPy tries to retain all features supported by either of its predecessors. Thus NumPy contains some linear algebra functions, even though these more properly belong in SciPy. In any case, SciPy contains more fully-featured versions of the linear algebra modules, as well as many other numerical algorithms. If you are doing scientific computing with python, you should probably install both NumPy and SciPy. Most new features belong in SciPy rather than NumPy.Following example we already done in Numpy but SciPy also done the same function:Example: Output:lefttop Other Following example using Scipy.io package provides a wide range of functions to work around with different format of files. Some of these formats are – Matlab, IDL, Matrix Market, Wave, Arff, Netcdf, etc.Following are the functions used to load and save a .mat file. This form of importing submodules is preferred for all submodules except?scipy.io?(because?io?is also the name of a module in the Python stdlib)Example:Output:Loadmat--Loads a MATLAB fileSavemat--Saves a MATLAB fileWhosmat--Lists variables inside a MATLAB fileSciPy is built using the optimized?ATLAS LAPACK?and?BLAS?libraries. It has very fast linear algebra capabilities. All of these linear algebra routines expect an object that can be converted into a two-dimensional array. The output of these routines is also a two-dimensional array.SciPy.linalg v/s NumPy.linalg:A scipy.linalg contains all the functions that are in numpy.linalg. Additionally, scipy.linalg also has some other advanced functions that are not in numpy.linalg. Another advantage of using scipy.linalg over numpy.linalg is that it is always compiled with BLAS/LAPACK support, while for NumPy this is optional. Therefore, the SciPy version might be faster depending on how NumPy was installed.The?scipy.linalg.solve?feature solves the linear equation a * x + b * y = Z, for the unknown x, y values.Example: Output:lefttop SciPy – Stats:All of the statistics functions are located in the sub-package?scipy.stats?and a fairly complete listing of these functions can be obtained using?info(stats)function. A list of random variables available can also be obtained from the?docstring?for the stats sub-package. This module contains a large number of probability distributions as well as a growing lib rary of statistical functions. rv_continuous--A generic continuous random variable class meant for subclassingrv_discrete--A generic discrete random variable class meant for subclassingrv_histogram--Generates a distribution given by a histogramThe?scipy.optimize package?provides several commonly used optimization algorithms and so many other build-in package provides many different functionalities.Plotting and Graphics:Matplotlib:Matplotlib?is probably the single most used Python package for 2D-graphics. It provides both a very quick way to visualize data from Python and publication-quality figures in many formats. We are going to explore matplotlib in interactive mode covering most common cases.You can generate plots, histograms, power spectra, bar charts, errorcharts, scatterplots, etc., with just a few lines of code.Importing the Matplotlib module:There are several ways to import matplotlib. The standard approach is to use a simple import statement: import matplotlibpyplot:pyplot?provides a procedural interface to the matplotlib object-oriented plotting library. It is modeled closely after Matlab?. Therefore, the majority of plotting commands in pyplot have Matlab? analogs with similar arguments. Important commands are explained with interactive examples.from matplotlitb import pyplot as plt1D plotting:Example:numpy.linspace ( ), numpy.linspace (start, stop, num = 50, endpoint = True, retstep = False, dtype = None) :?Returns number spaces evenly w.r.t interval. Similiar to? HYPERLINK "()%20in%20Python" arange?but instead of step it uses sample number.?plt.plot ( ),?is a versatile command, and will take an arbitrary number of arguments. For example, to plot x versus y.plt.show ( ), display the current figure that you are working on.Output: Plotting more than one line:Example:Output:EXERCISEUsing multiple Plotting example, change the colour of line using plt.gca( ) and adding legend to the plot using Matplotlib legend( ). Output looks alike :Plot function with?Matplotlib. First, import?SciPy?and?Matplotlib, some points on the (0, 1) interval and then plot a parabola defined by the above interval.Plot sine and cosine function using Numpy and Matplotlib library.Plot square and cube function using Scipy and Matplotlib library with linspace interval (0,5). Output looks alike: ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download