python - 色调位置不一致的 Seaborn Barplot 标签
问题描述
早上好,
我一直在努力将分类“色调”标记为 seaborn barplot。如果 x 轴上的每个位置都有数据点,我的代码就可以工作,但是我从 (height = rect.get_height()) 得到一个错误,因为第一个 rect 位置显示一个 NaN 值。我不确定如何“偏移”到下一个有值的矩形。
这是样本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 'Specialist' 在视觉上显示在 '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
解决方案
推荐阅读
- graphql - 如何根据环境在我的模式中使用 GraphQLEnumType?
- react-native - ReactNative - 在屏幕之间共享 Webview 会话
- apache - Apache 2 不重写 https 流量
- java - 在 java 8 中使用正确的偏移量将 OffsetDateTime 转换为 LocalDateTime
- image-processing - 如何计算频谱的能量平衡(EB),使右手边的能量等于左手边的能量?
- elasticsearch - 仅在 Elastic Search 中获得超过指定最低分数的结果
- c++ - 将字符串向量推回二维向量
- p-value - 根据 textstat_collocations {quanteda.textstats} 函数计算的 Wald z 统计量计算 p 值是否合适?
- c# - C# 控制台应用程序(WMI 查询)突然填满内存
- mysql - 查询名字的第二大值,如果只有一个值返回null