Book Image

Python Data Visualization Cookbook (Second Edition)

Book Image

Python Data Visualization Cookbook (Second Edition)

Overview of this book

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 will 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 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. Initially it uses simple plots and charts to more advanced ones, to make it easy to understand for readers. As the readers will go through the book, they will get to know about the 3D diagrams and animations. Maps are irreplaceable for displaying geo-spatial data, so this book will also show how to build them. In the last chapter, it includes explanation on how to incorporate matplotlib into different environments, such as a writing system, LaTeX, or how to create Gantt charts using Python.
Table of Contents (16 chapters)
Python Data Visualization Cookbook Second Edition
Credits
About the Authors
About the Reviewer
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 you're having 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 between 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 a 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 install virtualenv
    $ sudo pip install 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
    

A few useful and most frequently used commands are as follows:

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

  • workon ENV: This activates the previously created ENV

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

pip not only provides you with a practical way of installing packages, but it also is a good solution for keeping track of the python packages installed on your system, as well as their version. The command pip freeze will print all the installed packages on your current environment, followed by their version number:

$ pip freeze
matplotlib==1.4.3
mock==1.0.1
nose==1.3.6
numpy==1.9.2
pyparsing==2.0.3
python-dateutil==2.4.2
pytz==2015.2
six==1.9.0
wsgiref==0.1.2

In this case, we see that even though we simply installed matplotlib, many other packages are also installed. Apart from wsgiref, which is used by pip itself, these are required dependencies of matplotlib which have been automatically installed.

When transferring a project from an environment (possibly a virtual environment) to another, the receiving environment needs to have all the necessary packages installed (in the same version as in the original environment) in order to be sure that the code can be properly run. This can be problematic as two different environments might not contain the same packages, and, worse, might contain different versions of the same package. This can lead to conflicts or unexpected behaviors in the execution of the program.

In order to avoid this problem, pip freeze can be used to save a copy of the current environment configuration. The command will save the output of the command to the file requirements.txt:

$ pip freeze > requirements.txt

In a new environment, this file can be used to install all the required libraries. Simply run:

$ pip install -r requirements.txt

All the necessary packages will automatically be installed in their specified version. That way, we ensure that the environment where the code is used is always the same. This is a good practice to have a virtual environment and a requirements.txt file for every project you are developing. Therefore, before installing the required packages, it is advised that you first create a new virtual environment to avoid conflicts with other projects.

The overall workflow from one machine to another is therefore:

  • On machine 1:

    $ mkvirtualenv env1
    (env1)$ pip install matplotlib
    (env1)$ pip freeze > requirements.txt
    
  • On machine 2:

    $ mkvirtualenv env2
    (env2)$ pip install -r requirements.txt