我对 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
对于带两位小数的 float (f
) (.2
) 例如。看这个link用于文档。
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()
结果:
关于matplotlib - 如何将格式应用于 Matplotlib 表中的单元格内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15215173/