在Python的图形中不显示每个条的大小总数?
我对在图表中显示每个条上方的数字有一个问题 这是我的dafaframe,如下所示在Python的图形中不显示每个条的大小总数?,python,pandas,axes,Python,Pandas,Axes,我对在图表中显示每个条上方的数字有一个问题 这是我的dafaframe,如下所示 ID SEX count 6 Secret Identity Male Characters 1751 3 Public Identity Male Characters 1662 1 Public Identity Female Characters 765 4 Secret Identity Female Characters 625 2 Public Identity Gende
ID SEX count
6 Secret Identity Male Characters 1751
3 Public Identity Male Characters 1662
1 Public Identity Female Characters 765
4 Secret Identity Female Characters 625
2 Public Identity Genderless Characters 11
0 Identity Unknown Male Characters 9
5 Secret Identity Genderless Characters 5
def show_values_on_bars(axs, h_v="v", space= 0.4):
def _show_on_single_plot(ax):
if h_v == "v":
for p in ax.patches:
_x = p.get_x() + p.get_width() / 2
_y = p.get_y() + p.get_height()
value = int(p.get_height())
ax.text(_x, _y, value, ha="center", fontsize=18)
elif h_v == "h":
for p in ax.patches:
_x = p.get_x() + p.get_width()
_y = p.get_y() + p.get_height() - float(space)
value = int(p.get_width())
ax.text(_x, _y, value, ha="left", fontsize=18)
if isinstance(axs, np.ndarray):
for idx, ax in np.ndenumerate(axs):
_show_on_single_plot(ax)
else:
_show_on_single_plot(axs)
graph_3 = sns.barplot(data = dc_df,
x = "ID" ,
y = "count", ax=a[1,0], hue='SEX')
show_values_on_bars(graph_3, "v", 0.3)
下面是我的代码函数,如下所示
ID SEX count
6 Secret Identity Male Characters 1751
3 Public Identity Male Characters 1662
1 Public Identity Female Characters 765
4 Secret Identity Female Characters 625
2 Public Identity Genderless Characters 11
0 Identity Unknown Male Characters 9
5 Secret Identity Genderless Characters 5
def show_values_on_bars(axs, h_v="v", space= 0.4):
def _show_on_single_plot(ax):
if h_v == "v":
for p in ax.patches:
_x = p.get_x() + p.get_width() / 2
_y = p.get_y() + p.get_height()
value = int(p.get_height())
ax.text(_x, _y, value, ha="center", fontsize=18)
elif h_v == "h":
for p in ax.patches:
_x = p.get_x() + p.get_width()
_y = p.get_y() + p.get_height() - float(space)
value = int(p.get_width())
ax.text(_x, _y, value, ha="left", fontsize=18)
if isinstance(axs, np.ndarray):
for idx, ax in np.ndenumerate(axs):
_show_on_single_plot(ax)
else:
_show_on_single_plot(axs)
graph_3 = sns.barplot(data = dc_df,
x = "ID" ,
y = "count", ax=a[1,0], hue='SEX')
show_values_on_bars(graph_3, "v", 0.3)
当我运行下面定义的代码段时,我得到一条错误消息,如下所示
ID SEX count
6 Secret Identity Male Characters 1751
3 Public Identity Male Characters 1662
1 Public Identity Female Characters 765
4 Secret Identity Female Characters 625
2 Public Identity Genderless Characters 11
0 Identity Unknown Male Characters 9
5 Secret Identity Genderless Characters 5
def show_values_on_bars(axs, h_v="v", space= 0.4):
def _show_on_single_plot(ax):
if h_v == "v":
for p in ax.patches:
_x = p.get_x() + p.get_width() / 2
_y = p.get_y() + p.get_height()
value = int(p.get_height())
ax.text(_x, _y, value, ha="center", fontsize=18)
elif h_v == "h":
for p in ax.patches:
_x = p.get_x() + p.get_width()
_y = p.get_y() + p.get_height() - float(space)
value = int(p.get_width())
ax.text(_x, _y, value, ha="left", fontsize=18)
if isinstance(axs, np.ndarray):
for idx, ax in np.ndenumerate(axs):
_show_on_single_plot(ax)
else:
_show_on_single_plot(axs)
graph_3 = sns.barplot(data = dc_df,
x = "ID" ,
y = "count", ax=a[1,0], hue='SEX')
show_values_on_bars(graph_3, "v", 0.3)
错误:value=int(p.get_height())->value错误:无法将浮点NaN转换为整数
如何修复它?请检查它,并为0赋值为
p。get_height()
为NaN
将numpy导入为np
def在_条上显示_值(axs,h_v=“v”,空格=0.4):
def_在单点图(ax)上显示:
如果h_v==“v”:
对于ax.patches中的p:
值=0
如果不是np.isnan(p.get_height()):
值=int(p.获取高度()
_x=p.get_x()+p.get_width()/2
_y=p。获取_y()+值
ax.text(x,y,value,ha=“center”,fontsize=18)
多亏了你的帮助,我得到了一张图表,但我得到了一些红色警告posx和posy应该是有限值
。如何删除它们?@TonyBrand检查我的更新。如果不是np.isnan(p.get_height()):,你可以将整个块放到中,直到ax.text()
,有效地跳过了具有NaN高度或宽度的补丁。@Ynjxsjmh如果你有任何想法,你可以看看我的另一个问题吗?以下是链接: