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 virtualenv and virtualenvwrapper


If you are working on many projects simultaneously, or even just switching between them frequently, you'll find that having everything installed system-wide is not the best option and can bring problems in future on different systems (production) where you want to run your software. This is not a good time to find out that you are missing a certain package or have versioning conflicts between packages that are already installed on production system; hence, virtualenv.

virtualenv is an open source project started by Ian Bicking that enables a developer to isolate working environments per project, for easier maintenance of different package versions.

For example, you inherited legacy Django website based on Django 1.1 and Python 2.3, but at the same time you are working on a new project that must be written in Python 2.6. This is my usual case—having more than one required Python version (and related packages) depending on the project I am working on.

virtualenv enables me to easily switch to different environments and have the same package easily reproduced if I need to switch to another machine or to deploy software to a production server (or to a client's workstation).

Getting ready

To install virtualenv, you must have workable installation of Python and pip. Pip is a tool for installing and managing Python packages, and it is a replacement for easy install. We will use pip through most of this book for package management. Pip is easily installed, as root executes the following line in your terminal:

# easy_install pip

virtualenv by itself is really useful, but with the help of virtualenvwrapper, all this becomes easy to do and also easy to organize many virtual environments. See all the features at http://virtualenvwrapper.readthedocs.org/en/latest/#features.

How to do it...

By performing the following steps you can install the virtualenv and virtualenvwrapper tools:

  1. Install virtualenv and virtualenvwrapper:

    $ sudo pip virtualenv
    $ sudo pip virtualenvwrapper
    # Create folder to hold all our virtual environments and export the path to it.
    $ export VIRTENV=~/.virtualenvs
    $ mkdir -p $VIRTENV
    # We source (ie. execute) shell script to activate the wrappers
    $ source /usr/local/bin/virtualenvwrapper.sh
    # And create our first virtual environment
    $ mkvirtualenv virt1
    
  2. You can now install our favorite package inside virt1:

    (virt1)user1:~$ pip install matplotlib
    
  3. You will probably want to add the following line to your ~/.bashrc file:

    source /usr/loca/bin/virtualenvwrapper.sh

Few useful and most frequently used commands are as follows:

  • mkvirtualenv ENV: This creates virtual environment with name ENV and activates it

  • workon ENV: This activates the previously created ENV

  • deactivate: This gets us out of the current virtual environment