matplotlib - 如何将格式应用于 Matplotlib 表中的单元格内容

标签 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, 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()

结果:

enter image description here

关于matplotlib - 如何将格式应用于 Matplotlib 表中的单元格内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15215173/

相关文章:

python - 绘制图形并退出,但保持图形窗口打开

python - 将自定义错误栏添加到seaborn FacetGrid,确认色调和分类x顺序

r - R中如何按国家/地区格式化日期?

model - 从头开始生成3D模型的文件格式?

excel - 将日期格式更改为 yyyy-mm-dd

python - 更改基本条形图的宽度

python - 带有热图调色板的条形图

excel - 使用单元格值作为变量在 Excel 中插入方程(方程编辑器)

html - 表格单元格内的 CSS div 位置/大小

iOS 网页错误通过蜂窝数据而不是通过 Wifi?最近更改了 AT&T 蜂窝网络?