极坐标径向柱状图
代码截图恢复与适配 · 来源与修改说明 · 下载完整代码
1 """极坐标径向柱状图
2 Restored/adapted from supplied screenshots [72, 73, 74].
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 matplotlib.patches import Patch
18 values=[20,27,16,17,16,22,24,26,30,22,18,24,17,14,23,17,20,18,14,18,21,13,14,20]
19 count=len(values);angles=np.linspace(0,2*np.pi,count,endpoint=False)
20 colors=['#9dc1c5' if v<15 else '#d7a6b3' if v>25 else '#8e93af' for v in values]
21 fig,ax=plt.subplots(figsize=(8,5),subplot_kw={'projection':'polar'},dpi=150)
22 bars=ax.bar(angles,values,width=2*np.pi/count,bottom=10,color=colors,edgecolor='#f1f5f9',linewidth=1,zorder=2)
23 for bar,angle,value in zip(bars,angles,values):
24 rotation=np.degrees(angle);flip=90<rotation<270
25 ax.text(angle,value+7,str(value),ha='right' if flip else 'left',va='center',fontsize=8,fontweight='bold',rotation=rotation-180 if flip else rotation,rotation_mode='anchor',color='white',zorder=4)
26 handles=[Patch(color=c,label=l) for c,l in zip(['#9dc1c5','#8e93af','#d7a6b3'],['低值','中值','高值'])]
27 fig.legend(handles=handles,loc='center right',frameon=False)
28 ax.set_ylim(0,44);ax.set_yticks([]);ax.set_xticks(angles,np.arange(count))
29 ax.tick_params(axis='x',labelsize=8,pad=-5)
30 ax.grid(axis='x',linestyle='--',linewidth=.5,color='#8e93af',alpha=.5,zorder=1)
31 ax.spines['polar'].set_visible(False)
32 fig.text(.67,.72,'径向柱状图',fontsize=22,fontweight='bold',color='#515a85')
33 fig.subplots_adjust(left=.03,right=.67,top=.95,bottom=.05)
34 plt.show()
35