1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
| import seaborn as sns import matplotlib.pyplot as plt import pandas as pd import numpy as np
np.random.seed(42) df = pd.DataFrame({ '日期': pd.date_range('2024-01-01', periods=365, freq='D'), '销售额': np.random.normal(5000, 1000, 365) + np.linspace(0, 2000, 365), '客户数': np.random.poisson(50, 365), '客单价': np.random.normal(100, 20, 365), '地区': np.random.choice(['华北', '华东', '华南', '西南'], 365), '渠道': np.random.choice(['线上', '线下'], 365, p=[0.6, 0.4]) })
fig = plt.figure(figsize=(16, 12))
ax1 = plt.subplot(2, 3, 1) df_monthly = df.groupby(df['日期'].dt.to_period('M'))['销售额'].sum() df_monthly.plot(kind='line', ax=ax1, marker='o') ax1.set_title('月度销售额趋势') ax1.grid(True, alpha=0.3)
ax2 = plt.subplot(2, 3, 2) sns.boxplot(data=df, x='地区', y='销售额', ax=ax2, palette='Set2') ax2.set_title('各地区销售额分布')
ax3 = plt.subplot(2, 3, 3) channel_sales = df.groupby('渠道')['销售额'].sum() ax3.pie(channel_sales, labels=channel_sales.index, autopct='%1.1f%%', colors=['skyblue', 'lightcoral']) ax3.set_title('销售渠道占比')
ax4 = plt.subplot(2, 3, 4) sns.histplot(df['销售额'], kde=True, ax=ax4, color='green') ax4.set_title('销售额分布')
ax5 = plt.subplot(2, 3, 5) corr_data = df[['销售额', '客户数', '客单价']].corr() sns.heatmap(corr_data, annot=True, cmap='RdYlBu_r', ax=ax5, center=0) ax5.set_title('指标相关性')
ax6 = plt.subplot(2, 3, 6) sns.scatterplot(data=df, x='客户数', y='销售额', hue='渠道', ax=ax6, alpha=0.6) ax6.set_title('客户数 vs 销售额')
plt.suptitle('2024年销售数据分析报告', fontsize=16, fontweight='bold', y=0.98) plt.tight_layout(rect=[0, 0, 1, 0.96]) plt.savefig('sales_analysis_report.png', dpi=300, bbox_inches='tight') plt.show()
print("分析报告已生成!")
|