11 Python Programming and Numerical Methods.pptx

Full Transcript

Python Programming and Numerical Methods Basic plotting function: plot(x,y) Basic Plotting Function ◦ Function: plot(x,y) ◦ Takes in two lists/arrays: x and y ◦ Produces a visual display of points Resources ◦ Matplotlib Gallery: https://matplotlib.org/gallery/i ndex.html#gallery ◦...

Python Programming and Numerical Methods Basic plotting function: plot(x,y) Basic Plotting Function ◦ Function: plot(x,y) ◦ Takes in two lists/arrays: x and y ◦ Produces a visual display of points Resources ◦ Matplotlib Gallery: https://matplotlib.org/gallery/i ndex.html#gallery ◦ Python Programming and Numerical Methods: https://doi.org/10.1016/B978- 0-12-819549-9.00021-X Plotting with interactive features Default Plot Function Behavior ◦ Connects each point with a blue line ◦ Use finer discretization points for a smoother function Plotting and Displaying the Figure ◦ plt.plot function plots the figure ◦ plt.show() displays the figure Interactive Plot Features ◦ Buttons allow moving the line, zooming, or saving the figure ◦ Turn off interactive plot before plotting a new figure ◦ Use %matplotlib inline to disable interactive features Symbol Description Symbol Description Changing b blue T T markers g green s square and lines r c red cyan d v diamond triangle (down) m magenta ˆ triangle (up) Adding a Third Input Argument ◦ Specifies color and line style y yellow < triangle (left) ◦ Example: plot(x,y,"ro") k black > triangle (right) Possible Specifications ◦ Refer to the table for options w white p pentagram ◦ See also Fig. 12.3. point h hexagram Plotting Multiple Datasets o circle - solid ◦ Add datasets before plt.show() x x-mark : dotted ◦ See also Fig. 12.4 + plus -. dashed–dotted * star - dashed Adding titles and axis labels Adding Titles and Labels ◦ Use the title function to add a title to your plot ◦ Use xlabel and ylabel functions to name your axis labels Changing Figure Size ◦ Create a figure object and resize it ◦ Use plt.figure function to create a new figure object Customizing Axis Label Size ◦ Specify a fontsize argument in the plt.xlabel function Predefined Styles ◦ Automatically change the style using predefined styles Using predefined styles Seaborn Style ◦ One of the favorite predefined styles ◦ Can be changed using plt.style.use Creating legends Using the legend function ◦ Add the label argument in the plot function Loc argument in legend function ◦ Indicates where to put the legend ◦ Change loc value from 0 to 10 to see the effect ◦ Refer to Fig. 12.7 for visual representation Customizing plot appearance Change Axis Limits ◦ Use xlim function for x-axis ◦ Use ylim function for y-axis Enable Grid ◦ Use grid function to turn on grid ◦ Grid applied to Fig. 12.8 Use the subplot function Creating a Table of Plots Function takes three inputs Number of rows Creating of plots subplots Inputs for Subplot Number of columns of Function plots Designating plot location Switching Call subplot Between again with Subplots different entry Scatter, bar, and log plots Bar Plot ◦ Plots bars centered at x with height y Loglog Plot ◦ Plots data with both axes on a log- scale Semilogx Plot ◦ Plots data with x axis on a log-scale and y axis on a linear scale Semilogy Plot ◦ Plots data with y axis on a log-scale and x axis on a linear scale Scatter Plot ◦ Works like plot but defaults to red circles Saving figures Saving Figures in Specific Formats ◦ Use plt.savefig to save figures in formats like pdf, jpeg, png, etc. Plotting Data with Error Bars ◦ Use errorbar function to plot x versus y data with error bars for each element. Polar Plots ◦ Use polar function to plot θ versus r instead of x versus y. Stem Plots ◦ Use stem function to plot stems at x with height at y. Histograms Boxplots Pie Charts Exploring More Functions Creating 3D axes Import mplot3d toolkit ◦ Adds 3D plotting capabilities to matplotlib Create 3D axes ◦ Use ax = plt.axes(projection="3d") Add data to 3D axes ◦ Use plot3D function Customize plot ◦ Change title and set x, y, z labels ◦ Set labelpad=20 to avoid overlap with tick texts Rotate figure ◦ Get a 3D view of the plot 3D surface plots Creating a Mesh for 3D Surface Plotting ◦ Mesh is a listing of all possible combinations of x and y ◦ Created using np.meshgrid function in Python Plotting 3D Surfaces ◦ Function: plot_surface(X,Y,Z) ◦ X and Y are output arrays from meshgrid ◦ Z=f(X,Y) or Z[i,j]=f(X[i,j],Y[i,j]) Color Schemes in Surface Plots ◦ Different colors for different elevations ◦ Colormap plt.cm.cividis used for example ◦ Colorbar shows corresponding colors to values Subplots of 3D plots Subplots of Different 3D Plots ◦ Use the add_subplot function from the figure object ◦ Generate subplots for 3D cases Exploring More Plotting Functions ◦ Many more functions related to plotting in Python ◦ This list is not exhaustive ◦ Provides enough background to get started Additional Resources ◦ mplot3d tutorial website for more examples Plotting 3D scatter plots 3D Scatter Plot ◦ Created using the scatter function ◦ Visual representation of data in three dimensions Reference ◦ See Fig. 12.12 for an example Importance of Maps in Engineering and Science Used for geographical representation of data Various Python packages available for drawing maps Popular Python Packages for Drawing Maps Basemap Basics of Cartopy Folium map plotting Introduction to Cartopy Used for drawing static maps Install using conda install cartopy Basics of a Map 2D plot with specific projections Using cartopy for map plotting Cartopy package API ◦ Interacts with matplotlib to plot maps ◦ Requires specifying a map projection Example of Plate Carree projection ◦ Grid lines turned on ◦ Labels drawn on the map Other cartopy supported projections ◦ Explore various projections available Adding map background s Map Background in Cartopy ◦ Blank map background initially ◦ Use stock_img to add a nice map background ◦ Refer to Fig. 12.16 for visual representation Additional Resources ◦ Visit the Cartopy projections documentation ◦ Link: https://scitools.org.uk/cartopy/ docs/v0.16/crs/projections.ht ml#cartopy-projections Zooming in on maps Using ax.set_extent function ◦ Takes a list of four numbers ◦ First two numbers are x- axis limits ◦ Last two numbers are y- axis limits Zooming in on any place on Earth ◦ Adjust the x and y limits accordingly ◦ Refer to Fig. 12.17 for an example Adding features to maps Specifying Features in Cartopy ◦ Country boundary ◦ State boundary ◦ Lakes and water bodies Zooming in for Detailed Maps ◦ Download high-resolution coastlines ◦ Use high-resolution land data Plotting data on maps Plotting data on maps ◦ Shows spatial location of different entities ◦ Data can be added similarly to normal matplotlib axes Coordinate system ◦ Default coordinate system is same as axes defined at beginning Example: Adding data to map ◦ Plotting Berkeley and Stanford as blue dots ◦ Adding labels for the universities Versatility of cartopy package ◦ Official example available online ◦ Explore to augment map-making abilities Introduction to animations Definition of Animation ◦ Sequence of still frames displayed in succession ◦ Creates the illusion of continuous motion Advantages of Animations ◦ Conveys information better than individual plots Creating Animations in Python ◦ Use a plot function inside a loop ◦ Commonly use a for-loop Main Tool for Animations ◦ matplotlib.animation.Animation base class ◦ Provides a framework for animation functionality Steps to create a movie Pre-Movie Steps ◦ Define the meta data for the movie ◦ Decide what in the background does not need changing ◦ Decide which objects need changing in each movie frame Steps to Make the Movie ◦ Define the meta data for the movie ◦ Initialize the movie background figure ◦ Update the frames for the movie Examples of movie creation Defining the meta data for the movie Defining Establishing key information about the movie Initializing the movie background figure Initializing Setting up the visual elements Updating the frames for the movie Updating Modifying the sequence of images

Use Quizgecko on...
Browser
Browser