Visualization for Data Science in Python

Getting started & overview.

Choosing a library :

There seems to be a number of libraries out there, our first objective will be to try out a few of them to get a feel on how easy or hard they are, our first dataset will simply be a labeled time series:

Dataset : (Coindesks Bitcoin's yearly close price) :
https://github.com/KenoLeon/PythonDev/blob/master/visualization/coindeskBTC.csv
SourceFiles: 
https://github.com/KenoLeon/PythonDev/tree/master/visualization

1. Matplotlib:

#Import Libraries
import matplotlib.pyplot as plt
import pandas as pd
# Read DataFrame ( and parse axis dates)
df = pd.read_csv("visualization/coindeskBTC.csv", usecols=['Date','BTC Close'], parse_dates=['Date'])
# Use Date as index
df.set_index('Date',inplace=True)
# Plot & show
plt.plot(df['BTC Close'])
plt.show()

Super straightforward no frills out of the box solution, you can add options and formatting galore.

Note: While not a pandas tutorial, formatting and reading is easier with a data frame. 

2. Seaborn:

#Import Libraries
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
# Read DataFrame,parse and set axis dates)
df = pd.read_csv("visualization/coindeskBTC.csv", usecols=['Date','BTC Close'], parse_dates=['Date'])
df.set_index('Date',inplace=True)
# Seaborn Style:
sns.set(style="darkgrid")
# Same as with matplotlib:
plt.plot(df['BTC Close'])
plt.show()
# Notice it is basically the same as the matplotlib example, we just imported seaborn.

Rather than a different type of library, seaborn rides on top of matplotlib and adds both style and a number of data science & statistics features in API form.

3. Bokeh:

#Import Libraries
import pandas as pd
from bokeh.plotting import figure, output_file, show
# Read DataFrame and parse x axis dates
df = pd.read_csv("visualization/coindeskBTC.csv", usecols=['Date','BTC Close'], parse_dates=['Date'])
# Notice we don't need to set the index, bokeh takes care of that
# Create a file, this will save your plot in html format
output_file("BTC_Price_Chart.html")
# create a new plot with a datetime axis type
p = figure(plot_width=1200, plot_height=800, x_axis_type="datetime")
# Add the series with some minimal styling
p.line(df['Date'], df['BTC Close'], color='cornflowerblue', line_width= 3, alpha=1)
show(p)

Bokeh is different in a good way, it creates an html friendly interactive display you can embed, zoom and pan, but you can also export static images, it comes with varied and well documented API for all kinds of displays, and works nicely with dataframes.

Styling for instance, can be done by adding a few lines:

p.xgrid.grid_line_color = "purple"
p.xgrid.grid_line_alpha = 0.4
p.ygrid.grid_line_color = "purple"
p.ygrid.grid_line_alpha = 0.6
p.ygrid.grid_line_dash = [6, 4]
p.background_fill_color = "Darkgrey"
p.background_fill_alpha = 0.2

Conclusion:

While there are a few other libraries and solutions for plotting your data in python, these 3 I think are a good place to start, in general I would recommend bokeh ( for ease of use) , seaborn (if you need a quick statistics focused solution ) and if nothing works matplotlib.

EDIT :

Other Libraries :

4. Plotly:

# Import Libraries
import plotly as py
import plotly.graph_objs as go
import pandas as pd
# Read DataFrame
df = pd.read_csv("visualization/coindeskBTC.csv", usecols=['Date','BTC Close'], parse_dates=['Date'])
# Basic plotly plot
data = [go.Scatter(
x=df.Date,
y=df['BTC Close'])]
py.offline.plot(data)

Plotly is a whole ecosystem devoted to high quality graphical visualizations and they provide a free Python API ( it is part of a commercial operation though ), there’s a heavy cloud component ( but you can also work offline )and out of the box it is easy to work with and the results are good, like bokeh you get a nice interactive html page, highly recommended as long as you can deal with it not being 100% open source.

Keno.

About the Author :

Born Eugenio Noyola Leon (Keno) I am a Designer, Developer/programmer, Artist and Inventor, currently living in Mexico City, you can find me at: www.k3no.com