準備圖表,與他人分享

Matplotlib 資料視覺化入門

Ariel Rokem

Data Scientist

變更圖形樣式

import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot(seattle_weather["MONTH"], seattle_weather["MLY-TAVG-NORMAL"])
ax.plot(austin_weather["MONTH"], austin_weather["MLY-TAVG-NORMAL"])
ax.set_xlabel("Time (months)")
ax.set_ylabel("Average temperature (Fahrenheit degrees)")
plt.show()

Matplotlib 資料視覺化入門

選擇樣式

plt.style.use("ggplot")

fig, ax = plt.subplots() ax.plot(seattle_weather["MONTH"], seattle_weather["MLY-TAVG-NORMAL"]) ax.plot(austin_weather["MONTH"], austin_weather["MLY-TAVG-NORMAL"]) ax.set_xlabel("Time (months)") ax.set_ylabel("Average temperature (Fahrenheit degrees)") plt.show()

Matplotlib 資料視覺化入門

回到預設樣式

plt.style.use("default")
Matplotlib 資料視覺化入門

可用樣式

Matplotlib 資料視覺化入門

「bmh」樣式

plt.style.use("bmh")

fig, ax = plt.subplots() ax.plot(seattle_weather["MONTH"], seattle_weather["MLY-TAVG-NORMAL"]) ax.plot(austin_weather["MONTH"], austin_weather["MLY-TAVG-NORMAL"]) ax.set_xlabel("Time (months)") ax.set_ylabel("Average temperature (Fahrenheit degrees)") plt.show()

Matplotlib 資料視覺化入門

Seaborn 樣式

plt.style.use("seaborn-colorblind")

fig, ax = plt.subplots() ax.plot(seattle_weather["MONTH"], seattle_weather["MLY-TAVG-NORMAL"]) ax.plot(austin_weather["MONTH"], austin_weather["MLY-TAVG-NORMAL"]) ax.set_xlabel("Time (months)") ax.set_ylabel("Average temperature (Fahrenheit degrees)") plt.show()

Matplotlib 資料視覺化入門

選擇繪圖樣式的指引

  • 深色背景通常較不易看清
  • 若顏色很重要,選擇適合色盲的選項
    • 「seaborn-colorblind」或「tableau-colorblind10」
  • 若可能需要列印,盡量少用墨水
  • 若將以黑白列印,使用「grayscale」樣式
Matplotlib 資料視覺化入門

練習為你選對樣式!

Matplotlib 資料視覺化入門

Preparing Video For Download...