3

我对 python/Matplotlib 比较陌生。我正在尝试弄清楚如何控制表格单元格中显示的小数位数。

例如; 这是一个创建表格的代码块..但我希望每个单元格中的数据只显示到小数点后两位..

from pylab import *

# Create a figure
fig1 = figure(1)
ax1_1 = fig1.add_subplot(111)

# Add a table with some numbers....
the_table = table(cellText=[[1.0000, 3.14159], [sqrt(2), log(10.0)], [exp(1.0), 123.4]],colLabels=['Col A','Col B'],loc='center')    
show()
4

1 回答 1

4

您可以使用字符串格式化程序转换您的数字以执行您想要的操作:例如,对于带有两位小数 ( )'%.2f' % your_long_number的浮点数 ( ) 。有关文档,请参阅此链接f.2

from pylab import *

# Create a figure
fig1 = figure(1)
ax1_1 = fig1.add_subplot(111)

# Add a table with some numbers....

tab = [[1.0000, 3.14159], [sqrt(2), log(10.0)], [exp(1.0), 123.4]]

# Format table numbers as string
tab_2 = [['%.2f' % j for j in i] for i in tab]

the_table_2 = table(cellText=tab_2,colLabels=['Col A','Col B'],loc='center') 

show()

结果:

在此处输入图像描述

于 2013-03-05T20:16:31.343 回答