python可视化分析(七)-绘制带误差阴影的时间序列图

分享
手机软件开发 2024-9-21 14:03:27 111 0 来自 中国
实现功能:
python绘制带误差阴影的时间序列图。
实今世码:
from scipy.stats import sem
import pandas as pd
import matplotlib.pyplot as plt
# Import Data
df_raw = pd.read_csv('F:\数据杂坛\datasets\orders_45d.csv',
                     parse_dates=['purchase_time', 'purchase_date'])
# Prepare Data: Daily Mean and SE Bands
df_mean = df_raw.groupby('purchase_date').quantity.mean()
df_se = df_raw.groupby('purchase_date').quantity.apply(sem).mul(1.96)
# Plot
plt.figure(figsize=(10, 6), dpi=80)
plt.ylabel("Daily Orders", fontsize=12)
x = [d.date().strftime('%Y-%m-%d') for d in df_mean.index]
plt.plot(x, df_mean, color="#c72e29", lw=2)
plt.fill_between(x, df_mean - df_se, df_mean + df_se, color="#f8f2e4")
# Decorations
# Lighten borders
plt.gca().spines["top"].set_alpha(0)
plt.gca().spines["bottom"].set_alpha(1)
plt.gca().spines["right"].set_alpha(0)
plt.gca().spines["left"].set_alpha(1)
plt.xticks(x[::6], [str(d) for d in x[::6]], fontsize=12)
plt.title("Daily Order Quantity of Brazilian Retail with Error Bands (95% confidence)",fontsize=14)
# Axis limits
s, e = plt.gca().get_xlim()
plt.xlim(s, e - 2)
plt.ylim(4, 10)
# Draw Horizontal Tick lines
for y in range(5, 10, 1):
    plt.hlines(y,
               xmin=s,
               xmax=e,
               colors='black',
               alpha=0.5,
               linestyles="--",
               lw=0.5)
plt.show()
实现效果:

喜好记得点赞,在看,收藏,
关注V订阅号:数据杂坛,获取数据集,完备代码和效果,将连续更新!
您需要登录后才可以回帖 登录 | 立即注册

Powered by CangBaoKu v1.0 小黑屋藏宝库It社区( 冀ICP备14008649号 )

GMT+8, 2024-11-23 00:22, Processed in 0.158778 second(s), 32 queries.© 2003-2025 cbk Team.

快速回复 返回顶部 返回列表