代码截图恢复,原数据缺失 · 来源与修改说明 · 下载完整代码
1 """平滑极坐标面积叠加 2 Restored/adapted from supplied screenshots [118, 119, 120]. 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 from scipy.interpolate import make_interp_spline 18 BASE_RADIUS=50;SMOOTHING_POINTS=300 19 COLORS=['#529a9f','#d36a87','#ee8d5e'] 20 read_custom_data=False 21 if read_custom_data: 22 df=pd.read_csv('data.csv') 23 else: 24 # Synthetic cyclic series: the original data.csv is not supplied. 25 angles=np.arange(0,360,30);theta=np.deg2rad(angles) 26 df=pd.DataFrame({'angle':angles}) 27 for i in range(3): 28 df[f'value_{i+1}']=14+8*np.sin(theta*2+i*.9)+4*np.cos(theta*3-i*.7) 29 series_cols=[col for col in df.columns if 'value' in col] 30 original_angles=df['angle'].values 31 if len(original_angles)<4 or np.any(np.diff(original_angles)<=0) or original_angles[0]!=0 or original_angles[-1]>=360: 32 raise ValueError('Angles must start at zero, strictly increase, and exclude 360 degrees') 33 original_theta=np.deg2rad(original_angles) 34 fig,ax=plt.subplots(figsize=(8,6),dpi=150,subplot_kw={'projection':'polar'}) 35 ax.grid(color='gray',linestyle=':',linewidth=.8,alpha=.5) 36 ax.spines['polar'].set_visible(False) 37 LIMIT_RADIUS=BASE_RADIUS+df[series_cols].max().max()*2 38 for idx,col in enumerate(series_cols): 39 values=df[col].values 40 theta_closed=np.concatenate([original_theta,[original_theta[0]+2*np.pi]]) 41 values_closed=np.concatenate([values,[values[0]]]) 42 # Periodic boundary also matches slope at 0/360 (original only duplicated endpoint). 43 spl=make_interp_spline(theta_closed,values_closed,k=3,bc_type='periodic') 44 theta_smooth=np.linspace(0,2*np.pi,SMOOTHING_POINTS) 45 values_smooth=np.maximum(spl(theta_smooth),0) 46 r_values=values_smooth+BASE_RADIUS;color=COLORS[idx%len(COLORS)] 47 ax.fill_between(theta_smooth,BASE_RADIUS,r_values,color=color,alpha=.3,label=f'数据系列{idx+1}') 48 ax.plot(theta_smooth,r_values,color=color,linewidth=2,alpha=.9) 49 ax.set_ylim(0,LIMIT_RADIUS) 50 ax.set_yticks(np.linspace(BASE_RADIUS,LIMIT_RADIUS,4)[1:]);ax.set_yticklabels([]) 51 labels_deg=np.arange(0,360,45) 52 ax.set_xticks(np.deg2rad(labels_deg),[f'{d}°' for d in labels_deg],fontsize=9,color='#555555') 53 ax.tick_params(axis='x',pad=12) 54 # Cover the center using public polar coordinates rather than unused private-transform Circle. 55 ax.fill_between(np.linspace(0,2*np.pi,300),0,BASE_RADIUS,color='white',zorder=10) 56 ax.legend(loc='upper center',bbox_to_anchor=(.5,-.1),frameon=False,fontsize=10,ncol=3) 57 plt.tight_layout();plt.show() 58