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

Customizing matplotlib's parameters per project


This recipe explains where the various configuration files are that matplotlib uses, and why we want to use one or the other. Also, we explain what is in these configuration files.

Getting ready

If you don't want to configure matplotlib as the first step in your code every time you use it (as we did in the previous recipe), this recipe will explain how to have different default configurations of matplotlib for different projects. This way your code will not be cluttered with configuration data and, moreover, you can easily share configuration templates with your co-workers or even among other projects.

How to do it...

If you have a working project that always uses the same settings for certain parameters in matplotlib, you probably don't want to set them every time you want to add a new graph code. Instead, what you want is a permanent file, outside of your code, which sets defaults for matplotlib parameters.

matplotlib supports this via its matplotlibrc configuration file that contains most of the changeable properties of matplotlib.

How it works...

There are three different places where this file can reside and its location defines its usage. They are:

  • Current working directory: This is where your code runs from. This is the place to customize matplotlib just for your current directory that might contain your current project code. File is named matplotlibrc.

  • Per user .matplotlib/matplotlibrc: This is usually in user's $HOME directory (under Windows, this is your Documents and Settings directory). You can find out where your configuration directory is using the matplotlib.get_configdir() command. Check the next command.

  • Per installation configuration file: This is usually in your python site-packages. This is a system-wide configuration, but it will get overwritten every time you reinstall matplotlib; so it is better to use per user configuration file for more persistent customizations. Best usage so far for me was to use this as a default template if I mess up my user's configuration file or if I need fresh configuration to customize for a different project.

The following one-liner will print the location of your configuration directory and can be run from shell.

$ python -c 'import matplotlib as mpl; print mpl.get_configdir()'

The configuration file contains settings for:

  • axes: Deals with face and edge color, tick sizes, and grid display.

  • backend: Sets the target output: TkAgg and GTKAgg.

  • figure: Deals with dpi, edge color, figure size, and subplot settings.

  • font: Looks at font families, font size, and style settings.

  • grid: Deals with grid color and line settings.

  • legend: Specifies how legends and text inside will be displayed.

  • lines: It checks for line (color, style, width, and so on) and markers settings.

  • patch: Patches are graphical objects that fill 2D space, such as polygons and circles; set linewidth, color, antialiasing, and so on.

  • savefig: There are separate settings for saved figures. For example, to make rendered files with a white background.

  • text: This looks for text color, how to interepret text (plain versus latex markup) and similar.

  • verbose: It checks how much information matplotlib gives during runtime: silent, helpful, debug, and debug-annoying.

  • xticks and yticks: These set the color, size, direction, and labelsize for major and minor ticks for x and y axes.

There's more...

If you are interested in more details for every mentioned setting (and some that we did not mention here), the best place to go is the website of matplotlib project where there is up-to-date API documentation. If it doesn't help, user and development lists are always good places to leave questions. See the back of this book for useful online resources.