代码截图恢复,原数据缺失 · 来源与修改说明 · 下载完整代码
1 """多型号价格堆叠面积 2 Restored/adapted from supplied screenshots [49, 50, 51]. 3 Source: 小明的代码美学 (as shown in supplied screenshots). 4 This is a runnable restoration, not a byte-for-byte original source file. 5 See SOURCE.md for missing inputs and documented corrections. 6 """ 7 import numpy as np 8 import pandas as pd 9 import matplotlib.pyplot as plt 10 from matplotlib.colors import LinearSegmentedColormap, Normalize, to_hex 11 from matplotlib.cm import ScalarMappable 12 from pathlib import Path 13 # Missing .mplstyle files are replaced only by explicit, portable display defaults. 14 plt.rcParams.update({'font.sans-serif':['Microsoft YaHei','DejaVu Sans'], 15 'axes.unicode_minus':False,'figure.facecolor':'white','axes.facecolor':'white', 16 'axes.spines.top':False,'axes.spines.right':False,'font.size':9}) 17 def load_data(file_path): 18 return pd.read_csv(file_path) 19 read_custom_data=False 20 if read_custom_data: 21 data=load_data('airplane_price_dataset.csv') 22 else: 23 # The archive does not include aircraft price data. This is a synthetic schema demo. 24 rng=np.random.default_rng(48) 25 models=['Boeing 777','Boeing 737','Airbus A350','Airbus A330','Bombardier CRJ900','Cessna 172'] 26 records=[] 27 for i,model in enumerate(models): 28 for age in range(46): 29 for k in range(5): 30 price=(4.8e8-i*5.5e7)*(.4+.6*np.exp(-age/12))*rng.uniform(.95,1.05) 31 records.append((model,age,price)) 32 data=pd.DataFrame(records,columns=['Model','Yas','Fiyat ($)']) 33 plt.rcParams.update({'axes.facecolor':'#f1f5f9','axes.edgecolor':'white','axes.labelcolor':'#515a85','text.color':'#515a85','xtick.color':'#515a85','ytick.color':'#515a85'}) 34 def create_stacked_area_chart(ax,data): 35 age_groups=data.groupby(['Model','Yas'])['Fiyat ($)'].mean().reset_index() 36 models=age_groups['Model'].unique() 37 colors=['#214e81','#6983a2','#8d9eb2','#b59fb1','#dc9fb0','#c2768b'] 38 ages=np.sort(age_groups['Yas'].unique()) 39 y_list=[] 40 for model in models: 41 model_data=age_groups[age_groups.Model==model] 42 y_values=model_data.set_index('Yas').reindex(ages)['Fiyat ($)'].values 43 if np.isnan(y_values).any():raise ValueError('Missing age/model cells need explicit treatment; not zero filling') 44 y_list.append((model,y_values)) 45 y_list.sort(key=lambda x:np.sum(x[1]),reverse=True) 46 bottom=np.zeros(len(ages)) 47 for i,(model,y_values) in enumerate(y_list): 48 ax.fill_between(ages,bottom,bottom+y_values,label=model,color=colors[i],alpha=.6,edgecolor='white',linewidth=1) 49 bottom+=y_values 50 ax.set(xlabel='使用年限(年)',ylabel='各型号均价的累计值($)',xlim=(ages.min(),ages.max()),ylim=(0,max(2e9,bottom.max()*1.1))) 51 ax.legend(loc='upper right',bbox_to_anchor=(.98,.98),ncol=2,fontsize=8) 52 fig,ax=plt.subplots(figsize=(8,5),dpi=150) 53 create_stacked_area_chart(ax,data) 54 plt.tight_layout();plt.show() 55