Skip to content
Data Visualization in Python
%%capture
!pip install vega-datasets altair folium
Static Plots
Matplotlib
Matplotlib is a comprehensive library for creating static, animated, and interactive visualizations in Python. Matplotlib makes easy things easy and hard things possible. It is the first data visualization library that every Python Data Scientist encounters.
import matplotlib as mpl
import matplotlib.pyplot as plt
%config InlineBackend.figure_format = 'retina'
plt.rcParams["figure.figsize"] = [12, 4]
plt.style.use(['seaborn-darkgrid'])
import numpy as np
x = np.linspace(0, 10, 100)
plt.plot(x, np.sin(x), color='crimson', linestyle="-.")
plt.plot(x, np.cos(x));
Matlab Style Interface
# Create a figure to house the plots
plt.figure()
# Create a panel and add the first plot
plt.subplot(2, 1, 1)
plt.plot(x, np.sin(x))
# Create a panel and add the second plot
plt.subplot(2, 1, 2)
plt.plot(x, np.cos(x));
Object-Oriented Interface
# Create a grid of plots and an array of axis objects
fig, ax = plt.subplots(2)
# Use the plot() method to add the two plots
ax[0].plot(x, np.sin(x))
ax[1].plot(x, np.cos(x));
import pandas as pd
happiness_rankings_csv = "https://raw.githubusercontent.com/Nothingaholic/Python-Cheat-Sheet/master/matplotlib/happiness_rank.csv"
happiness_rankings = pd.read_csv(happiness_rankings_csv)
happiness_rankings.head()
fig, ax = plt.subplots(figsize = (10,5))
x = happiness_rankings['GDP']
y = happiness_rankings['Score']
plt.scatter(x,y)
plt.title('GDP vs Happiness Score')
plt.xlabel('GDP')
plt.ylabel('Score');
Seaborn