Matplotlib

Python for R Users

Daniel Chen

Instructor

Matplotlib plots

import matplotlib.pyplot as plt
plt.hist(iris['sepal_length'])
plt.show()

Python for R Users

Matplotlib scatter

plt.scatter(iris['sepal_length'], iris['sepal_width'])
plt.show()

Python for R Users

Polishing up the figure

fig, ax = plt.subplots()
ax.scatter(iris['sepal_length'], iris['sepal_width'])
ax.set_title('Sepal Length')
ax.set_xlabel('Sepal Length')
ax.set_ylabel('Sepal Width')
plt.show()

Python for R Users

Rotating axis ticks

fig, ax = plt.subplots()
ax.scatter(iris['sepal_length'], iris['sepal_width'])
ax.set_title('Sepal Length')
ax.set_xlabel('Sepal Length')
ax.set_ylabel('Sepal Width')
plt.xticks(rotation=45) # rotate the x-axis ticks
plt.show()

Python for R Users

Parts of a matplotlib figure

Python for R Users

Parts of a matplotlib figure 2

Python for R Users

Figures and axes

fig, ax = plt.subplots()
ax.scatter(iris['sepal_length'], iris['sepal_width'])
plt.show()

Python for R Users

Multiple axes

fig, (ax1, ax2) = plt.subplots(1, 2)
ax1.scatter(iris['sepal_length'], iris['sepal_width'])
ax2.hist(iris['sepal_length'])
plt.show()

Python for R Users

Remember

fig, ax = plt.subplots()
sns.regplot(x='sepal_length', y='sepal_width',
            data=iris, fit_reg=False, ax=ax)
plt.show()

plot

Python for R Users

Clearing the figure

fig, (ax1, ax2) = plt.subplots(1, 2)
ax1.scatter(iris['sepal_length'], iris['sepal_width'])
ax2.hist(iris['sepal_length'])
plt.show()
plt.clf()

Python for R Users

Let's practice!

Python for R Users

Preparing Video For Download...