Book Image

Python Data Visualization Cookbook

By : Igor Milovanovic
Book Image

Python Data Visualization Cookbook

By: Igor Milovanovic

Overview of this book

Today, data visualization is a hot topic as a direct result of the vast amount of data created every second. Transforming that data into information is a complex task for data visualization professionals, who, at the same time, try to understand the data and objectively transfer that understanding to others. This book is a set of practical recipes that strive to help the reader get a firm grasp of the area of data visualization using Python and its popular visualization and data libraries. Python Data Visualization Cookbook will progress the reader from the point of installing and setting up a Python environment for data manipulation and visualization all the way to 3D animations using Python libraries. Readers will benefit from over 60 precise and reproducible recipes that guide the reader towards a better understanding of data concepts and the building blocks for subsequent and sometimes more advanced concepts. Python Data Visualization Cookbook starts by showing you how to set up matplotlib and the related libraries that are required for most parts of the book, before moving on to discuss some of the lesser-used diagrams and charts such as Gantt Charts or Sankey diagrams. During the book, we go from simple plots and charts to more advanced ones, thoroughly explaining why we used them and how not to use them. As we go through the book, we will also discuss 3D diagrams. We will peep into animations just to show you what it takes to go into that area. Maps are irreplaceable for displaying geo-spatial data, so we also show you how to build them. In the last chapter, we show you how to incorporate matplotlib into different environments, such as a writing system, LaTeX, or how to create Gantt charts using Python. This book will help those who already know how to program in Python to explore a new field – one of data visualization. As this book is all about recipes that explain how to do something, code samples are abundant, and they are followed by visual diagrams and charts to help you understand the logic and compare your own results with what is explained in the book.
Table of Contents (15 chapters)
Python Data Visualization Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Installing matplotlib on Mac OS X


The easiest way to get matplotlib on Mac OS X is to use prepackaged python distributions such as Enthought Python Distribution (EPD). Just go to the EPD site and download and install the latest stable version for your OS.

In case you are not satisfied with EPD or cannot use it for other reasons such as versions distributed with it, there is a manual (read: harder) way of installing Python, matplotlib, and its dependencies.

Getting ready

We will use the Homebrew project that eases installation of all software that Apple did not install on your OS, including Python and matplotlib. Under the hood, Homebrew is a set of Ruby and Git that automate download and installation. Following these instructions should get the installation working. First, we will install Homebrew, and then Python, followed by tools such as virtualenv, then dependencies for matplotlib (NumPy and SciPy), and finally matplotlib. Hold on, here we go.

How to do it...

  1. In your Terminal paste and execute the following command:

    ruby <(curl -fsSkL raw.github.com/mxcl/homebrew/go)
    

    After the command finishes, try running brew update or brew doctor to verify that installation is working properly.

  2. Next, add the Homebrew directory to your system path, so the packages you install using Homebrew have greater priority than other versions. Open ~/.bash_profile (or /Users/[your-user-name]/.bash_profile) and add the following line to the end of file:

    export PATH=/usr/local/bin:$PATH
    
  3. You will need to restart the terminal so it picks a new path. Installing Python is as easy as firing up another one-liner:

    brew install python --framework --universal
    

    This will also install any prerequisites required by Python.

  4. Now, you need to update your path (add to the same line):

    export PATH=/usr/local/share/python:/usr/local/bin:$PATH
    
  5. To verify that installation worked, type python --version at the command line, you should see 2.7.3 as the version number in the response.

  6. You should have pip installed by now. In case it is not installed, use easy_install to add pip:

    $ easy_install pip
    
  7. Now, it's easy to install any required package; for example, virtualenv and virtualenvwrapper are useful:

    pip install virtualenv
    pip install virtualenvwrapper
    
  8. Next step is what we really wanted to do all along—install matplotlib:

    pip install numpy
    brew install gfortran
    pip install scipy
    

    Note

    Mountain Lion users will need to install the development version of SciPy (0.11) by executing the following line:

    pip install -e git+https://github.com/scipy/scipy#egg=scipy-dev
    
  9. Verify that everything is working. Call Python and execute the following commands:

    import numpy
    print numpy.__version__
    import scipy
    print scipy.__version__
    quit()
    
  10. Install matplotlib:

    pip install matplotlib