早上好
我一直在努力将明显的“色相”标记为一个海洋原型。 如果x轴上每个位置都有数据点,我所拥有的代码将起作用,但是由于第一个rect位置显示一个NaN值,因此我从(height = rect.get_height())中收到错误消息。我不确定如何“偏移”到有值的下一个矩形。
以下是示例df:
Procedure plan_cat Region Fees Referrals
0 9 InNetwork a 0.178155 44
1 92 InNetwork a 1.491056 1
2 9 InNetwork b 0.249290 45
3 92 InNetwork b 1.491000 2
4 9 InNetwork c 0.250906 105
5 92 InNetwork c 1.490889 2
6 9 InNetwork d 0.316211 63
7 92 InNetwork d 1.491000 3
8 9 InNetwork e 0.300000 29
9 92 InNetwork e 1.491000 4
10 9 InNetwork f 0.300000 24
11 92 InNetwork f 1.491000 5
12 9 Specialist a 0.433522 6
13 89 Specialist a 1.481618 6
14 9 Specialist b 0.594962 2
15 89 Specialist b 1.487511 9
16 9 Specialist c 0.879057 6
17 89 Specialist c 1.484376 25
18 9 Specialist d 1.153279 8
19 89 Specialist d 1.467492 22
20 9 Specialist e 1.009909 6
21 89 Specialist e 1.495741 11
22 92 Specialist e 1.736217 2
23 9 Specialist f 1.568886 10
24 89 Specialist f 1.683757 22
我刚学了pd.read_clipboard技巧,用光标和ctrl + c选择完整的df,然后继续操作。
df = pd.read_clipboard()
df['plan_cat']=df['plan_cat'].astype('category')
for prod in set(df['Procedure']):
deef = df[df['Procedure']==prod]
deef.reset_index(inplace=True,drop=True)
a4_dims = (11.7, 8.27)
fig, ax = plt.subplots(figsize=a4_dims)
gr=sns.barplot( x='Region' , y='Referrals', data=deef,ci=0 , hue='plan_cat'
,ax=ax ,saturation=.6, alpha=.5,hue_order=['InNetwork','Specialist'])
gr.axes.set_title("Procedure "+str(prod),fontsize=25)
gr.set_xlabel("Region Code",fontsize=15)
gr.set_ylabel("Referrals",fontsize=15)
gr.tick_params(labelsize=10)
rects = gr.patches
for rect, label in zip(rects,deef['Fees']):
height = rect.get_height()
if np.isnan(height):
continue
gr.text(rect.get_x() + rect.get_width()/2, height+0.2,str(round(label,2)),
color='black',
ha="center",fontweight='bold')
过程9的图看起来完全按计划。
但是,如果按照步骤89缺少'InNetwork'或'Specialist'并非以正确的顺序出现,则从'a'开始,标签会乱码或被忽略。
我在循环中四处查看打印的rect.height和rect.get_x,以了解它们的位置,而为什么程序92中的1个“专家”在视觉上显示在“ e”区域中却没有意义但是rect是0吗?
无论如何,感谢您的所有帮助。
我在代码视图中的评论
for rect, label in zip(rects,deef['Fees']):
print(str(round(rect.get_x(),1))+':'+str(rect.get_height()))
-0.4:1.0
0.6:2.0
1.6:2.0
2.6:3.0
3.6:4.0
4.6:5.0
0.0:nan