如何对Matplotlib表中的单元格内容应用格式

如何对Matplotlib表中的单元格内容应用格式,matplotlib,format,cell,Matplotlib,Format,Cell,我对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,

我对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()

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

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()
结果:


FYI-我使用的是Python=2.6.5、Matplotlib=1.2.0、numpy=1.7.0