Posts

Showing posts from August, 2022

Syllabus CST 445 Python For Engineers

CST445 PYTHON FOR ENGINEERS CATEGORY  L T P CREDIT      YEAR OF INTRODUCTION OEC                  2  1 0   3                     2019 Preamble: The objective of the course is to provide learners an insight into Python programming in a scientific computation context and develop programming skills to solve engineering problems. It covers programming environment, important instructions, data representations, intermediate level features, Object Oriented Programming and file data processing of Python. This course lays the foundation to scientific computing, develop web applications, Machine Learning, and Artificial Intelligence-based applications and tools, Data Science and Data Visualization applications.   Prerequisite: NIL Note : Students who have successfully completed CST 283 - Python for Machine Learning (...

More advanced graphical output

Image
 Here, we will learn a more advanced syntax that harnesses the full power of matplotlib. It gives the user more options and greater control. import numpy as np import matplotlib.pyplot as plt # Define the sinc function, with output for x=0 # defined as a special case to avoid division by zero def s(x):      a = np.where(x == 0., 1., np.sin(x)/x)      return a x=np.arange(0., 10., 0.1) y=np.exp(x) t=np.linspace(-15., 15., 150) z=s(t) # create a figure window fig = plt.figure(figsize=(9, 7)) # subplot: linear plot of exponential ax1 = fig.add_subplot(2, 2, 1) ax1.plot(x, y, 'C0') ax1.set_xlabel('time (ms)') ax1.set_ylabel('distance (mm)') ax1.set_title('exponential') #subplot: semi-log plot of exponential ax2 = fig.add_subplot(2, 2, 2) ax2.plot(x, y, 'C2') ax2.set_yscale('log') # ax2.semilogy(x, y, 'C2') # same as 2 previous lines ax2.set_xlabel('time (ms)') ax2.set_ylabel('distance (mm)') ax2.set_title('exponent...

Mathematics and Greek symbols

Image
matplotlib can display mathematical formulas, Greek letters, and mathematical symbols using a math rendering module known as mathtext. The mathtext module parses a subset of Donald Knuth’s TEX mathematical typesetting language, and provides basic mathematical typesetting without any software other than matplotlib. If, in addition, you have TEX (and/or LATEX) as a separate standalone program (such as MacTex [TexShop] or MiKTeX), then you can do even more. In what follows we will assume that you are using the native matplotlib mathtext, but will make a few comments applicable to those who have a separate installation of LATEX. matplotlib’s mathtext can display Greek letters and mathematical symbols using the syntax of TEX. If you are familiar with TEX or LATEX, you have hardly anything to learn. Even if you are not familiar with them, the syntax is simple enough that you can employ it in most cases without too much effort.You designate text as mathtext by placing dollar signs (dollar) ...

Structure of matplotlib

Image
In this section, we provide an overview of the logical structure of matplotlib. This will help you better understand the matplotlib syntax. matplotlib is a Python module for generating graphical output to your computer screen or to a computer file. Fundamentally, its job is to translate Python scripts into graphical instructions that your computer can understand. It does this using two different layers of software, the backend layer and the artist layer. To these two layers it adds a scripting layer , PyPlot, which we have met already (import matplotlib.pyplot as plt). PyPlot is a convenience layer, and not really necessary, but it facilitates rapid scripting, and aids with portability. As you shall see, for most programming we advocate using a hybrid of the scripting and artist layers. Figure below portrays the matplotlib software hierarchy. The backend layer For the sake of being concrete, we’ll start by considering the task of creating a figure on your computer screen. matplot...

Contour and Vector Field Plots

Image
  matplotlib has extensive tools for creating and annotating two dimensional contour plots and vector field plots. A contour plot is used to visualize two-dimensional scalar functions, such as the electric potential V (x;y) or elevations h(x;y) over some physical terrain. Vector field plots come in different varieties. There are field line plots, which in some contexts are called streamline plots , that show the direction of a vector field over some 2D (x;y) range. There are also quiver plots, which consist essentially of a 2D grid of arrows, that give the direction and magnitude of a vector field over some 2D (x;y) range. Making a 2D grid of points When plotting a function f (x) of a single variable, the first step is usually to create a one-dimensional x array of points, and then to evaluate and plot the function f (x) at those points, often drawing lines between the points to create a continuous curve. Similarly, when making a two dimensional plot, we usually need to make a...

Three Dimensional Plots

Image
While matplotlib is primarily a 2D plotting package, it does have basic 3D plotting capabilities. To create a 3D plot, we need to import Axes3D  from mpl_toolkits.mplot3d and then set the keyword projection to '3d' in a subplot call as follows: import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D fig = plt.figure() ax = plt.axes(projection='3d') # Data for a three-dimensional line zline = np.linspace(0, 15, 1000) xline = np.sin(zline) yline = np.cos(zline) ax.plot3D(xline, yline, zline, 'red') Three-dimensional Contour Plots Analogous to the contour plots we explored in Density and Contour Plots, mplot3d contains tools to create three-dimensional relief plots using the same inputs. Like two-dimensional ax.contour plots, ax.contour3D requires all the input data to be in the form of two-dimensional regular grids, with the Z data evaluated at each point. Here we'll show a three-dimensional contour diagram of a three-dimensional sinusoidal fu...