2015-07-09 88 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')