Beginning python visualization, 2nd edition

405 1.3K 0
Beginning python visualization, 2nd edition

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

I have always been drawn to math and computers, ever since I was a kid playing computer games on my Sinclair ZX81. When I attended university, I had a special interest in numerical analysis, a field that I feel combines math and computers ideally. During my career, I learned of MATLAB, widely popular for digital signal processing, numerical analysis, and feedback and control. MATLAB’s strong suits include a highlevel programming language, excellent graphing capabilities, and numerous packages from almost every imaginable engineering field. But I found that MATLAB wasn’t enough. I worked with very large files and needed the ability to manipulate both text and data. So I combined Perl, AWK, and Bash scripts to write programs that automate data analysis and visualization. And along the way, I’ve developed practices and ideas involving the organization of data, such as ways to ensure file names are unique and selfexplanatory.

www.it-ebooks.info For your convenience Apress has placed some of the front matter material after the index Please use the Bookmarks and Contents at a Glance links to access them www.it-ebooks.info Contents at a Glance About the Author��������������������������������������������������������������������������������������������������������������� xix About the Technical Reviewer������������������������������������������������������������������������������������������� xxi Acknowledgments����������������������������������������������������������������������������������������������������������� xxiii Introduction���������������������������������������������������������������������������������������������������������������������� xxv ■■Chapter 1: Navigating the World of Data Visualization������������������������������������������������������1 ■■Chapter 2: The Environment��������������������������������������������������������������������������������������������31 ■■Chapter 3: Python for Programmers�������������������������������������������������������������������������������55 ■■Chapter 4: Data Organization ���������������������������������������������������������������������������������������109 ■■Chapter 5: Processing Text Files ����������������������������������������������������������������������������������141 ■■Chapter 6: Graphs and Plots������������������������������������������������������������������������������������������189 ■■Chapter 7: Math Games�������������������������������������������������������������������������������������������������233 ■■Chapter 8: Science and Visualization����������������������������������������������������������������������������269 ■■Chapter 9: Image Processing����������������������������������������������������������������������������������������307 ■■Chapter 10: Advanced File Processing��������������������������������������������������������������������������343 ■■Appendix: Additional Source Listing�����������������������������������������������������������������������������371 Index���������������������������������������������������������������������������������������������������������������������������������379 v www.it-ebooks.info Introduction I have always been drawn to math and computers, ever since I was a kid playing computer games on my Sinclair ZX81 When I attended university, I had a special interest in numerical analysis, a field that I feel combines math and computers ideally During my career, I learned of MATLAB, widely popular for digital signal processing, numerical analysis, and feedback and control MATLAB’s strong suits include a high-level programming language, excellent graphing capabilities, and numerous packages from almost every imaginable engineering field But I found that MATLAB wasn’t enough I worked with very large files and needed the ability to manipulate both text and data So I combined Perl, AWK, and Bash scripts to write programs that automate data analysis and visualization And along the way, I’ve developed practices and ideas involving the organization of data, such as ways to ensure file names are unique and self-explanatory With the increasing popularity of the Internet, I learned about GNU/Linux and the open source movement I’ve made an effort to use open source software whenever possible, and so I’ve learned of GNU-Octave and gnuplot, which together provide excellent scientific computing functionality That fit well on my Linux machine: Bash scripts, Perl and AWK, GNU-Octave, and gnuplot Knowing I was interested in programming languages and open source software, a friend suggested I give Python a try My first impression was that it was just another programming language: I could almost anything I needed with Perl and Bash, resorting to C/C++ if things got hairy And I’d still need GNU-Octave and gnuplot, so what was the advantage? Eventually, I did learn Python and discovered that it is far better than my previous collection of tools Python provides something that is extremely appealing: it’s a one-stop shop—you can it all in Python I’ve shared my enthusiasm with friends and colleagues Many who expressed interest with the ideas of data processing and visualization would ask, “Can you recommend a book that teaches the ideas you’re preaching?” And I would tell them, “Of course, numerous books cover this subject! But they didn’t want numerous books, just one, with information distilled to focus on data analysis and visualization I realized there wasn’t such a title, and this was how the idea for this book originated What’s New in the Second Edition Aside from using the most up-to-date version of Python that supports all the visualization packages (version 3.3 at the time of the writing the second edition), I’ve also introduced the following additional content: • 3-D plots and graphs • Non-rectangular contour plots • Matplotlib’s basemap toolkit • Reading and writing MATLAB binary files • Reading and writing data to NumPy arrays • Reading and writing images to NumPy arrays • Making movies • IPython, IPython Notebook, and Spyder development environments xxv www.it-ebooks.info ■ Introduction Who This Book Is For Although this book is about software, the target audience is not necessarily programmers or computer scientists I’ve assumed the reader’s main line of work is research or R&D, in his or her field of interest, be it astrophysics, signal and image processing, or biology The audience includes the following: • Graduate and PhD students in exact and natural sciences (physics, biology, and chemistry) working on their thesis, dealing with large experimental data sets The book also appeals to students working on purely theoretical projects, as they require simulations and means to analyze the results • R&D engineers in the fields of electrical engineering (EE), mechanical engineering, and chemical engineering: engineers working with large sets of data from multiple sources In EE more specifically, signal processing engineers, communication engineers, and systems engineers will find the book appealing • Programmers and computer enthusiasts, unfamiliar with Python and the GNU/Linux world, but who are willing to dive into a new world of tools • Hobbyist astronomers and other hobbyists who deal with data and are interested in using Python to support their hobby The book can be appealing to these groups for different reasons For scientists and engineers, the book provides the means to be more productive in their work, without investing a considerable amount of time learning new tools and programs that constantly change For programmers and computer enthusiasts, the book can serve as an appetizer, opening up their world to Python And because of the unique approach presented here, they might share the enthusiasm the author has for this wonderful software world Perhaps it will even entice them to be part of the large and growing open source community, sharing their own code It is assumed that the reader does have minimal proficiency with a computer, namely that he or she must know how to manipulate files, install applications, view and edit files, and use applications to generate reports and presentations A background in numerical analysis, signal processing, and image processing, as well as programming, is also helpful, but not required This book is not intended to serve as an encyclopedia of programming in Python and the covered packages Rather, it is meant to serve as an introduction to data analysis and visualization in Python, and it covers most of the topics associated with that field How This Book Is Structured The book is designed so that you can easily skip back and forth as you engage various topics Chapter is a case study that introduces the topics discussed throughout the book: data analysis, data management, and, of course, data visualization The case study involves reading GPS data, analyzing it, and plotting it along with relevant annotations (direction of travel, speed, etc.) A fully functional Python script will be built from the ground up, complemented with lots of explanations The fruit of our work will be an eye-catching GPS route If you’re new to data analysis and visualization, consider reading Chapter first The chapter describes how to set up a development environment to perform the tasks associated with data analysis and visualization in Python, including the selection of an OS, installing Python, and installing third-party packages If you’re new to Python, your next stop should be Chapter In this chapter, I swiftly discuss the Python programming language I won’t be overly rehashing basic programming paradigms; instead I’ll provide a quick overview of the building blocks for the Python programming Regardless of your Python programming experience, I highly encourage you to read Chapter before proceeding to the next chapters Organization is the key to successful data analysis and visualization This chapter covers organizing data files, pros and cons of different file formats, file naming conventions, finding data files, and automating file creation The ideas in Chapter are used throughout the book xxvi www.it-ebooks.info ■ Introduction From there on out, you have several options If you intend to process text and data files, proceed to Chapter Chapter covers text files from all aspects: I/O operations, string processing, the csv module, regular expressions, and localization and internationalization If Chapter leaves you wanting to know more about file processing, proceed to Chapter 10 Chapter 10 includes advanced file processing topics: binary files, command-line arguments, file and directory manipulation, and more Both Chapters and 10 are augmented with numerous examples If graphs and plots are your heart’s desire, skip directly to Chapter In Chapter I examine matplotlib and explore its capabilities If you’re interested in the numerical aspects of data, it is advised you read Chapter first Chapter discusses the basic building blocks for scientific computing Chapter builds on Chapter and includes more advanced topics such as numerical analysis and signal processing Image processing is an important aspect of data processing Chapter deals with tools available as part of the Python Imaging Library (Pillow) package and shows how to further expand the package and perform more complex image processing tasks Chapter 10 includes advanced file processing topics including binary files and random access, object serialization, command line parameters, file compression and more Finally, the Appendix provides additional source code listings used in the book Downloading the Code The source code for this book is available to readers at www.apress.com in the Downloads section of this book’s home page Please feel free to visit the Apress web site and download all the code there You can also check for errata and find related titles from Apress Contacting the Author You can contact me at shai.vaingast@gmail.com xxvii www.it-ebooks.info Chapter Navigating the World of Data Visualization A Case Study As an engineer, I work with data all the time I parse log files, analyze data, estimate values, and compare the results with theory Things don’t always add up So I double-check my analysis, perform more calculations, or run simulations to understand the results better I refer to previous work because the ideas are similar or sometimes because they’re dissimilar I look at the graphs and realize I’m missing some crucial information So I add the missing data, but it’s noisy and needs filtering Eventually, I realize my implementation of the algorithm is poor or that there is a better algorithm with better results, and so it’s back to square one It’s an iterative process: tweak, test, and tweak again until I’m satisfied with the results Those are the tasks surrounding research and development (R&D) work And to be honest, there’s no systematic method Most of the time, research is organized chaos The emphasis, however, should be on “organized”, not “chaos” Data should be analyzed and presented in a clear and coherent manner Sources for graphs should be well understood and verified to be accurate Algorithms should be tested and proven to be working as intended The system should be flexible Introducing new ideas and challenging previous methods should be easy, and testing new ideas on current data should be fast and efficient In this book I will attempt to address all the topics associated with data processing and visualization: managing files and directories, reading files of varying formats, and performing signal processing and numerical analysis in a high-level programming language similar to MATLAB and GNU-Octave Along the way, I will teach you Python, a rich and powerful programming language In a nutshell, Beginning Python Visualization deals with the processing, analysis, manipulation, and visualization of data using the Python programming language The book covers the following: • Fundamentals of the Python programming language required for data analysis and visualization • Data files, format, and organization, as well as methods and guidelines for selecting file formats and storing and organizing data to enable fast, efficient data processing • Readily available Python packages for numerical analysis, signal and image processing, graphing and plotting, and more To demonstrate what’s possible, this chapter will present a case study of using Python to gather GPS data, analyze the data prior to visualization, and plot the results www.it-ebooks.info Chapter ■ Navigating the World of Data Visualization Before we begin, however, you should understand a few fundamentals about Python Python is an interpreted programming language This means that each command is first read and then executed This is in contrast to compiled programming languages, where the entire program is evaluated (compiled) and then executed One of the important features of interpreted programming languages is that it’s easy to run them interactively That is, you can perform a command, examine the results, perform more commands, examine more results, and so on The ability to run Python interactively is very useful, and it allows you to examine topics as you learn them It’s also possible to run programs, referred to as scripts, non-interactively in Python, and there are several ways to that You can run scripts from the interactive Python prompt by issuing the command exec(open('scriptname.py').read()) Or you can enter python scriptname.py at the command-line interface of your operating system If you’re using IPython, you can issue the command run scriptname.py instead; and if you’re running IDLE, the Python GUI, you can open the script and press F5 to execute it The py extension is a common convention that distinguishes Python scripts from other files The case study described in this chapter takes advantage of scripts, as well as running Python interactively ■■Note It is important to be able to distinguish between interactive sessions and Python scripts When code starts with >>>, it means that the code was run on Python interactively In cases where the ellipsis symbol ( .) appears, it means that the code is a continuation of a previously interactively entered command Lines of text following the symbols or >>> are Python’s response to the issued command A code listing that does not start with >>>is a script written in an editor; in order to execute it, you will have to save it under scriptname.py (or some other name) and execute it as described previously Gathering Data We spend considerable time recording and analyzing data Data is stored in various formats depending on the tools used to collect it, the nature of the data (e.g., pictures vs sampled analog data), the application that will later process the data, and personal preferences Data files are of varying sizes; some are very large, others are smaller but in larger quantities Data organization adds another level of complexity Files can be stored in directories according to date, grouped together in one big directory or in a database, or adhere to a different scheme altogether Typically, the number of data files or the amount of data per file is too large to allow skimming or browsing with an editor or viewer Methods and tools are required to find the data and analyze it to produce meaningful results As you’ll soon see, Python provides all the tools required to just that Case Study: GPS Data You just got a USB GPS receiver for your birthday! You’d like to analyze GPS data and find out how often you exceed the speed limit and how much time you spend in traffic You’d like to track data over a year, or even longer You decide to record, analyze, and visualize the GPS data in Python Some hardware background: most USB GPS receivers behave as serial ports (this is also true for Bluetooth GPS devices) This means that once a GPS is connected (assuming it’s installed properly), reading GPS data is as simple as opening the COM port associated with the GPS and reading the values GPS values are typically clear text values: numbers and text Of course, if you’re planning on recording GPS data from your car, it would make a lot of sense to hook it up to a laptop rather than a desktop www.it-ebooks.info Chapter ■ Navigating the World of Data Visualization ■■Note If you wish to follow along with the remainder of the chapter by issuing the commands yourself and then viewing the results, you might first want to refer to Chapter and set up Python on your system That said, it’s not necessary, and you can follow along to get an understanding of the book and its purpose In fact, I encourage you to come back to this chapter and read it again after you’ve had more experience with Python To be able to access the serial port from Python, we’ll use the pySerial module pySerial, as the name suggests, allows seamless access to serial ports (the module pySerial requires downloading and installing; see Chapter for details) To use pySerial, we must first read the module to memory, that is, we must import it using the import command If all goes well, we’ll be presented with the Python prompt again   >>> import serial   Scanning Serial Ports Next, we need to find the serial port parameters: the baud rate and the port number The baud rate is a GPS parameter, so it’s best to consult the GPS manual (don’t worry if you can’t find this information, I’ll discuss later how to “guess” what it is) The port number is determined by your operating system If you’re not sure how to find the port number—or if the port number keeps changing when you plug and unplug your GPS—you can use the following code to identify active serial ports (see Listing 1-1a) Listing 1-1a.  Scanning Serial Ports (Linux) >>> from serial.tools.list_ports import comports >>> comports() [('/dev/ttyS3', 'ttyS3', 'n/a'), ('/dev/ttyS2', 'ttyS2', 'n/a'), ('/dev/ttyS1', 'ttyS1', 'n/a'), ('/dev/ttyS0', 'ttyS0', 'n/a'), ('/dev/ttyUSB0', 'Company name and device info should be here', 'USB VID:PID=xxxx:yyyy')]   Listing 1-1a tells us that there are four serial ports named /dev/ttySn, where n is an integer less than or equal to There is also a port named /dev/ttyUSB0, and this is the port I’m looking for In Windows the code looks slightly different The reason: the function comport() returns a generator expression instead of a list of available ports (you will learn more about generator expressions in Chapter 3) Listing 1-1b shows the Windows version of the script Listing 1-1b.  Scanning Serial Ports (Windows) >>> from serial.tools.list_ports import comports >>> >>> list(comports()) [('COM6', 'Company name and device info', 'USB VID:PID=xxxx:yyyy')]   www.it-ebooks.info Chapter ■ Navigating the World of Data Visualization This is a rather quick introduction to Python! First, let’s dissect the code line-by-line The first line, from serial.tools.list_ports import comports, allows us to access a function named comports() By using the import command, we load the function comports() and are able to use it The function comports() is part of a module (a module is a collection of functions and data structures) named tools The package serial is a collection of modules associated with the serial port, one of which is tools Accessing modules within packages is performed using the dot operator This is something you’ll see a lot of in Python: from package.module import function (see Chapter for more on this topic) The second line calls the function comports(); in both the Linux and the Windows versions, it returns a list of available serial ports In the Linux version, the list is returned by calling the function comports() directly In the Windows version, a rather more complex mechanism is used, called a generator expression This is a rather advanced topic and is discussed in Chapter 3, so we will skip it for now In both versions, the list is composed of pairs of values The first value is the location of the serial port, and the second is a description Write down the serial port location; you’ll need it for the next section Recording GPS Data Let’s start gathering data Enter the code in Listing 1-2 and save it in the file, record_gps.py Listing 1-2.  record_gps.py import time, serial   # change these parameters to your GPS parameters port = '/dev/ttyUSB0' # in Windows, set this to 'COMx' ser = serial.Serial(port)   ser.baudrate = 4800 fmt = " /data/GPS-%4d-%02d-%02d-%02d-%02d-%02d.csv"   filename = fmt % time.localtime()[0:6] f = open(filename, 'wb') while True: line = ser.readline() f.write(line) print(line)  ■■Note If your GPS does not support access through the serial port and you would like to follow along with this chapter, you can download an example data file from the official website for this book and continue with that This time, we’ve imported another module: time The time module provides access to date and time functions, and we’ll use those to name our GPS data files We also introduce an important notion here: comments! Comments in Python are denoted by the # sign and are similar to C++ double slash notation, // Everything in the line from that point onward is considered a remark If the # sign is at the beginning of a line, then the entire line is a remark, usually describing the next line or block of code The exception to the # sign indicating a remark occurs when it is quoted inside a string, as follows: "#" www.it-ebooks.info This book is dedicated to my wife, Orna Vaingast www.it-ebooks.info Contents About the Author��������������������������������������������������������������������������������������������������������������� xix About the Technical Reviewer������������������������������������������������������������������������������������������� xxi Acknowledgments����������������������������������������������������������������������������������������������������������� xxiii Introduction���������������������������������������������������������������������������������������������������������������������� xxv ■■Chapter 1: Navigating the World of Data Visualization������������������������������������������������������1 Gathering Data������������������������������������������������������������������������������������������������������������������������������2 Case Study: GPS Data�������������������������������������������������������������������������������������������������������������������������������������������� Scanning Serial Ports�������������������������������������������������������������������������������������������������������������������������������������������� Recording GPS Data����������������������������������������������������������������������������������������������������������������������������������������������� Data Organization��������������������������������������������������������������������������������������������������������������������������6 File Format������������������������������������������������������������������������������������������������������������������������������������������������������������ File Naming Conventions��������������������������������������������������������������������������������������������������������������������������������������� Data Location��������������������������������������������������������������������������������������������������������������������������������������������������������� Data Analysis���������������������������������������������������������������������������������������������������������������������������������7 Walking Directories����������������������������������������������������������������������������������������������������������������������������������������������� Reading CSV Files�������������������������������������������������������������������������������������������������������������������������������������������������� Analyzing GPS Data��������������������������������������������������������������������������������������������������������������������������������������������� 12 Extracting GPS Data��������������������������������������������������������������������������������������������������������������������������������������������� 14 Data Visualization������������������������������������������������������������������������������������������������������������������������17 GPS Location Plot������������������������������������������������������������������������������������������������������������������������������������������������ 18 Annotating the Graph������������������������������������������������������������������������������������������������������������������������������������������� 20 Velocity Plot��������������������������������������������������������������������������������������������������������������������������������������������������������� 22 vii www.it-ebooks.info ■ Contents Subplots��������������������������������������������������������������������������������������������������������������������������������������������������������������� 23 Text���������������������������������������������������������������������������������������������������������������������������������������������������������������������� 23 Tying It All Together���������������������������������������������������������������������������������������������������������������������25 Final Notes and References��������������������������������������������������������������������������������������������������������29 ■■Chapter 2: The Environment��������������������������������������������������������������������������������������������31 Tools of the Trade������������������������������������������������������������������������������������������������������������������������31 Operating Systems����������������������������������������������������������������������������������������������������������������������31 GNU/Linux������������������������������������������������������������������������������������������������������������������������������������������������������������ 32 Windows�������������������������������������������������������������������������������������������������������������������������������������������������������������� 32 Choosing an Operating System��������������������������������������������������������������������������������������������������������������������������� 34 Then Again, Why Choose? Using Several Operating Systems����������������������������������������������������������������������������� 35 The Python Environment�������������������������������������������������������������������������������������������������������������36 Versions��������������������������������������������������������������������������������������������������������������������������������������������������������������� 36 Python������������������������������������������������������������������������������������������������������������������������������������������������������������������ 38 Python Integrated Development Environments��������������������������������������������������������������������������������������������������� 38 Scientific Computing������������������������������������������������������������������������������������������������������������������������������������������� 43 Manually Installing a Python Package����������������������������������������������������������������������������������������������������������������� 44 Installation Summary������������������������������������������������������������������������������������������������������������������������������������������ 45 Additional Applications����������������������������������������������������������������������������������������������������������������46 Editors����������������������������������������������������������������������������������������������������������������������������������������������������������������� 46 A Short List of Text Editors����������������������������������������������������������������������������������������������������������������������������������� 48 Spreadsheets������������������������������������������������������������������������������������������������������������������������������������������������������� 49 Word Processors�������������������������������������������������������������������������������������������������������������������������������������������������� 49 Image Viewers����������������������������������������������������������������������������������������������������������������������������������������������������� 49 Version-Control Systems������������������������������������������������������������������������������������������������������������������������������������� 49 Licensing�������������������������������������������������������������������������������������������������������������������������������������52 Final Notes and References��������������������������������������������������������������������������������������������������������53 viii www.it-ebooks.info ■ Contents ■■Chapter 3: Python for Programmers�������������������������������������������������������������������������������55 The Building Blocks���������������������������������������������������������������������������������������������������������������������55 What Is Python?��������������������������������������������������������������������������������������������������������������������������55 Interactive Python�����������������������������������������������������������������������������������������������������������������������56 Invoking Python��������������������������������������������������������������������������������������������������������������������������������������������������� 56 Entering Commands�������������������������������������������������������������������������������������������������������������������������������������������� 56 The Interactive Help System�������������������������������������������������������������������������������������������������������������������������������� 58 Moving Around����������������������������������������������������������������������������������������������������������������������������������������������������� 59 Running Scripts���������������������������������������������������������������������������������������������������������������������������60 Data Types�����������������������������������������������������������������������������������������������������������������������������������62 Numbers�������������������������������������������������������������������������������������������������������������������������������������������������������������� 63 Strings����������������������������������������������������������������������������������������������������������������������������������������������������������������� 67 Booleans�������������������������������������������������������������������������������������������������������������������������������������������������������������� 70 Data Structures���������������������������������������������������������������������������������������������������������������������������71 Lists��������������������������������������������������������������������������������������������������������������������������������������������������������������������� 72 Tuples������������������������������������������������������������������������������������������������������������������������������������������������������������������ 76 Dictionaries���������������������������������������������������������������������������������������������������������������������������������������������������������� 79 Sets��������������������������������������������������������������������������������������������������������������������������������������������������������������������� 83 Variables��������������������������������������������������������������������������������������������������������������������������������������85 Statements����������������������������������������������������������������������������������������������������������������������������������86 Printing���������������������������������������������������������������������������������������������������������������������������������������������������������������� 86 User Input������������������������������������������������������������������������������������������������������������������������������������������������������������ 91 Comments����������������������������������������������������������������������������������������������������������������������������������������������������������� 91 Flow Control�������������������������������������������������������������������������������������������������������������������������������������������������������� 92 Some Built-in Functions������������������������������������������������������������������������������������������������������������100 Defining Functions��������������������������������������������������������������������������������������������������������������������101 Generators��������������������������������������������������������������������������������������������������������������������������������������������������������� 103 Generator Expressions��������������������������������������������������������������������������������������������������������������������������������������� 103 Object-Oriented Programming��������������������������������������������������������������������������������������������������105 ix www.it-ebooks.info ■ Contents Modules and Packages�������������������������������������������������������������������������������������������������������������106 The import Statement���������������������������������������������������������������������������������������������������������������������������������������� 106 Modules Installed in a System��������������������������������������������������������������������������������������������������������������������������� 108 The dir Statement���������������������������������������������������������������������������������������������������������������������������������������������� 108 Final Notes and References������������������������������������������������������������������������������������������������������108 ■■Chapter 4: Data Organization����������������������������������������������������������������������������������������109 Organizing Chaos����������������������������������������������������������������������������������������������������������������������109 File Name Conventions��������������������������������������������������������������������������������������������������������������110 Date and Time in a File Name���������������������������������������������������������������������������������������������������������������������������� 110 Useful File Name Titles�������������������������������������������������������������������������������������������������������������������������������������� 111 File Name Extensions���������������������������������������������������������������������������������������������������������������������������������������� 112 File Name Convention Recap����������������������������������������������������������������������������������������������������������������������������� 113 Other Schemes�������������������������������������������������������������������������������������������������������������������������������������������������� 114 File Formats������������������������������������������������������������������������������������������������������������������������������116 CSV File Format������������������������������������������������������������������������������������������������������������������������������������������������� 117 Binary Files�������������������������������������������������������������������������������������������������������������������������������������������������������� 124 Readme Files����������������������������������������������������������������������������������������������������������������������������������������������������� 130 INI Files�������������������������������������������������������������������������������������������������������������������������������������������������������������� 130 XML and Other Formats������������������������������������������������������������������������������������������������������������������������������������� 132 Locating Data Files��������������������������������������������������������������������������������������������������������������������132 Organization into Directories����������������������������������������������������������������������������������������������������������������������������� 132 Searching for Files��������������������������������������������������������������������������������������������������������������������������������������������� 133 Indexing������������������������������������������������������������������������������������������������������������������������������������������������������������� 134 Catalogs������������������������������������������������������������������������������������������������������������������������������������������������������������� 137 Files vs a Database������������������������������������������������������������������������������������������������������������������������������������������� 139 Final Notes and References������������������������������������������������������������������������������������������������������140 x www.it-ebooks.info ■ Contents ■■Chapter 5: Processing Text Files�����������������������������������������������������������������������������������141 Text Is Everywhere��������������������������������������������������������������������������������������������������������������������141 Text and Strings������������������������������������������������������������������������������������������������������������������������141 Splitting Text������������������������������������������������������������������������������������������������������������������������������������������������������ 142 Joining Strings��������������������������������������������������������������������������������������������������������������������������������������������������� 143 Converting Strings to Numbers������������������������������������������������������������������������������������������������������������������������� 144 Find and Replace����������������������������������������������������������������������������������������������������������������������������������������������� 148 Stripping Strings������������������������������������������������������������������������������������������������������������������������������������������������ 149 String Formatting���������������������������������������������������������������������������������������������������������������������������������������������� 150 String Conditionals�������������������������������������������������������������������������������������������������������������������������������������������� 152 More on Strings������������������������������������������������������������������������������������������������������������������������������������������������� 153 Files�������������������������������������������������������������������������������������������������������������������������������������������153 Opening a File���������������������������������������������������������������������������������������������������������������������������������������������������� 153 Closing a File����������������������������������������������������������������������������������������������������������������������������������������������������� 154 Writing Text�������������������������������������������������������������������������������������������������������������������������������������������������������� 154 Reading Text������������������������������������������������������������������������������������������������������������������������������������������������������ 155 Working with Text Files�������������������������������������������������������������������������������������������������������������157 Example: Character, Word, and Line Count�������������������������������������������������������������������������������������������������������� 157 Example: head and tail ������������������������������������������������������������������������������������������������������������������������������������� 158 Example: Splitting and Combining Files������������������������������������������������������������������������������������������������������������ 159 Example: Searching Inside a Text File��������������������������������������������������������������������������������������������������������������� 161 Example: Working with Comments�������������������������������������������������������������������������������������������������������������������� 163 Example: Extracting Numbers from a Text File�������������������������������������������������������������������������������������������������� 163 CSV Files�����������������������������������������������������������������������������������������������������������������������������������165 The csv Module������������������������������������������������������������������������������������������������������������������������������������������������� 165 The csv.reader Object���������������������������������������������������������������������������������������������������������������������������������������� 166 The csv.writer Object����������������������������������������������������������������������������������������������������������������������������������������� 167 More CSV Functionality������������������������������������������������������������������������������������������������������������������������������������� 167 DictReader and DictWriter Objects�������������������������������������������������������������������������������������������������������������������� 168 xi www.it-ebooks.info ■ Contents Date and Time���������������������������������������������������������������������������������������������������������������������������170 Time Module������������������������������������������������������������������������������������������������������������������������������������������������������ 171 The struct_time Tuple���������������������������������������������������������������������������������������������������������������������������������������� 171 Parsing and Formatting Date and Time������������������������������������������������������������������������������������������������������������� 171 The Epoch: “Linearizing” the Time Base������������������������������������������������������������������������������������������������������������ 174 Additional Time and Date Functions������������������������������������������������������������������������������������������������������������������ 179 Regular Expressions������������������������������������������������������������������������������������������������������������������179 Regular Expression Patterns����������������������������������������������������������������������������������������������������������������������������� 179 Special Sequences�������������������������������������������������������������������������������������������������������������������������������������������� 181 Alternatives������������������������������������������������������������������������������������������������������������������������������������������������������� 181 Ranges��������������������������������������������������������������������������������������������������������������������������������������������������������������� 181 When to Use Regular Expressions��������������������������������������������������������������������������������������������������������������������� 182 Internationalization and Localization����������������������������������������������������������������������������������������183 Locale���������������������������������������������������������������������������������������������������������������������������������������������������������������� 183 Unicode Strings������������������������������������������������������������������������������������������������������������������������������������������������� 184 Final Notes and References������������������������������������������������������������������������������������������������������188 ■■Chapter 6: Graphs and Plots������������������������������������������������������������������������������������������189 Visualizing Data�������������������������������������������������������������������������������������������������������������������������189 The matplotlib Package�������������������������������������������������������������������������������������������������������������189 Interactive Graphs vs Image Files��������������������������������������������������������������������������������������������190 Interactive Graphs��������������������������������������������������������������������������������������������������������������������������������������������� 191 Savings Graphs to Files������������������������������������������������������������������������������������������������������������������������������������� 192 Plotting Graphs��������������������������������������������������������������������������������������������������������������������������194 Lines and Markers��������������������������������������������������������������������������������������������������������������������������������������������� 195 Plotting Several Graphs on One Figure�������������������������������������������������������������������������������������������������������������� 197 Line Widths and Marker Sizes��������������������������������������������������������������������������������������������������������������������������� 197 Colors���������������������������������������������������������������������������������������������������������������������������������������������������������������� 198 Controlling the Graph����������������������������������������������������������������������������������������������������������������199 Axis�������������������������������������������������������������������������������������������������������������������������������������������������������������������� 200 Grid and Ticks���������������������������������������������������������������������������������������������������������������������������������������������������� 201 xii www.it-ebooks.info ■ Contents Subplots������������������������������������������������������������������������������������������������������������������������������������������������������������� 201 Erasing the Graph���������������������������������������������������������������������������������������������������������������������������������������������� 203 Adding Text��������������������������������������������������������������������������������������������������������������������������������203 Title�������������������������������������������������������������������������������������������������������������������������������������������������������������������� 203 Axis Labels and Legend������������������������������������������������������������������������������������������������������������������������������������� 203 Text Rendering��������������������������������������������������������������������������������������������������������������������������������������������������� 205 Mathematical Symbols and Expressions����������������������������������������������������������������������������������������������������������� 205 More Graph Types����������������������������������������������������������������������������������������������������������������������206 Bar Charts���������������������������������������������������������������������������������������������������������������������������������������������������������� 206 Histograms�������������������������������������������������������������������������������������������������������������������������������������������������������� 208 Pie Charts���������������������������������������������������������������������������������������������������������������������������������������������������������� 210 Logarithmic Plots����������������������������������������������������������������������������������������������������������������������������������������������� 211 Polar Plots��������������������������������������������������������������������������������������������������������������������������������������������������������� 213 Stem Plots��������������������������������������������������������������������������������������������������������������������������������������������������������� 214 Additional Graphs���������������������������������������������������������������������������������������������������������������������������������������������� 215 Getting and Setting Values��������������������������������������������������������������������������������������������������������220 Setting Figure and Axis Parameters������������������������������������������������������������������������������������������������������������������ 222 Patches�������������������������������������������������������������������������������������������������������������������������������������224 Example: Adding Arrows to a Graph������������������������������������������������������������������������������������������������������������������ 224 Example: Some Other Patches�������������������������������������������������������������������������������������������������������������������������� 226 3D Plots�������������������������������������������������������������������������������������������������������������������������������������227 The Basemap Toolkit�����������������������������������������������������������������������������������������������������������������230 Example: French Airports����������������������������������������������������������������������������������������������������������������������������������� 230 Final Notes and References������������������������������������������������������������������������������������������������������232 ■■Chapter 7: Math Games�������������������������������������������������������������������������������������������������233 Preprocessing Data Prior to Visualization���������������������������������������������������������������������������������233 Modules math and cmath���������������������������������������������������������������������������������������������������������233 Example: Mandelbrot Set����������������������������������������������������������������������������������������������������������������������������������� 236 Example: A Newton Fractal�������������������������������������������������������������������������������������������������������������������������������� 240 xiii www.it-ebooks.info ■ Contents Module decimal�������������������������������������������������������������������������������������������������������������������������245 Module fractions�����������������������������������������������������������������������������������������������������������������������246 Module random�������������������������������������������������������������������������������������������������������������������������247 Using Module random to Solve Probability Questions��������������������������������������������������������������������������������������� 248 Random Sequences������������������������������������������������������������������������������������������������������������������������������������������� 251 Module NumPy��������������������������������������������������������������������������������������������������������������������������252 Array Creation���������������������������������������������������������������������������������������������������������������������������������������������������� 253 Slicing, Indexing, and Reshaping����������������������������������������������������������������������������������������������������������������������� 254 N-Dimensional Arrays���������������������������������������������������������������������������������������������������������������������������������������� 255 Math Functions�������������������������������������������������������������������������������������������������������������������������������������������������� 259 Array Methods and Properties��������������������������������������������������������������������������������������������������������������������������� 261 Other Useful Array Functions����������������������������������������������������������������������������������������������������������������������������� 267 Final Notes and References������������������������������������������������������������������������������������������������������268 ■■Chapter 8: Science and Visualization����������������������������������������������������������������������������269 Numerical Analysis and Signal Processing�������������������������������������������������������������������������������269 Finding Your Way: Variables and Functions�������������������������������������������������������������������������������269 SciPy������������������������������������������������������������������������������������������������������������������������������������������������������������������ 270 Linear Algebra���������������������������������������������������������������������������������������������������������������������������271 Solving a System of Linear Equations��������������������������������������������������������������������������������������������������������������� 271 Vector and Matrix Operations���������������������������������������������������������������������������������������������������������������������������� 272 Matrix Decomposition���������������������������������������������������������������������������������������������������������������������������������������� 274 Additional Linear Algebra Functionality������������������������������������������������������������������������������������������������������������� 275 Numerical Integration����������������������������������������������������������������������������������������������������������������275 More Integration Methods��������������������������������������������������������������������������������������������������������������������������������� 278 Interpolation and Curve Fitting��������������������������������������������������������������������������������������������������279 Piecewise Linear Interpolation�������������������������������������������������������������������������������������������������������������������������� 279 Polynomials������������������������������������������������������������������������������������������������������������������������������������������������������� 281 Uses of Polynomials������������������������������������������������������������������������������������������������������������������������������������������ 282 Spline Interpolation������������������������������������������������������������������������������������������������������������������������������������������� 287 xiv www.it-ebooks.info ■ Contents Solving Nonlinear Equations�����������������������������������������������������������������������������������������������������288 Special Functions����������������������������������������������������������������������������������������������������������������������289 Signal Processing����������������������������������������������������������������������������������������������������������������������290 Functions find, nonzero, where and select�������������������������������������������������������������������������������������������������������� 290 Functions diff and split�������������������������������������������������������������������������������������������������������������������������������������� 295 Waveforms��������������������������������������������������������������������������������������������������������������������������������������������������������� 297 Fourier Transform����������������������������������������������������������������������������������������������������������������������298 Example: FFT of a Sampled Cosine Wave���������������������������������������������������������������������������������������������������������� 298 Window Functions��������������������������������������������������������������������������������������������������������������������������������������������� 300 Filtering�������������������������������������������������������������������������������������������������������������������������������������301 Filter Design������������������������������������������������������������������������������������������������������������������������������������������������������ 301 Example: A Heart-Rate Monitor������������������������������������������������������������������������������������������������������������������������� 303 Example: Moving Average���������������������������������������������������������������������������������������������������������������������������������� 305 Final Notes and References������������������������������������������������������������������������������������������������������306 ■■Chapter 9: Image Processing����������������������������������������������������������������������������������������307 Two-Dimensional Data��������������������������������������������������������������������������������������������������������������307 Reading, Writing, and Displaying Images����������������������������������������������������������������������������������307 Reading Images from File���������������������������������������������������������������������������������������������������������������������������������� 308 Image Attributes������������������������������������������������������������������������������������������������������������������������������������������������ 308 Displaying Images��������������������������������������������������������������������������������������������������������������������������������������������� 310 Converting File Formats������������������������������������������������������������������������������������������������������������������������������������ 310 Image Manipulation�������������������������������������������������������������������������������������������������������������������312 Creating New Images���������������������������������������������������������������������������������������������������������������������������������������� 312 Copy and Paste�������������������������������������������������������������������������������������������������������������������������������������������������� 313 Crop and Resize������������������������������������������������������������������������������������������������������������������������������������������������� 315 Rotate���������������������������������������������������������������������������������������������������������������������������������������������������������������� 316 Image Annotation����������������������������������������������������������������������������������������������������������������������317 Annotating with Geometrical Shapes���������������������������������������������������������������������������������������������������������������� 317 Text Annotations������������������������������������������������������������������������������������������������������������������������������������������������ 318 xv www.it-ebooks.info ■ Contents Image Processing����������������������������������������������������������������������������������������������������������������������323 Matrix Representation and Colors��������������������������������������������������������������������������������������������������������������������� 323 Reading an Image to a NumPy Array����������������������������������������������������������������������������������������������������������������� 326 Example: Counting Objects (Five Parts)������������������������������������������������������������������������������������������������������������� 327 Image Arithmetic����������������������������������������������������������������������������������������������������������������������������������������������� 335 Image Filtering��������������������������������������������������������������������������������������������������������������������������337 Making Movies��������������������������������������������������������������������������������������������������������������������������339 Splitting Movies������������������������������������������������������������������������������������������������������������������������������������������������� 339 Creating Movies from Images���������������������������������������������������������������������������������������������������������������������������� 340 Example: A Fractal Movie���������������������������������������������������������������������������������������������������������������������������������� 340 Final Notes and References������������������������������������������������������������������������������������������������������341 ■■Chapter 10: Advanced File Processing��������������������������������������������������������������������������343 More on Files�����������������������������������������������������������������������������������������������������������������������������343 Binary Files and Random Access����������������������������������������������������������������������������������������������343 Example: Reading the Nth Field������������������������������������������������������������������������������������������������������������������������� 345 Example: Efficient Tail Implementation�������������������������������������������������������������������������������������������������������������� 346 Example: Creating a Fixed Size File������������������������������������������������������������������������������������������������������������������� 347 Example: Recording Time-Based Binary Data��������������������������������������������������������������������������������������������������� 348 Reading MATLAB Files as NumPy Arrays����������������������������������������������������������������������������������350 Reading Text Files Directly to NumPy Arrays�����������������������������������������������������������������������������351 Example: Reading and Writing Text Files to NumPy Arrays������������������������������������������������������������������������������� 351 Object Serialization�������������������������������������������������������������������������������������������������������������������352 The Pickle Module��������������������������������������������������������������������������������������������������������������������������������������������� 352 Command-Line Parameters������������������������������������������������������������������������������������������������������354 argv�������������������������������������������������������������������������������������������������������������������������������������������������������������������� 354 Example: Creating a Fixed Size File (a Stand-Alone Script)������������������������������������������������������������������������������� 355 The optparse Module����������������������������������������������������������������������������������������������������������������������������������������� 356 The FileInput Module����������������������������������������������������������������������������������������������������������������������������������������� 359 xvi www.it-ebooks.info ■ Contents File and Directory Manipulation������������������������������������������������������������������������������������������������361 Module glob������������������������������������������������������������������������������������������������������������������������������������������������������� 361 Additional os Module Functionality�������������������������������������������������������������������������������������������������������������������� 361 Additional os.path Module Functionality����������������������������������������������������������������������������������������������������������� 362 Module shutil����������������������������������������������������������������������������������������������������������������������������������������������������� 363 File Compression�����������������������������������������������������������������������������������������������������������������������365 Example: A Compressed tar File������������������������������������������������������������������������������������������������������������������������ 365 Comparing Files������������������������������������������������������������������������������������������������������������������������367 Module filecmp�������������������������������������������������������������������������������������������������������������������������������������������������� 367 Module difflib����������������������������������������������������������������������������������������������������������������������������������������������������� 368 Final Notes and References������������������������������������������������������������������������������������������������������370 ■■Appendix: Additional Source Listing�����������������������������������������������������������������������������371 Nudge Subplots�������������������������������������������������������������������������������������������������������������������������371 Magic Square Arrows����������������������������������������������������������������������������������������������������������������372 Numerical Integration Visualization Source Code���������������������������������������������������������������������375 Fractal Function Source Code���������������������������������������������������������������������������������������������������376 Index���������������������������������������������������������������������������������������������������������������������������������379 xvii www.it-ebooks.info About the Author Shai Vaingast has been an engineer, an engineering manager, and a director of engineering since 1993 He has worked in the defense industry and in the medical device industry while being heavily involved with data processing and visualization He has several patents xix www.it-ebooks.info About the Technical Reviewer Leigh Sheneman is a Graduate Research Assistant in the BEACON Center for the Study of Evolution in Action at Michigan State University She has spent many years helping non-profit organizations develop interactive applications that focus on efficiency Sheneman holds degrees from Texas A&M University and The University of Texas at Tyler She blogs on a wide range of topics at leighsheneman.com xxi www.it-ebooks.info Acknowledgments I’d like to thank the following individuals for their contributions to the first edition of the book (in alphabetical order): Shai Ayal, C.Titus Brown, Ehud Cohen, Bryan Crouse, Kylie Johnston, Michelle Lowman, Rich Lundeen, Frank Pohlmann, Ami Saguy, Sam Saguy, Janet Vaingast, Motty Vaingast, Orna Vaingast, and Arnon Zeira I would also like to thank the following individuals for their contributions to the second edition of the book (in alphabetical order): Alex Elberg, Linda Laflamme, and Leigh Sheneman xxiii www.it-ebooks.info ... you Python, a rich and powerful programming language In a nutshell, Beginning Python Visualization deals with the processing, analysis, manipulation, and visualization of data using the Python. .. New in the Second Edition Aside from using the most up-to-date version of Python that supports all the visualization packages (version 3.3 at the time of the writing the second edition) , I’ve also... with data analysis and visualization in Python, including the selection of an OS, installing Python, and installing third-party packages If you’re new to Python, your next stop should be Chapter

Ngày đăng: 18/05/2017, 23:26

Mục lục

  • Contents at a Glance

  • About the Technical Reviewer

  • Chapter 1: Navigating the World of Data Visualization

    • Gathering Data

      • Case Study: GPS Data

      • Data Visualization

        • GPS Location Plot

        • Tying It All Together

        • Final Notes and References

        • Chapter 2: The Environment

          • Tools of the Trade

          • Choosing an Operating System

          • Then Again, Why Choose? Using Several Operating Systems

          • Python

            • Python Distributions with Scientific Packages

            • Python Integrated Development Environments

              • IDLE

              • Manually Installing a Python Package

              • Additional Applications

                • Editors

                  • Selecting the Proper Editor for You

                  • A Short List of Text Editors

                  • Version-Control Systems

                    • Example: Directory Structure for the Book

                    • Final Notes and References

                    • Chapter 3: Python for Programmers

                      • The Building Blocks

                      • Entering Commands

                        • The Result Variable

                        • The Interactive Help System

                        • Flow Control

                          • if, elif, else

Tài liệu cùng người dùng

Tài liệu liên quan