2015-07-09 80 views
0

我一直在努力這一段時間,希望這是一個簡單的疏忽,它通常是。填充文本不對齊

我只想將兩行文本對齊到列中。我正在嘗試使用.format字符串方法。

import matplotlib.pyplot as plt 

bank_percentage=[.33,.25,.08,.15,.07,.08,.11,.3] 
percent_text1 = "B0:{:>10.2%} B1:{:>10.2%} B2:{:>10.2%} B3:{:>10.2%}".format(
       bank_percentage[0],bank_percentage[1],bank_percentage[2],bank_percentage[3]) 
percent_text2 = "B4:{:>10.2%} B5:{:>10.2%} B6:{:>10.2%} B7:{:>10.2%}".format(
       bank_percentage[4],bank_percentage[5],bank_percentage[6],bank_percentage[7]) 
fig5= plt.figure(5) 
ax = fig5.add_subplot(111) 
plt.plot([1,2,3],[1,2,3]) 
plt.xlabel('Time (ms)') 
plt.ylabel('Number of active banks') 
plt.title('Active Banks') 
ax.text(0.02,0.01, percent_text2, transform=ax.transAxes, fontsize=12, va='bottom', ha='left', 
     backgroundcolor='black', color='white',weight='bold') 
ax.text(0.02,.05, percent_text1, transform=ax.transAxes, fontsize=12, va='bottom', ha='left', 
     backgroundcolor='black', color='white',weight='bold') 

繪圖顯示每個元素都進一步不對齊。

回答

2

您正確設置了文本的格式,但matpllotlib使用了襯線字體,因此空格不顯示您的期望。只需添加以下內容即可使用:

ax.text(stuff, family='monospace')