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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
| import matplotlib.pyplot as plt import seaborn as sns
class PriceVisualizer: def __init__(self, monitor): self.monitor = monitor plt.style.use('seaborn-v0_8') def plot_price_trends(self): """价格趋势对比图""" fig, axes = plt.subplots(2, 2, figsize=(16, 10)) axes = axes.flatten() products = self.monitor.df['产品'].unique() for idx, product in enumerate(products[:4]): ax = axes[idx] product_df = self.monitor.df[self.monitor.df['产品'] == product] our_price = product_df['我方价格'].iloc[0] for competitor in product_df['竞对'].unique(): comp_df = product_df[product_df['竞对'] == competitor] ax.plot(comp_df['日期'], comp_df['价格'], label=competitor, alpha=0.7, linewidth=1.5) ax.axhline(y=our_price, color='red', linestyle='--', linewidth=2, label='Our Price') ax.set_title(f'{product}', fontsize=12, fontweight='bold') ax.set_ylabel('Price (CNY)') ax.legend(fontsize=8) ax.grid(True, alpha=0.3) plt.suptitle('Competitor Price Trends by Product', fontsize=16, fontweight='bold') plt.tight_layout() plt.savefig('price_trends.png', dpi=300, bbox_inches='tight') plt.show() def plot_price_distribution(self): """价格分布箱线图""" plt.figure(figsize=(12, 6)) plot_data = [] labels = [] for product in self.monitor.df['产品'].unique(): product_df = self.monitor.df[self.monitor.df['产品'] == product] for competitor in product_df['竞对'].unique(): comp_prices = product_df[product_df['竞对'] == competitor]['价格'].values plot_data.append(comp_prices) labels.append(f"{product}\n{competitor}") bp = plt.boxplot(plot_data, labels=labels, patch_artist=True) colors = plt.cm.Set3(np.linspace(0, 1, len(bp['boxes']))) for patch, color in zip(bp['boxes'], colors): patch.set_facecolor(color) plt.xticks(rotation=45, ha='right') plt.ylabel('Price (CNY)') plt.title('Price Distribution by Product & Competitor', fontsize=14, fontweight='bold') plt.grid(True, alpha=0.3, axis='y') plt.tight_layout() plt.savefig('price_distribution.png', dpi=300, bbox_inches='tight') plt.show() def plot_competitive_matrix(self): """竞争力矩阵热力图""" matrix = self.monitor.df.groupby(['产品', '竞对'])['价差率'].mean().unstack() plt.figure(figsize=(10, 6)) sns.heatmap(matrix, annot=True, fmt='.1f', cmap='RdYlGn_r', center=0, cbar_kws={'label': 'Price Difference %'}) plt.title('Competitive Price Matrix (% vs Our Price)', fontsize=14, fontweight='bold') plt.xlabel('Competitor') plt.ylabel('Product') plt.tight_layout() plt.savefig('competitive_matrix.png', dpi=300, bbox_inches='tight') plt.show() def plot_promotion_calendar(self): """促销日历图""" daily_promo = self.monitor.df.groupby('日期')['是否促销'].sum() plt.figure(figsize=(14, 4)) plt.bar(daily_promo.index, daily_promo.values, color='coral', alpha=0.7, width=0.8) plt.title('Daily Promotion Count (All Competitors)', fontsize=14, fontweight='bold') plt.xlabel('Date') plt.ylabel('Number of Promotions') plt.grid(True, alpha=0.3, axis='y') plt.tight_layout() plt.savefig('promotion_calendar.png', dpi=300, bbox_inches='tight') plt.show()
|