带背景圆环的极坐标散点

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

   1 """带背景圆环的极坐标散点
   2 Restored/adapted from supplied screenshots [111, 112].
   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 np.random.seed(7);n=50
  18 r1=np.random.uniform(30,80,n);theta1=np.random.uniform(0,.75*np.pi,n)
  19 r2=np.random.uniform(40,70,2*n);theta2=np.random.uniform(.5*np.pi,1.5*np.pi,2*n)
  20 r3=np.random.uniform(20,60,n);theta3=np.random.uniform(1.25*np.pi,2*np.pi,n)
  21 colors=['#e97a7a','#5595d1','#e5c679']
  22 fig,ax=plt.subplots(figsize=(8,5),dpi=150,subplot_kw={'projection':'polar'})
  23 for i,(theta,r,color) in enumerate(zip([theta1,theta2,theta3],[r1,r2,r3],colors)):
  24     ax.scatter(theta,r,s=100,c=color,alpha=.5,label=f'类别{i+1}',zorder=10)
  25 theta=np.linspace(0,2*np.pi,100)
  26 for i in range(4):
  27     ax.fill_between(theta,20*i+10,20*i+20,color=colors[0],alpha=.15,zorder=1)
  28 ax.set_title('极坐标分组散点图')
  29 ax.legend(loc='center right',bbox_to_anchor=(1.4,.5),frameon=False)
  30 ax.spines['polar'].set_visible(False)
  31 plt.tight_layout();plt.show()
  32