Matplotlib

給 R 使用者的 Python

Daniel Chen

Instructor

Matplotlib 繪圖

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

給 R 使用者的 Python

Matplotlib 散佈圖

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

給 R 使用者的 Python

美化圖形

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()

給 R 使用者的 Python

旋轉座標刻度

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()

給 R 使用者的 Python

matplotlib 圖形構成

給 R 使用者的 Python

matplotlib 圖形構成 2

給 R 使用者的 Python

Figure 與 Axes

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

給 R 使用者的 Python

多個 Axes

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

給 R 使用者的 Python

請記住

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

plot

給 R 使用者的 Python

清空圖形

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

給 R 使用者的 Python

一起來練習吧!

給 R 使用者的 Python

Preparing Video For Download...