环形刻度阶梯面积图

代码截图恢复与适配 · 来源与修改说明 · 下载完整代码

   1 """环形刻度阶梯面积图
   2 Restored/adapted from supplied screenshots [113, 114, 115].
   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.patheffects as path_effects
  18 bgcolor='#ebf0f5';edgecolor='#597cab'
  19 plt.rcParams.update({'figure.facecolor':bgcolor,'axes.facecolor':bgcolor,'axes.edgecolor':edgecolor,'text.color':'#597cab'})
  20 fig,ax=plt.subplots(figsize=(6,6),subplot_kw={'projection':'polar'},dpi=150)
  21 ax.set(ylim=(0,1000),xticks=[],yticks=[])
  22 radius=ax.get_rmax();length=.02*radius
  23 for i in range(0,360,30):
  24     angle=np.pi*i/180
  25     ax.plot([angle,angle],[radius,100],linewidth=.5,color='white')
  26     ax.text(angle,radius+4*length,str(i),rotation=i-90,rotation_mode='anchor',va='top',ha='center')
  27 def polar_to_cartesian(theta,radius):
  28     return np.array([radius*np.cos(theta),radius*np.sin(theta)])
  29 def cartesian_to_polar(x,y):
  30     return np.array([np.arctan2(y,x),np.sqrt(x*x+y*y)])
  31 for i in range(100,1000,100):
  32     p=cartesian_to_polar(*(polar_to_cartesian(0,i)+[0,-length]))
  33     text=ax.text(p[0],p[1],str(i),zorder=500,va='top',ha='center',size='x-small')
  34     text.set_path_effects([path_effects.Stroke(linewidth=2,foreground='white'),path_effects.Normal()])
  35 T=np.linspace(0,2*np.pi,1000)
  36 for i in range(0,1000,200):
  37     ax.fill_between(T,i,i+100,color='#597cab33',zorder=-50)
  38 ax.scatter([0],[0],20,facecolor=bgcolor,edgecolor='#597cab',zorder=1000)
  39 np.random.seed(1);n=100
  40 T=2*np.pi/n+np.linspace(0,2*np.pi,n)
  41 T[1::2]=T[0:-1:2]
  42 R=np.random.uniform(500,800,n)
  43 R[-1]=R[0];R[1:-1:2]=R[2::2]
  44 ax.fill(T,R,color='#e6a6a5',zorder=150,alpha=.3)
  45 ax.plot(T,R,color='#e6a6a5',zorder=250,linewidth=1)
  46 plt.tight_layout();plt.show()
  47