阶梯折线与区间带
代码截图恢复与适配 · 来源与修改说明 · 下载完整代码
1 """阶梯折线与区间带
2 Restored/adapted from supplied screenshots [85, 86].
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 import matplotlib.ticker as ticker
18 x=np.arange(1860,2020)
19 np.random.seed(3)
20 y1=np.random.uniform(20,30,len(x))+10*np.sin(np.linspace(0,3*np.pi,len(x)))
21 y2=np.random.uniform(50,55,len(x))+5*np.sin(np.linspace(0,3*np.pi,len(x)))
22 y3=np.random.uniform(80,85,len(x))+7*np.sin(np.linspace(0,3*np.pi,len(x)))
23 # Widths are synthetic illustration intervals, not estimated confidence intervals.
24 ci1=np.random.uniform(4,5,len(x));ci2=np.random.uniform(4,7,len(x));ci3=np.random.uniform(5,9,len(x))
25 line_colors=['#8e93af','#d7a6b3','#eac890'];edge_colors=['#3f51af','#d7607e','#eab159']
26 fig,ax=plt.subplots(figsize=(10,5),dpi=150)
27 ax.set_facecolor('#f4f6f8')
28 for i,(y,ci,fill_color,line_color) in enumerate(zip([y1,y2,y3],[ci1,ci2,ci3],line_colors,edge_colors)):
29 ax.step(x,y,label=f'类别{chr(65+i)}',linewidth=1,where='mid',color=line_color,zorder=3)
30 ax.fill_between(x,y-ci,y+ci,label=f'示意区间{chr(65+i)}',alpha=.4,step='mid',color=fill_color,zorder=3 if i<2 else 2)
31 ax.text(1940,5,'低值期',ha='center',color='#3f51af',fontweight='bold')
32 ax.text(2000,80,'高值期',ha='center',color='#eab159',fontweight='bold')
33 ax.set(xlim=(1860,2020),ylim=(0,120))
34 ax.legend(loc='upper right',ncols=3,frameon=False)
35 ax.grid(axis='y',which='major',linestyle='-',linewidth=1,color='white',zorder=1)
36 ax.xaxis.set_major_locator(ticker.MultipleLocator(10))
37 ax.set_title('阶梯趋势及区间带(合成数据)',loc='left')
38 plt.tight_layout();plt.show()
39