|
通过使用不同的颜色表示水平轴和线之间的区域,面积图不仅强调峰值和低谷值,同时还强调它们持续的时间:即峰值持续时间越长,面积越大。
- import numpy as np
- import pandas as pd
-
- # Prepare Data
- df = pd.read_csv("https://github.com/selva86/datasets/raw/master/economics.csv", parse_dates=['date']).head(100)
- x = np.arange(df.shape[0])
- y_returns = (df.psavert.diff().fillna(0)/df.psavert.shift(1)).fillna(0) * 100
-
- # Plot
- plt.figure(figsize=(16,10), dpi= 80)
- plt.fill_between(x[1:], y_returns[1:], 0, where=y_returns[1:] >= 0, facecolor='green', interpolate=True, alpha=0.7)
- plt.fill_between(x[1:], y_returns[1:], 0, where=y_returns[1:] <= 0, facecolor='red', interpolate=True, alpha=0.7)
-
- # Annotate
- plt.annotate('Peak n1975', xy=(94.0, 21.0), xytext=(88.0, 28),
- bbox=dict(boxstyle='square', fc='firebrick'),
- arrowprops=dict(facecolor='steelblue', shrink=0.05), fontsize=15, color='white')
-
-
- # Decorations
- xtickvals = [str(m)[:3].upper()+"-"+str(y) for y,m in zip(df.date.dt.year, df.date.dt.month_name())]
- plt.gca().set_xticks(x[::6])
- plt.gca().set_xticklabels(xtickvals[::6], rotation=90, fontdict={'horizontalalignment': 'center', 'verticalalignment': 'center_baseline'})
- plt.ylim(-35,35)
- plt.xlim(1,100)
- plt.title("Month Economics Return %", fontsize=22)
- plt.ylabel('Monthly returns %')
- plt.grid(alpha=0.5)
- plt.show()

3. 密度图(Density Plot)
(编辑:PHP编程网 - 金华站长网)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|