72556
72556
com
https://ebookname.com/product/python-data-visualization-
cookbook-milovanovic/
OR CLICK HERE
DOWLOAD EBOOK
https://ebookname.com/product/data-visualization-with-python-and-
javascript-scrape-clean-explore-transform-your-data-1st-edition-
kyran-dale/
https://ebookname.com/product/python-cookbook-2nd-ed-edition-
beazley/
https://ebookname.com/product/python-machine-learning-
cookbook-1st-edition-joshi/
https://ebookname.com/product/misreading-plato-1st-edition-
matthew-clemente/
Research Design in Counseling 3rd Edition P. Paul
Heppner
https://ebookname.com/product/research-design-in-counseling-3rd-
edition-p-paul-heppner/
https://ebookname.com/product/artificial-intelligence-approaches-
tools-and-applications-approaches-tools-and-applications-1st-
edition-brent-m-gordon/
https://ebookname.com/product/the-christian-hope-brian-
hebblethwaite/
https://ebookname.com/product/plant-user-specification-guide-1st-
edition-james-hitchmough/
https://ebookname.com/product/special-edition-using-
wordperfect-12-laura-acklen/
Weapons Warfare Ancient Medieval John Powell
https://ebookname.com/product/weapons-warfare-ancient-medieval-
john-powell/
www.it-ebooks.info
Python Data
Visualization Cookbook
Igor Milovanović
BIRMINGHAM - MUMBAI
www.it-ebooks.info
Python Data Visualization Cookbook
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or
transmitted in any form or by any means, without the prior written permission of the publisher,
except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the
information presented. However, the information contained in this book is sold without
warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers
and distributors will be held liable for any damages caused or alleged to be caused directly or
indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies
and products mentioned in this book by the appropriate use of capitals. However, Packt
Publishing cannot guarantee the accuracy of this information.
ISBN 978-1-78216-336-7
www.packtpub.com
www.it-ebooks.info
Credits
Reviewers Proofreaders
Tarek Amr Amy Johnson
Simeone Franklin Lindsey Thomas
Jayesh K. Gupta
Indexer
Kostiantyn Kucher
Mariammal Chettiyar
Kenneth Emeka Odoh
Graphics
Acquisition Editor Abhinash Sahu
James Jones
Production Coordinator
Lead Technical Editor Shantanu Zagade
Ankita Shashi
Cover Work
Technical Editors Shantanu Zagade
Pratik More
Amit Ramadas
Ritika Singh
Copy Editors
Brandt D'Mello
Janbal Dharmaraj
Deepa Nambiar
Kirti Pai
Laxmi Subramanian
www.it-ebooks.info
About the Author
He is an Evangelist for high-quality systems design who holds strong interests in software
architecture and development methodologies. He is always persistent on advocating
methodologies that promote high-quality software, such as test-driven development,
one-step builds, and continuous integration.
He also possesses solid knowledge of product development. Having field experience and
official training, he is capable of transferring knowledge and communication flow from
business to developers and vice versa.
www.it-ebooks.info
About the Reviewers
Tarek Amr achieved his postgraduate degree in Data Mining and Information Retrieval from
the University of East Anglia. He has about 10 years' experience in Software Development.
He has been volunteering in Global Voices Online (GVO) since 2007, and currently he is the
local ambassador of the Open Knowledge Foundation (OKFN) in Egypt. Words such as Open
Data, Government 2.0, Data Visualisation, Data Journalism, Machine Learning, and Natural
Language Processing are like music to his ears.
Jayesh K. Gupta is the Lead Developer of Matlab Toolbox for Biclustering Analysis (MTBA).
He is currently an undergraduate student and researcher at IIT Kanpur. His interests lie in the
field of pattern recognition. His interests also lie in basic sciences, recognizing them as the
means of analyzing patterns in nature. Coming to IIT, he realized how this analysis is being
augmented by Machine Learning algorithms with various diverse applications. He believes
that augmenting human thought with machine intelligence is one of the best ways to advance
human knowledge. He is a long time technophile and a free-software Evangelist. He usually
goes by the handle, rejuvyesh online. He is also an avid reader and his books can be checked
out at Goodreads. Checkout his projects at Bitbucket and GitHub. For all links visit http://
home.iitk.ac.in/~jayeshkg/. He can be contacted at [email protected].
www.it-ebooks.info
Kostiantyn Kucher was born in Odessa, Ukraine. He received his Master's degree in
Computer Science from Odessa National Polytechnic University in 2012. He used Python
as well as Matplotlib and PIL for Machine Learning and Image Recognition purposes.
Kenneth Emeka Odoh performs research on state of the art Data Visualization
techniques. His research interest includes exploratory search where the users are
guided to their search results using visual clues.
When Kenneth is not writing source codes, you can find him singing at the Campion College
chant choir.
www.it-ebooks.info
www.PacktPub.com
Did you know that Packt offers eBook versions of every book published, with PDF and ePub
files available? You can upgrade to the eBook version at www.PacktPub.com and as a print
book customer, you are entitled to a discount on the eBook copy. Get in touch with us at
[email protected] for more details.
At www.PacktPub.com, you can also read a collection of free technical articles, sign up
for a range of free newsletters and receive exclusive discounts and offers on Packt books
and eBooks.
TM
http://PacktLib.PacktPub.com
Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book
library. Here, you can access, read and search across Packt's entire library of books.
Why Subscribe?
ff Fully searchable across every book published by Packt
www.it-ebooks.info
www.it-ebooks.info
Table of Contents
Preface 1
Chapter 1: Preparing Your Working Environment 5
Introduction 5
Installing matplotlib, NumPy, and SciPy 6
Installing virtualenv and virtualenvwrapper 8
Installing matplotlib on Mac OS X 10
Installing matplotlib on Windows 11
Installing Python Imaging Library (PIL) for image processing 12
Installing a requests module 14
Customizing matplotlib's parameters in code 14
Customizing matplotlib's parameters per project 16
Chapter 2: Knowing Your Data 19
Introduction 19
Importing data from CSV 20
Importing data from Microsoft Excel files 22
Importing data from fixed-width datafiles 25
Importing data from tab-delimited files 27
Importing data from a JSON resource 28
Exporting data to JSON, CSV, and Excel 31
Importing data from a database 36
Cleaning up data from outliers 40
Reading files in chunks 46
Reading streaming data sources 48
Importing image data into NumPy arrays 50
Generating controlled random datasets 56
Smoothing the noise in real-world data 64
www.it-ebooks.info
Table of Contents
Chapter 3: Drawing Your First Plots and Customizing Them 71
Introduction 72
Defining plot types – bar, line, and stacked charts 72
Drawing a simple sine and cosine plot 78
Defining axis lengths and limits 81
Defining plot line styles, properties, and format strings 84
Setting ticks, labels, and grids 89
Adding a legend and annotations 92
Moving spines to the center 95
Making histograms 96
Making bar charts with error bars 99
Making pie charts count 101
Plotting with filled areas 103
Drawing scatter plots with colored markers 105
Chapter 4: More Plots and Customizations 109
Introduction 109
Setting the transparency and size of axis labels 110
Adding a shadow to the chart line 113
Adding a data table to the figure 116
Using subplots 118
Customizing grids 121
Creating contour plots 125
Filling an under-plot area 128
Drawing polar plots 131
Visualizing the filesystem tree using a polar bar 134
Chapter 5: Making 3D Visualizations 139
Introduction 139
Creating 3D bars 139
Creating 3D histograms 143
Animating in matplotlib 146
Animating with OpenGL 150
ii
www.it-ebooks.info
Table of Contents
iii
www.it-ebooks.info
Table of Contents
iv
www.it-ebooks.info
Preface
The best data is the data that we can see and understand. As developers, we want to
create and build the most comprehensive and understandable visualizations. It is not always
simple; we need to find the data, read it, clean it, massage it, and then use the right tool to
visualize it. This book explains the process of how to read, clean, and visualize the data into
information with straight and simple (and not so simple) recipes.
How to read local data, remote data, CSV, JSON, and data from relational databases are all
explained in this book.
Some simple plots can be plotted with a simple one-liner in Python using matplotlib, but
doing more advanced charting requires knowledge of more than just Python. We need to
understand the information theory and human perception aesthetics to produce the most
appealing visualizations.
This book will explain some practices behind plotting with matplotlib in Python, statistics used,
and usage examples for different charting features we should use in an optimal way.
This book is written and the code is developed on Ubuntu 12.03 using Python 2.7,
IPython 0.13.2, virtualenv 1.9.1, matplotlib 1.2.1, NumPy 1.7.1, and SciPy 0.11.0.
Chapter 2, Knowing Your Data, introduces you to common data formats and how to read and
write them, be it CSV, JSON, XSL, or relational databases.
Chapter 3, Drawing Your First Plots and Customizing Them, starts with drawing simple plots
and covers some of the customization.
Chapter 4, More Plots and Customizations, follows up from previous chapter and covers more
advanced charts and grid customization.
www.it-ebooks.info
Preface
Chapter 6, Plotting Charts with Images and Maps, covers image processing, projecting data
onto maps, and creating CAPTCHA test images.
Chapter 7, Using Right Plots to Understand Data, covers explanations and recipes on some
more advanced plotting techniques such as spectrograms and correlations.
Chapter 8, More on matplotlib Gems, covers a set of charts such as Gantt charts, box plots,
and whisker plots, and also explains how to use LaTeX for rendering text in matplotlib.
Other software packages used in this book are IPython, which is an interactive Python
environment that is very powerful, and flexible. This can be installed using package
managers for Linux-based OSes or prepared installers for Windows and Mac OSes.
If you are new to Python installation and software installation in general, it is very much
recommended to use prepackaged scientific Python distributions such as Anaconda,
Enthought Python Distribution, or Python(X,Y).
Other required software mainly comprises of Python packages that are all installed using
the Python installation manager, pip, which itself is installed using Python's easy_install
setup tool.
You will need to know some general programming concepts, and any kind of programming
experience will be helpful. However, the code in this book is explained almost line by line. You
don't need math for this book; every concept that is introduced is thoroughly explained in plain
English, and references are available for further interest in the topic.
Conventions
In this book, you will find a number of styles of text that distinguish between different kinds of
information. Here are some examples of these styles, and an explanation of their meaning.
www.it-ebooks.info
Another Random Document on
Scribd Without Any Related Topics
"Compel them to come in."—ST. LUKE'S GOSPEL
He rose, he heard—
Our father, our St. Peter, in his tears—
The crowing, twice, of the prophetic bird,
The saddest cock-crow of our human years.
THE QUESTION
(I) PROMETHEUS
(II) THETIS
TO SILENCE
SURMISE
CHRISTMAS NIGHT
"IF I CANNOT SEE THEE PRESENT I WILL MOURN
THEE ABSENT, FOR THIS ALSO IS A PROOF OF LOVE"
Thomas à Kempis
We do not find Him on the difficult earth,
In surging human-kind,
In wayside death or accidental birth,
Or in the "march of mind."
*****
EVERLASTING FAREWELLS
"EVERLASTING FAREWELLS! AND AGAIN, AND
YET AGAIN ... EVERLASTING FAREWELLS!"
De Quincey
Hereditary song,
Illyrian lark and Paduan nightingale,
Is yours, unchangeable the ages long;
Assyria heard your tale;
AT NIGHT
To W. M.
Updated editions will replace the previous one—the old editions will
be renamed.
1.D. The copyright laws of the place where you are located also
govern what you can do with this work. Copyright laws in most
countries are in a constant state of change. If you are outside the
United States, check the laws of your country in addition to the
terms of this agreement before downloading, copying, displaying,
performing, distributing or creating derivative works based on this
work or any other Project Gutenberg™ work. The Foundation makes
no representations concerning the copyright status of any work in
any country other than the United States.
1.E.6. You may convert to and distribute this work in any binary,
compressed, marked up, nonproprietary or proprietary form,
including any word processing or hypertext form. However, if you
provide access to or distribute copies of a Project Gutenberg™ work
in a format other than “Plain Vanilla ASCII” or other format used in
the official version posted on the official Project Gutenberg™ website
(www.gutenberg.org), you must, at no additional cost, fee or
expense to the user, provide a copy, a means of exporting a copy, or
a means of obtaining a copy upon request, of the work in its original
“Plain Vanilla ASCII” or other form. Any alternate format must
include the full Project Gutenberg™ License as specified in
paragraph 1.E.1.
• You pay a royalty fee of 20% of the gross profits you derive
from the use of Project Gutenberg™ works calculated using the
method you already use to calculate your applicable taxes. The
fee is owed to the owner of the Project Gutenberg™ trademark,
but he has agreed to donate royalties under this paragraph to
the Project Gutenberg Literary Archive Foundation. Royalty
payments must be paid within 60 days following each date on
which you prepare (or are legally required to prepare) your
periodic tax returns. Royalty payments should be clearly marked
as such and sent to the Project Gutenberg Literary Archive
Foundation at the address specified in Section 4, “Information
about donations to the Project Gutenberg Literary Archive
Foundation.”
• You comply with all other terms of this agreement for free
distribution of Project Gutenberg™ works.
1.F.
1.F.4. Except for the limited right of replacement or refund set forth
in paragraph 1.F.3, this work is provided to you ‘AS-IS’, WITH NO
OTHER WARRANTIES OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO WARRANTIES OF
MERCHANTABILITY OR FITNESS FOR ANY PURPOSE.
Please check the Project Gutenberg web pages for current donation
methods and addresses. Donations are accepted in a number of
other ways including checks, online payments and credit card
donations. To donate, please visit: www.gutenberg.org/donate.
Most people start at our website which has the main PG search
facility: www.gutenberg.org.
Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.
ebookname.com