為時間序列加上註解

Matplotlib 資料視覺化入門

Ariel Rokem

Data Scientist

時間序列資料

Matplotlib 資料視覺化入門

註解(Annotation)

fig, ax = plt.subplots()
plot_timeseries(ax, climate_change.index, climate_change['co2'],
               'blue', 'Time', 'CO2 (ppm)')
ax2 = ax.twinx()
plot_timeseries(ax2, climate_change.index, 
                climate_change['relative_temp'],
               'red', 'Time', 'Relative temperature (Celsius)')

ax2.annotate(">1 degree", xy=(pd.Timestamp("2015-10-06"), 1))
plt.show()

Matplotlib 資料視覺化入門

調整文字位置

ax2.annotate(">1 degree",
             xy=(pd.Timestamp('2015-10-06'), 1),
             xytext=(pd.Timestamp('2008-10-06'), -0.2))

Matplotlib 資料視覺化入門

在註解加上箭頭

ax2.annotate(">1 degree",
             xy=(pd.Timestamp('2015-10-06'), 1),
             xytext=(pd.Timestamp('2008-10-06'), -0.2),
             arrowprops={})

Matplotlib 資料視覺化入門

自訂箭頭樣式

ax2.annotate(">1 degree",
             xy=(pd.Timestamp('2015-10-06'), 1),
             xytext=(pd.Timestamp('2008-10-06'), -0.2),
             arrowprops={"arrowstyle":"->", "color":"gray"})

Matplotlib 資料視覺化入門

自訂註解

Matplotlib 資料視覺化入門

一起來練習吧!

Matplotlib 資料視覺化入門

Preparing Video For Download...