Understanding Python Libraries for Data Analysis in Geophysics

c e r i n.w
1 / 33
Embed
Share

Explore the essential libraries for scientific number crunching and plotting in Python, including SciPy, NumPy, and matplotlib. Learn how to import and utilize these libraries for data analysis in geophysics using Python.

  • Python
  • Data Analysis
  • Geophysics
  • Libraries
  • SciPy

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. C E R I C E R I- -7 1 04/ C IV L 7 1 04/ C IV L - -8 1 2 6 D ata A nalysis in G eophysics Introduction to P Y T H O N L ab P ython 8 1 2 6 D ata A nalysis in G eophysics Introduction to P Y T H O N L ab 2 6 , 1 1 / 2 6 / 1 9 P ython 2 6 , 1 1 / 2 6 / 1 9 W e are going to continue to interactively look at and modify python code. W e are going to continue to interactively look at and modify python code. M ath, matrix, and plotting libraries M ath, matrix, and plotting libraries

  2. T he three principal libraries for scientific number crunching and plotting are T he three principal libraries for scientific number crunching and plotting are SciPy (pronounced "S igh P ie") contains modules for statistics, optimization, integration, linear algebra, F ourier transforms, signal and image processing, O D E solvers, and more. NumPy(pronounced N um P ie") is an extension to P ython, adding support for large, multi arrays and matrices, along with a large library of high these arrays. matplotlib is a plotting library for P ython and its numerical mathematics extension N umP y. (pronounced "S igh P ie") contains modules for statistics, optimization, integration, linear algebra, F ourier transforms, signal and image processing, O D E solvers, and more. (pronounced N um P ie") is an extension to P ython, adding support for large, multi- -dimensional arrays and matrices, along with a large library of high- -level mathematical functions to operate on these arrays. is a plotting library for P ython and its numerical mathematics extension N umP y. dimensional level mathematical functions to operate on

  3. T he first thing one has to do is import them. T he first thing one has to do is import them. python_stuff:544 $ python >>> import numpy >>> import scipy >>> import matplotlib

  4. N ext look at - matplotlib.pyplot a collection of command line functions that make matplotlib work like M A T L A B . E ach pyplot function makes some change to a figure: e.g., creates a figure, creates a plotting area in a figure, plots some lines in a plotting area, decorates the plot with labels, etc. matplotlib.pyplot preserves various states across function calls, so it keeps track of things like the current figure and plotting area, and the plotting functions are directed to the current axes (please note that "axes" here and in most places in the documentation refers to the axes part of a figure and not the strict mathematical term for more than one axis).

  5. Import and create alias so we dont have to call matplotlib.pyplot.method to do method import matplotlib.pyplot as plt P lot a list plt.plot([1, 2, 3, 4]) L abel it plt.ylabel('some numbers ) N ot seeing anything? Y ou need to show it. plt.show()

  6. Y ou may be wondering why the x 0-3 and the y If you provide a single list or array to matplotlib assumes it is a sequence of automatically generates x values. S ince python ranges start with vector has the same length as y but starts with H ence the x data are R eturn to the P ython prompt by closing the X window (click in the red X ). Y ou may be wondering why the x- -axis ranges from and the y- -axis from If you provide a single list or array to plot(), , matplotlib assumes it is a sequence of y values, and automatically generates x values. S ince python ranges start with 0, the default x vector has the same length as y but starts with 0.. H ence the x data are [0,1,2,3].. R eturn to the P ython prompt by closing the X - - window (click in the red X ). axis ranges from axis from 1-4.. values, and , the default x

  7. A s in M A T L A B , plot() can take an arbitrary number of arguments. F or example, to plot plt.plot([1, 2, 3, 4], [1, 4, 9, 16]) A s in M A T L A B , can take an arbitrary number of arguments. F or example, to plot x versus versus y, use: , use:

  8. M ore M A T L A B emulation. O ptional 3 argument: the format string that indicates the color and line type of the plot. S ame as M A T L A B . M ore M A T L A B emulation. O ptional 3rd rd argument: the format string that indicates the color and line type of the plot. S ame as M A T L A B . plt.plot([1, 2, 3, 4], [1, 4, 9, 16], '-o ) plt.axis([0, 5, 0, 17])

  9. D ifferent from M A T L A B emulation. A new plot() call does not clear plot. D ifferent from M A T L A B emulation. A new call does not clear plot. X=[1, 2, 3, 4] Y=[1, 4, 9, 16] plt.plot([X, Y) plt.axis([0, 5, 0, 17]) plt.plot(X,Y,'ro ) plt.grid(color='g', linestyle='.', linewidth=1)

  10. D ifferent from M A T L A B emulation. A new plot() call does not clear plot. D ifferent from M A T L A B emulation. A new call does not clear plot. X=[1, 2, 3, 4] Y=[1, 4, 9, 16] plt.plot([X, Y) plt.axis([0, 5, 0, 17]) plt.plot(X,Y,'ro ) plt.grid(color='g', linestyle='.', linewidth=1)

  11. If matplotlib were limited to working with lists, it would be fairly useless for numeric processing. If matplotlib were limited to working with lists, it would be fairly useless for numeric processing. G enerally, you will use G enerally, you will use numpy numpy arrays. arrays. In fact, all sequences are converted to In fact, all sequences are converted to numpy internally. numpy arrays arrays internally. T he next example illustrates plotting several lines with different format styles in one command using arrays. T he next example illustrates plotting several lines with different format styles in one command using arrays.

  12. D efine array (look up np.arange(start, stop, delta)) goes from In this case math operates on vectors (but not exact same way as M A T L A B , this is doing D efine array (look up numpy.arrange numpy.arrange on web, on web, ) goes from [start to to stop) in steps of In this case math operates on vectors (but not exact same way as M A T L A B , this is doing .^ ) ) in steps of delta.. import matplotlib.pyplot as plt, numpy as np # evenly sampled time at 200ms intervals t = np.arange(0., 5., 0.2) # red dashes, blue squares and green triangles plt.plot(t, t, 'r--', t, t**2, 'bs', t, t**3, 'g^') plt.show() N ote N ote t = np.arange(0., 5., 0.2) >>> t array([0. , 0.2, , 4.6, 4.8])

  13. T his is what we get T his is what we get

  14. W orking with multiple figures and axes W orking with multiple figures and axes M A T L A B , and current figure and the current axes. A ll plotting commands apply to the current axes. T he function matplotlib.axes.A xes the current figure ( (matplotlib.figure.Figure instance). ). M A T L A B , and pyplot current figure and the current axes. A ll plotting commands apply to the current axes. T he function gca() returns the current axes (a matplotlib.axes.A xes instance), and the current figure pyplot, have the concept of the , have the concept of the returns the current axes (a instance), and gcf() returns returns

  15. E xample of subplots with lots of new stuff E xample of subplots with lots of new stuff def f(t): return np.exp(-t) * np.cos(2*np.pi*t) t1 = np.arange(0.0, 5.0, 0.1) t2 = np.arange(0.0, 5.0, 0.02) plt.figure() plt.subplot(211) plt.plot(t1, f(t1), 'bo', t2, f(t2), 'k') plt.subplot(212) plt.plot(t2, np.cos(2*np.pi*t2), 'r--') plt.show()

  16. R un R un python mulplt.py

  17. W hat does this do? W hat does this do? np.exp(-t) * np.cos(2*np.pi*t) Is same as Is same as .* in M A T L A B ! in M A T L A B !

  18. S ubplots in M atplotlib S ubplots in M atplotlib planning your plots planning your plots T wo ways to make subplots T wo ways to make subplots ..add_ subplot add_ subplot() () .subplot2 grid() .subplot2 grid() F irst simpler, but it can t do everything F irst simpler, but it can t do everything

  19. def f(t): return np.exp(-t) * np.cos(2*np.pi*t) t1 = np.arange(0.0, 5.0, 0.1) t2 = np.arange(0.0, 5.0, 0.02) plt.figure() plt.subplot(211) plt.plot(t1, f(t1), 'bo', t2, f(t2), 'k') plt.subplot(212) plt.plot(t2, np.cos(2*np.pi*t2), 'r--') plt.show()

  20. def f(t): return np.exp(-t) * np.cos(2*np.pi*t) t1 = np.arange(0.0, 5.0, 0.1) t2 = np.arange(0.0, 5.0, 0.02) plt.figure() plt.subplot(211) plt.plot(t1, f(t1), 'bo', t2, f(t2), 'k') plt.subplot(212) plt.plot(t2, np.cos(2*np.pi*t2), 'r--') plt.show()

  21. def f(t): return np.exp(-t) * np.cos(2*np.pi*t) t1 = np.arange(0.0, 5.0, 0.1) t2 = np.arange(0.0, 5.0, 0.02) plt.figure() plt.subplot(211) plt.plot(t1, f(t1), 'bo', t2, f(t2), 'k') plt.subplot(212) plt.plot(t2, np.cos(2*np.pi*t2), 'r--') plt.show()

  22. def f(t): return np.exp(-t) * np.cos(2*np.pi*t) t1 = np.arange(0.0, 5.0, 0.1) t2 = np.arange(0.0, 5.0, 0.02) plt.figure() #not really needed here plt.subplot(211) plt.plot(t1, f(t1), 'bo', t2, f(t2), 'k ) plt.subplot(212) plt.plot(t2, np.cos(2*np.pi*t2), 'r--') plt.show()

  23. D efining subplot regions L ook at file T hat makes this figure. D efining subplot regions L ook at file subplotex1.py T hat makes this figure.

  24. D efining subplot regions L ook at file subplotex1.py N O T E count from 1 , not zero

  25. D efining subplot regions L ook at file subplotex1.py M akes this so far

  26. D efining subplot regions L ook at file subplotex1.py F inish off

  27. def f(t): return np.exp(-t) * np.cos(2*np.pi*t) t1 = np.arange(0.0, 5.0, 0.1) t2 = np.arange(0.0, 5.0, 0.02) plt.figure() plt.subplot(211) plt.plot(t1, f(t1), 'bo', t2, f(t2), 'k') plt.subplot(212) plt.plot(t2, np.cos(2*np.pi*t2), 'r--') plt.show()

  28. .add_subplot() has limitations. F or instance, every subplot you create can only take up one cell. T hat means something like this isn t possible with .add_subplot().. T he problem here is that the red subplot takes up 2 / 3 rds of the left U nfortunately, .add_subplot() can t handle selecting 2 / 3 rds of the figure area (2 cells). has limitations. F or instance, every subplot you create can only take up one cell. T hat means something like this isn t possible with T he problem here is that the red subplot takes up 2 / 3 rds of the left- -hand side. U nfortunately, hand side. can t handle selecting 2 / 3 rds of the figure area (2 cells).

  29. .subplot2grid()returns an axis object with the information about where the new subplot should be placed. It takes in two required positional arguments, shape, and loc. F or left-hand side N ote N ote counts from 0! counts from 0!

  30. F or right .add_subplot or In this case, one on left is cleaner and recommended) F or right- -hand side have 2 choices or .subplot2grid In this case, one on left is cleaner and recommended) hand side have 2 choices - -

  31. F or fun you could try to make this! F or fun you could try to make this!

  32. T o see your T o see your python path python path import sys >>> for path in sys.path: print(path) /Users/robertsmalley/anaconda2/lib/python27.zip /Users/robertsmalley/anaconda2/lib/python2.7 /Users/robertsmalley/anaconda2/lib/python2.7/plat-darwin /Users/robertsmalley/anaconda2/lib/python2.7/plat-mac /Users/robertsmalley/anaconda2/lib/python2.7/plat-mac/lib- scriptpackages /Users/robertsmalley/anaconda2/lib/python2.7/lib-tk /Users/robertsmalley/anaconda2/lib/python2.7/lib-old /Users/robertsmalley/anaconda2/lib/python2.7/lib-dynload /Users/robertsmalley/anaconda2/lib/python2.7/site-packages /Users/robertsmalley/anaconda2/lib/python2.7/site-packages/aeosa

More Related Content