Vẽ chuỗi thời gian đầu tiên

Trực quan hóa dữ liệu chuỗi thời gian trong Python

Thomas Vincent

Head of Data Science, Getty Images

Thư viện Matplotlib

  • Trong Python, matplotlib là gói mạnh dùng để vẽ dữ liệu
  • Tiểu mô-đun pyplot của matplotlib thường được import với bí danh plt
import matplotlib.pyplot as plt
Trực quan hóa dữ liệu chuỗi thời gian trong Python

Vẽ dữ liệu chuỗi thời gian

Chuỗi thời gian đầu tiên của bạn

Trực quan hóa dữ liệu chuỗi thời gian trong Python

Vẽ dữ liệu chuỗi thời gian

import matplotlib.pyplot as plt
import pandas as pd

df = df.set_index('date_column')
df.plot()
plt.show()
Trực quan hóa dữ liệu chuỗi thời gian trong Python

Thêm style cho biểu đồ

plt.style.use('fivethirtyeight')
df.plot()
plt.show()
Trực quan hóa dữ liệu chuỗi thời gian trong Python

Style FiveThirtyEight

Ví dụ dùng style fivethirtyeight

Trực quan hóa dữ liệu chuỗi thời gian trong Python

Các style sheet của Matplotlib

print(plt.style.available)
['seaborn-dark-palette', 'seaborn-darkgrid', 
'"seaborn-dark"', 'seaborn-notebook', 
'seaborn-pastel', 'seaborn-white', 
'classic', 'ggplot', 'grayscale', 
'dark_background', 'seaborn-poster', 
'seaborn-muted', 'seaborn', 'bmh', 
'seaborn-paper', 'seaborn-whitegrid', 
'seaborn-bright', 'seaborn-talk', 
'fivethirtyeight', 'seaborn-colorblind', 
'seaborn-deep', 'seaborn-ticks']

Trực quan hóa dữ liệu chuỗi thời gian trong Python

Mô tả đồ thị bằng nhãn

ax = df.plot(color='blue')
ax.set_xlabel('Date')
ax.set_ylabel('The values of my Y axis')
ax.set_title('The title of my plot')
plt.show()

Gắn nhãn cho biểu đồ

Trực quan hóa dữ liệu chuỗi thời gian trong Python

Figure size, linewidth, linestyle và fontsize

ax = df.plot(figsize=(12, 5), fontsize=12,
             linewidth=3, linestyle='--')
ax.set_xlabel('Date', fontsize=16)
ax.set_ylabel('The values of my Y axis', fontsize=16)
ax.set_title('The title of my plot', fontsize=16)
plt.show()

Trực quan hóa dữ liệu chuỗi thời gian trong Python

Cùng luyện tập nào!

Trực quan hóa dữ liệu chuỗi thời gian trong Python

Preparing Video For Download...