Data Viz Crash Course


In [1]:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

For Jupyter Notebooks Only


In [2]:
%matplotlib inline

For other IDES use plt.show() after your plot commands


In [3]:
x = np.arange(0, 10)

In [4]:
y = x ** 2

In [5]:
x


Out[5]:
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

In [6]:
y


Out[6]:
array([ 0,  1,  4,  9, 16, 25, 36, 49, 64, 81], dtype=int32)

In [7]:
plt.plot(x, y)


Out[7]:
[<matplotlib.lines.Line2D at 0x128bea031d0>]

In [8]:
# Visualizing the data with a specific marker
plt.plot(x, y, marker = '*')


Out[8]:
[<matplotlib.lines.Line2D at 0x128beae77f0>]

In [9]:
# Visualizing the data with a marker and linestyle
plt.plot(x, y, 'r--')


Out[9]:
[<matplotlib.lines.Line2D at 0x128beb84a90>]

In [10]:
# Visualizing the data with a marker and linestyle (with arguments description)
plt.plot(x, y, color = 'r', linestyle = '--')


Out[10]:
[<matplotlib.lines.Line2D at 0x128bec223c8>]

In [11]:
# Zooming, axis naming and title
plt.plot(x, y, 'r--')
plt.xlim(2, 4)
plt.ylim(10, 20)
plt.title("Zoomed")
plt.xlabel("X Axis")
plt.ylabel("Y Axis")


Out[11]:
<matplotlib.text.Text at 0x128beb43128>

In [12]:
mat = np.arange(0, 100).reshape(10, 10)

In [13]:
# Show image from an array
plt.imshow(mat)


Out[13]:
<matplotlib.image.AxesImage at 0x128bed50eb8>

In [14]:
mat = np.random.randint(0, 100, (10, 10))

In [15]:
plt.imshow(mat)


Out[15]:
<matplotlib.image.AxesImage at 0x128bedf1da0>

In [16]:
# Changing color map
plt.imshow(mat, cmap = 'coolwarm')
plt.colorbar()


Out[16]:
<matplotlib.colorbar.Colorbar at 0x128bee2d240>

Pandas Plotting


In [17]:
df = pd.read_csv('salaries.csv')

In [18]:
df


Out[18]:
Name Salary Age
0 John 50000 34
1 Sally 120000 45
2 Alyssa 80000 27

In [19]:
# Scatter plot
df.plot(x = 'Salary',
        y = 'Age',
        kind = 'scatter')


Out[19]:
<matplotlib.axes._subplots.AxesSubplot at 0x128beef1b38>

In [20]:
# Histogram
df.plot(x = 'Salary',
        kind = 'hist')


Out[20]:
<matplotlib.axes._subplots.AxesSubplot at 0x128bf00ac50>