2013-03-05 65 views
2

我對python/Matplotlib相對較新。我正在設法解決如何控制表格單元格中顯示的小數位數。如何將格式應用於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() 
+0

僅供參考 - 我使用Python 2.6.5 =,= Matplotlib 1.2.0,numpy的= 1.7.0 – 2013-03-05 03:20:49

回答

1

您可以使用字符串格式化器做你想做的轉換你的電話號碼:'%.2f' % your_long_number例如有兩位小數的浮點數(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

相關問題