如何在pyplot表中四舍五入数值

1 投票
1 回答
32 浏览
提问于 2025-04-12 07:15

我用pandas的Concat函数把两个表合并成一个。然后我把这个合并后的表放进了pyplot的表格里。结果出来的表格里所有的数值都有一位小数,而原来的表格在合并之前是没有小数的。

我该怎么才能得到一个没有小数的表格呢?

我试着在合并之前把表格里的数值四舍五入,但这样并没有效果。

这是我的代码:

frames = [country_tab, country_birth_tab] 

result = pd.concat(frames, axis=1)
#print(result)

ax = plt.subplot(111, frame_on=False) # no visible frame
ax.xaxis.set_visible(False)  # hide the x axis
ax.yaxis.set_visible(False)  # hide the y axis

table2 = table(ax, result) 
table2.auto_set_font_size(False)
table2.set_fontsize(16)
table2.scale(4,4)

这是输出结果

相关问题:

1 个回答

0

对于你的情况,result = result.astype(int) 也应该能用。这个 astype(int) 是用来把你的浮点数转换成整数的。所以你的列就会变成整数类型。

而且这一行代码可以让你的代码正常运行。

result[["Column1", "Column2"]] = result[["Column1", "Column2"]].astype(int)

所以你最终的代码是:

frames = [country_tab, country_birth_tab] 

result = pd.concat(frames, axis=1)

result = result.astype(int)

ax = plt.subplot(111, frame_on=False) # no visible frame
ax.xaxis.set_visible(False)  # hide the x axis
ax.yaxis.set_visible(False)  # hide the y axis

table2 = table(ax, result) 
table2.auto_set_font_size(False)
table2.set_fontsize(16)
table2.scale(4,4)

撰写回答