Python Data Visualization Cookbook 2nd Edition Igor Milovanovic pdf download
Python Data Visualization Cookbook 2nd Edition Igor Milovanovic pdf download
https://ebookultra.com/download/python-data-visualization-
cookbook-2nd-edition-igor-milovanovic/
https://ebookultra.com/download/python-cookbook-third-edition-beazley/
https://ebookultra.com/download/python-cookbook-1st-edition-alex-
martelli/
https://ebookultra.com/download/python-cookbook-3rd-edition-david-
beazley/
Python cookbook 3ed. Edition Beazley D.
https://ebookultra.com/download/python-cookbook-3ed-edition-beazley-d/
https://ebookultra.com/download/ipython-interactive-computing-and-
visualization-cookbook-1st-edition-cyrille-rossant/
https://ebookultra.com/download/data-visualization-with-d3-and-
angularjs-1st-edition-korner/
https://ebookultra.com/download/maya-programming-with-python-
cookbook-1st-edition-herbez/
Data Driven SEO with Python Solve SEO Challenges with Data
Science Using Python 4th Edition Andreas Voniatis
https://ebookultra.com/download/data-driven-seo-with-python-solve-seo-
challenges-with-data-science-using-python-4th-edition-andreas-
voniatis/
Python Data Visualization Cookbook 2nd Edition Igor
Milovanovic Digital Instant Download
Author(s): Igor Milovanovic, Dimitry Foures, Giuseppe Vettigli
ISBN(s): 9781784396695, 1784396699
Edition: 2nd
File Details: PDF, 19.66 MB
Year: 2015
Language: english
www.it-ebooks.info
Python Data Visualization
Cookbook
Second Edition
Igor Milovanović
Dimitry Foures
Giuseppe Vettigli
BIRMINGHAM - MUMBAI
www.it-ebooks.info
Python Data Visualization Cookbook
Second Edition
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 authors, 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-78439-669-5
www.packtpub.com
www.it-ebooks.info
Credits
Reviewer
Kostiantyn Kucher Indexer
Rekha Nair
Commissioning Editor
Akram Hussain Graphics
Jason Monteiro
Acquisition Editor
Meeta Rajani Production Coordinator
Manu Joseph
Technical Editor
Anushree Arun Tendulkar
Copy Editor
Charlotte Carneiro
www.it-ebooks.info
About the Authors
Evangelist for high-quality systems design who holds strong interests in software architecture
and development methodologies, Igor is always persistent on advocating methodologies
which 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.
Igor is most grateful to his girlfriend for letting him spent hours on the work instead with
her and being avid listener to his endless book monologues. He thanks his brother for
being the strongest supporter. He is thankful to his parents to let him develop in various
ways and become a person he is today.
Giuseppe Vettigli is a data scientist who has worked in the research industry and
academia for many years. His work is focused on the development of machine learning
models and applications to use information from structured and unstructured data.
He also writes about scientific computing and data visualization in Python on his blog
at http://glowingpython.blogspot.com.
www.it-ebooks.info
About the Reviewer
Kostiantyn Kucher was born in Odessa, Ukraine. He received his master's degree in
computer science from Odessa National Polytechnic University in 2012, and he has used
Python as well as matplotlib and PIL for machine learning and image recognition purposes.
Since 2013, Kostiantyn has been a PhD student in computer science specializing in information
visualization. He conducts his research under the supervision of Prof. Dr. Andreas Kerren with
the ISOVIS group at the Computer Science department of Linnaeus University (Växjö, Sweden).
Kostiantyn was a technical reviewer for the first edition of this book.
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
service@packtpub.com 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
https://www2.packtpub.com/books/subscription/packtlib
Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book
library. Here, you can search, access, and read Packt's entire library of books.
Why Subscribe?
ff Fully searchable across every book published by Packt
ff Copy and paste, print, and bookmark content
ff On demand and accessible via a web browser
www.it-ebooks.info
Table of Contents
Preface v
Chapter 1: Preparing Your Working Environment 1
Introduction 1
Installing matplotlib, NumPy, and SciPy 2
Installing virtualenv and virtualenvwrapper 4
Installing matplotlib on Mac OS X 7
Installing matplotlib on Windows 9
Installing Python Imaging Library (PIL) for image processing 10
Installing a requests module 11
Customizing matplotlib's parameters in code 12
Customizing matplotlib's parameters per project 14
Chapter 2: Knowing Your Data 17
Introduction 17
Importing data from CSV 18
Importing data from Microsoft Excel files 20
Importing data from fixed-width data files 23
Importing data from tab-delimited files 25
Importing data from a JSON resource 27
Exporting data to JSON, CSV, and Excel 29
Importing and manipulating data with Pandas 34
Importing data from a database 35
Cleaning up data from outliers 40
Reading files in chunks 45
Reading streaming data sources 47
Importing image data into NumPy arrays 49
Generating controlled random datasets 55
Smoothing the noise in real-world data 62
www.it-ebooks.info
Table of Contents
ii
www.it-ebooks.info
Table of Contents
Plotting data on a map using the Google Map API 179
Generating CAPTCHA images 185
Chapter 7: Using the Right Plots to Understand Data 191
Introduction 191
Understanding logarithmic plots 192
Understanding spectrograms 195
Creating stem plot 200
Drawing streamlines of vector flow 204
Using colormaps 208
Using scatter plots and histograms 213
Plotting the cross correlation between two variables 221
Importance of autocorrelation 224
Chapter 8: More on matplotlib Gems 229
Introduction 229
Drawing barbs 229
Making a box-and-whisker plot 233
Making Gantt charts 236
Making error bars 241
Making use of text and font properties 244
Rendering text with LaTeX 251
Understanding the difference between pyplot and OO API 255
Chapter 9: Visualizations on the Clouds with Plot.ly 261
Introduction 261
Creating line charts 262
Creating bar charts 266
Plotting a 3D trefoil knot 269
Visualizing maps and bubbles 272
Index 275
iii
www.it-ebooks.info
www.it-ebooks.info
Preface
The best data is the data that we can see and understand. As developers and data scientists,
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, filter 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 sometimes 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 one simple line in Python using matplotlib, but
performing more advanced charting requires knowledge of more than just Python. We need
to understand 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 that we should use in an optimal way.
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 customization.
Chapter 4, More Plots and Customizations, follows up from the previous chapter and covers
more advanced charts and grid customization.
www.it-ebooks.info
Preface
Chapter 6, Plotting Charts with Images and Maps, deals with 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 it also explains how to use LaTeX for rendering text in matplotlib.
Chapter 9, Visualizations on the Clouds with Plot.ly, introduces how to use Plot.ly to create
and share your visualizations on its cloud environment.
Another software package used in this book is 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 OS X.
If you are new to Python installation and software installation in general, it is highly
recommended to use prepackaged scientific Python distributions such as Anaconda,
Enthought Python Distribution or Python(x, y).
Other required software mainly comprises 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.
vi
www.it-ebooks.info
Preface
Sections
In this book, you will find several headings that appear frequently (Getting ready, How to do it,
How it works, There's more, and See also).
To give clear instructions on how to complete a recipe, we use these sections as follows:
Getting ready
This section tells you what to expect in the recipe, and describes how to set up any software or
any preliminary settings required for the recipe.
How to do it…
This section contains the steps required to follow the recipe.
How it works…
This section usually consists of a detailed explanation of what happened in the previous section.
There's more…
This section consists of additional information about the recipe in order to make the reader
more knowledgeable about the recipe.
See also
This section provides helpful links to other useful information for the recipe.
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.
Code words in text, database table names, folder names, filenames, file extensions, pathnames,
dummy URLs, user input, and Twitter handles are shown as follows: "We packed our little demo
in the DemoPIL class, so that we can extend it easily, while sharing the common code around
the demo function, run_fixed_filters_demo."
vii
www.it-ebooks.info
Preface
When we wish to draw your attention to a particular part of a code block, the relevant lines or
items are set in bold:
for a in range(10):
print a
Reader feedback
Feedback from our readers is always welcome. Let us know what you think about this
book—what you liked or may have disliked. Reader feedback is important for us to develop
titles that you really get the most out of.
If there is a topic that you have expertise in and you are interested in either writing or
contributing to a book, see our author guide on www.packtpub.com/authors.
Customer support
Now that you are the proud owner of a Packt book, we have a number of things to help you to
get the most from your purchase.
viii
www.it-ebooks.info
Random documents with unrelated
content Scribd suggests to you:
Fig. 37.—View of Dwelling from Garden, Tokio.
The verandah is quite spacious; and in line with the division between
the rooms is a groove for the adjustment of a wooden screen or
shutter when it is desired to separate the house into two portions
temporarily. At the end of the verandah to the left of the sketch is the
latrine. The house is quite open beneath, and the air has free
circulation.
Fig. 38.—Dwelling Near Kudan, Tokio.
Another type of a Tokio house is shown in Fig. 38. This is a low, one-
storied house, standing directly upon the street, its tiled roof cut up
into curious gables. The entrance is protected by a barred sliding
door. A large hanging bay-window is also barred. Just over the fence
a bamboo curtain may be seen, which shades the verandah. The
back of the house was open, and probably looked out on a pretty
garden,—though this I did [pg 57] not see, as this sketch, like many
others, was taken somewhat hastily.
From this example some idea may be got of the diminutive character
of many of the Japanese dwellings, in which, nevertheless, families
live in all cleanliness and comfort.
In the northern part of Japan houses are often seen which possess
features suggestive of the picturesque architecture of Switzerland,—
the gable ends showing, in their exterior, massive timbers roughly
hewn, with all the irregularities of the tree-trunk preserved, the
interstices between these beams being filled with clay or plaster. The
eaves are widely overhanging, with projecting rafters. Oftentimes
delicately-carved wood is seen about the gable-ends and projecting
balcony. As a still further suggestion of this resemblance, the main
roof, if shingled, as well as the roof that shelters the verandah, is
weighted with stones of various sizes to prevent its being blown away
by the high [pg 58] winds that often prevail. This feature is
particularly common in the Island of Yezo.
[pg 59]
Another house, shown in fig. 41, was seen on the road to Mororan, in
Yezo. Here the smoke-outlet was in the form of a low supplementary
structure on the ridge. The ridge itself was flat, and upon it grew a
luxuriant mass of lilies. This roof was unusually large and capacious.
At the place where the river Kitakami empties into the Bay of Sendai,
and where we left our boat in which we had come down the river
from Morioka, the houses were all of the olden-style,—a number of
these presenting some good examples of projecting windows. Fig. 42
represents the front of a house in this place. This shows a large
gable-roof, with broad overhanging eaves in front,—the ends of the
rafters projecting to support the eaves and the transverse-beams of
the gable ends being equally in sight. The projecting window, which
might perhaps be called a bay, runs nearly the entire length of the
gable. The panels in the frieze were of [pg 60] dark wood, and bore
perforated designs of pine and bamboo alternating.
The larger houses of this description are always inns. They usually
abut directly upon the road, and have an open appearance and an air
of hospitality about them which at once indicates their character. One
encounters such places so frequently in Japan, that travelling in the
interior is rendered a matter of ease and comfort as compared with
similar experiences in neighboring countries. The larger number of
these inns in the north are of one-story, though many may be seen
that are two-storied. Very rarely does a three-storied building occur.
Fig. 43 represents one of this nature, that was seen in a small village
north of Sendai.
Houses of the better classes stand back from the road, and have
bordering the road high and oftentimes ponderous ridged walls, with
gateways of similar proportions and character, or fences of various
kinds with rustic gateways. Long, low [pg 61] out-buildings, for
servants' quarters, also often form portions of the boundary wall. In
the denser part of larger cities it is rare to find an old house,—the
devastating conflagrations that so often sweep across the cities
rendering the survival of old houses almost an impossibility. In the
suburbs of cities and in the country, however, it is not difficult to find
houses one hundred, and even two or three hundred years old. The
houses age as rapidly as the people, and new houses very soon turn
gray from the weather; the poorer class of houses in particular
appear much older than they really are.
Fig. 44.—Street in the Suburbs of Morioka.
[pg 62]
The country house of an independent samurai, or rich farmer, is
large, roomy, and thoroughly comfortable. I recall with the keenest
pleasure the delightful days enjoyed under the roof of one of these
typical mansions in Kabutoyama, in the western part of the province
of Musashi. The residence consisted of a group of buildings shut in
from the road by a high wall. Passing through a ponderous gateway,
one enters a spacious court-yard, flanked on either side by long, low
buildings used as store-houses and servants' quarters. At the farther
end of the yard, and facing the entrance, was a comfortable old
farmhouse, having a projecting gable-wing to its right (fig. 45). The
roof was a thatched one of unusual thickness. At the end of the wing
was a triangular latticed opening, from which thin blue wreaths of
smoke were curling. This building contained a few rooms, including
an unusually spacious kitchen,—a sketch of which is given farther on.
The kitchen opened directly into a larger and unfinished portion of
the house, having the earth [pg 63] for its floor, and used as a wood-
shed. The owner informed me that the farm-house was nearly three
hundred years old. To the left of the building was a high wooden
fence, and passing through a gateway one came into a smaller yard
and garden. In this area was another house quite independent of the
farmhouse; this was the house for guests. Its conspicuous feature
consisted of a newly-thatched roof, surmounted by an elaborate and
picturesque ridge,—its design derived from temple architecture.
Within were two large rooms opening upon a narrow verandah.
These rooms were unusually high in stud, and the mats and all the
appointments were most scrupulously clean. Communication with the
old house was by means of a covered passage. Back of this dwelling,
and some distance from it, was still another house, two stories in
height, and built in the most perfect taste; and here lived the
grandfather of the family,—a fine old gentleman, dignified and courtly
in his manners.
In the city houses of the better class much care is often taken to
make the surroundings appear as rural as possible, by putting here
and there quaint old wells, primitive and rustic arbors, fences, and
gateways. The gateways receive special attention in this way, and the
oddest of entrances are often seen in thickly-settled parts of large
cities.
The first view, then (fig. 46), is from the street, and represents a
heavily-roofed gateway, with a smaller gateway at the side. The big
gates had been removed, and the little gateway was permanently
closed. This ponderous structure was flanked on one side by a low
stretch of buildings, plastered on the outside, having small barred
windows on the street, and a barred look-out commanding the
gateway both outside and within. On the other side of the gateway
was a high, thick wall, also furnished with a [pg 65] window or
lookout. The outer walls rose directly from the wall forming the
gutter, or, more properly speaking, a diminutive moat that ran along
the side of the street. Blocks of worked stone formed a bridge across
this moat, by which access was gained to the enclosure. The old
dwelling, with its sharp-ridged roof, may be seen above the buildings
just described.
Fig. 47 represents the appearance of this old house from just within
the gateway. The barred window to the left of the sketch may be
seen through the open gateway in fig. 46, and the tree which showed
over the top of the gateway in that sketch is now in full view. The old
house has a thatched roof with a remarkably steep pitch, surmounted
by a ridge of tiles; a narrow tiled roof runs about the house directly
below the eaves of the thatched roof. Suspended below this roof is
seen a ladder and fire-engine, to be ready in case of emergency. The
truth must be [pg 66] told, however, that these domestic engines are
never ready; for when they are wanted, it is found that the square
cylinders are so warped and cracked by the hot summers that when
they are brought into action their chief accomplishment consists in
squirting water through numerous crevices upon the men who are
frantically endeavoring to make these engines do their duty properly.
The yard was well swept, and quite free from weeds, though at one
side a number of shrubs and a banana tree were growing in a
luxuriant tangle. A single tree, of considerable age, rose directly in a
line with the entrance to the yard.
The house, like all such houses, had its uninteresting end toward the
street; and here, attached to the house, was a “lean-to,” or shed,
with a small circular window. This was [pg 67] probably a kitchen, as
a gateway is seen in the sketch, which led to the kitchen-garden.
In Fig. 48 a sketch of this house is given from the garden in the rear.
The house is quite open behind, and looks out on the garden and
fish-pond, which is seen in the foreground. The tiled roof which
covers the verandah, and the out-buildings as well, was a subsequent
addition to the old house. The sole occupants consisted of the
mother and maiden sister of the famous antiquarian Ninagawa
Noritani. The garden, with its shrubs, plats of flowers, stepping-
stones leading to the fish-pond filled with lotus and lilies, and the
bamboo trellis, is a good specimen of an old garden upon which but
little care has been bestowed.
[pg 68]
Fig. 49.—House in Tokio.
The wood-work from the rafters of the verandah roof above, to the
planks below, was undefiled by oil, paint, wood-filling, or varnish of
any kind. The carpentry was light, yet durable and thoroughly
constructive; while outside and inside every feature was as neat and
clean as a cabinet. The room bordering this verandah is shown in fig.
125.
Fig. 50.—View from the Second Story of Dwelling in Imado, Tokio.
The inns, particularly the country inns, have a most cosey and
comfortable air about them. One always has the freedom of the
entire place; at least a foreigner generally makes himself at home
everywhere about the public houses, and in this respect [pg 71]
impress a Japanese with his boorish ways, since the native guests
usually keep to their own rooms. The big, capacious kitchen, with its
smoke-blackened rafters overhead, its ruddy glow of wood-fire (a
sight rarely seen in the cities, where charcoal is the principal fuel),
and the family busy with their various domestic duties, is a most
cosey and agreeable region.
[pg 75]
In the city no outbuildings, such as sheds and barns, are seen.
Accompanying the houses of the better class are solid, thick-walled,
fire-proof buildings called kura, in which the goods and chattels are
stowed away in times of danger from conflagrations. These buildings,
which are known to the foreigner as “go-downs,” are usually two
stories in height, and have one or two small windows, and one door,
closed by thick and ponderous shutters. Such a building usually
stands isolated from the dwelling, and sometimes, though rarely, they
are converted into domiciles. Of such a character is the group of
buildings in Tokio represented in fig. 57, belonging to a genial
antiquary, in which he has stored a rare collection of old books,
manuscripts, paintings, and other antique objects.
The great elaboration and variety in the form and structure of the
house-roof almost merit the dignity of a separate section. For it is
mainly to the roof that the Japanese house owes its picturesque
appearance; it is the roof which gives to the houses that novelty and
variety which is so noticeable among them in different parts of the
country. The lines of a well-made thatched roof are something quite
remarkable in their proportions. A great deal of taste and skill is
displayed in the proper trimming of the eaves; and the graceful way
in which the [pg 78] eaves of the gable are made to join the side
eaves is always attractive and a noticeable feature in Japanese
architecture, and the admirable way in which a variety of gables are
made to unite with the main roof would excite praise from the most
critical architect.
The elaborate structure of the thatched and tiled roofs, and the great
variety in the design and structure of the ridges show what might be
done by a Japanese architect if other portions of the house-exterior
received an equal amount of ingenuity and attention.
The usual form of the roof is generally that of a hip or gable. In the
thatched roof, the portion coming directly below the ridge-pole is in
the form of a gable, and this blends into a hip-roof. A curb-roof is
never seen. Among the poorer classes a simple pent roof is common;
and additions or attachments to the main building are generally
covered with a pent roof. A light, narrow, supplementary roof is often
seen projecting just below the eaves of the main roof; it is generally
made of wide thin boards (fig. 60). This roof is called hisashi. [pg 79]
It commonly shelters from the sun and rain an open portion of the
house or a verandah. It is either supported by uprights from the
ground, or by slender brackets which are framed at right angles to
the main uprights of the building proper. Weak and even flimsy as
this structure often appears to be, it manages to support itself, in
violation of all known laws of structure and gravitation. After a heavy
fall of damp snow one may see thick accumulations covering these
slight roofs, and yet a ride through the city reveals no evidences of
their breaking down. One recalls similar structures at home yielding
under like pressure, and wonders whether gravitation behaves
differently in this land of anomalies.
Fig. 60.—Hisashi.
That the shingles are not always held firmly to the roof by this
method of shingling is seen in the fact that oftentimes long narrow
strips of bamboo are nailed obliquely across the roof, from the ridge-
pole to the eaves (fig. 63). These strips are placed at the distance of
eighteen inches or two feet apart. Yet even in spite of this added
precaution, in violent gales the roof is often rapidly denuded of its
shingles, which fill the air at such times like autumn leaves.
[pg 83]
The shingled roof is the most dangerous element of house-structure
in the cities. The shingles are nothing more than thick shavings, and
Welcome to our website – the ideal destination for book lovers and
knowledge seekers. With a mission to inspire endlessly, we offer a
vast collection of books, ranging from classic literary works to
specialized publications, self-development books, and children's
literature. Each book is a new journey of discovery, expanding
knowledge and enriching the soul of the reade
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.
ebookultra.com