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 61 62
| import matplotlib.pyplot as plt import matplotlib.gridspec as gridspec import numpy as np
plt.rcParams['font.sans-serif'] = ['Arial Unicode MS'] plt.rcParams['axes.unicode_minus'] = False
fig = plt.figure(figsize=(20, 14)) gs = gridspec.GridSpec(3, 4, figure=fig, hspace=0.35, wspace=0.3)
months = [f'{m}月' for m in range(1, 13)] revenue = [120, 135, 148, 160, 155, 170, 185, 190, 175, 195, 210, 230] cost = [80, 85, 90, 95, 92, 98, 105, 100, 95, 102, 110, 115] profit = [r-c for r, c in zip(revenue, cost)] categories = ['电子产品', '服装', '食品', '家居', '美妆'] cat_sales = [450, 380, 320, 280, 210]
ax1 = fig.add_subplot(gs[0, :3]) ax1.fill_between(months, revenue, alpha=0.3, color='#4CAF50') ax1.plot(months, revenue, 'o-', color='#4CAF50', linewidth=2, label='收入') ax1.plot(months, cost, 's--', color='#FF5722', linewidth=2, label='成本') ax1.set_title('月度收入与成本趋势', fontsize=16, fontweight='bold') ax1.legend(fontsize=12) ax1.set_ylabel('金额(万元)')
ax2 = fig.add_subplot(gs[0, 3]) profit_rate = [p/r*100 for p, r in zip(profit, revenue)] colors = ['#4CAF50' if p > 30 else '#FF9800' for p in profit_rate] ax2.barh(months, profit_rate, color=colors, height=0.6) ax2.set_title('利润率(%)', fontsize=14)
ax3 = fig.add_subplot(gs[1, :2]) ax3.pie(cat_sales, labels=categories, autopct='%1.1f%%', colors=plt.cm.Set3(np.linspace(0, 1, 5))) ax3.set_title('品类销售占比', fontsize=14)
ax4 = fig.add_subplot(gs[1, 2:]) ax4.bar(categories, cat_sales, color=plt.cm.Set3(np.linspace(0, 1, 5))) ax4.set_title('品类销售排行', fontsize=14) ax4.set_ylabel('销售额(万元)')
ax5 = fig.add_subplot(gs[2, :]) ax5.axis('off') metrics = [ f'全年总收入: ¥{sum(revenue):,}万', f'全年总利润: ¥{sum(profit):,}万', f'平均利润率: {np.mean(profit_rate):.1f}%', f'增长趋势: +{((revenue[-1]-revenue[0])/revenue[0]*100):.1f}%' ] for i, m in enumerate(metrics): ax5.text(i*0.25+0.05, 0.5, m, fontsize=16, fontweight='bold', transform=ax5.transAxes, ha='left', va='center', bbox=dict(boxstyle='round,pad=0.5', facecolor='#E3F2FD', alpha=0.8))
plt.savefig('annual_dashboard.png', dpi=150, bbox_inches='tight') plt.show()
|