自定义时间序列图

用 Python 可视化时间序列数据

Thomas Vincent

Head of Data Science, Getty Images

切片时间序列数据

discoveries['1960':'1970']
discoveries['1950-01':'1950-12']
discoveries['1960-01-01':'1960-01-15']
用 Python 可视化时间序列数据

绘制时间序列数据的子集

import matplotlib.pyplot as plt
plt.style.use('fivethirtyeight')
df_subset = discoveries['1960':'1970']
ax = df_subset.plot(color='blue', fontsize=14)
plt.show()

时间序列子集示例

用 Python 可视化时间序列数据

添加标记线

ax.axvline(x='1969-01-01', 
           color='red', 
           linestyle='--')
ax.axhline(y=100, 
           color='green', 
           linestyle='--')
用 Python 可视化时间序列数据

使用标记线:完整代码

ax = discoveries.plot(color='blue')
ax.set_xlabel('Date')
ax.set_ylabel('Number of great discoveries')

ax.axvline('1969-01-01', color='red', linestyle='--') ax.axhline(4, color='green', linestyle='--')

为图添加标记线

用 Python 可视化时间序列数据

高亮感兴趣的区域

ax.axvspan('1964-01-01', '1968-01-01', 
           color='red', alpha=0.5)
ax.axhspan(8, 6, color='green',
           alpha=0.2)
用 Python 可视化时间序列数据

高亮感兴趣区域:完整代码

ax = discoveries.plot(color='blue')
ax.set_xlabel('Date')
ax.set_ylabel('Number of great discoveries')
ax.axvspan('1964-01-01', '1968-01-01', color='red',
alpha=0.3)
ax.axhspan(8, 6, color='green', alpha=0.3)

在图中添加感兴趣区域

用 Python 可视化时间序列数据

让我们练习吧!

用 Python 可视化时间序列数据

Preparing Video For Download...