【问题标题】:Padded text doesn't align填充文本不对齐
【发布时间】:2015-09-28 13:07:39
【问题描述】:

我已经为此苦苦挣扎了一段时间,希望这是一个简单的疏忽,通常是这样。

我只想将两行文本对齐到列中。我正在尝试使用 .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')

绘制此图表明每个元素都进一步失准。

【问题讨论】:

    标签: python text matplotlib string-formatting padding


    【解决方案1】:

    您正确格式化了文本,但 matpllotlib 使用衬线字体,因此空格不会按您的预期显示。只需添加以下内容即可:

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

    【讨论】:

      猜你喜欢
      • 2011-09-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-28
      • 2011-07-25
      • 2018-03-04
      • 2017-03-29
      • 2010-12-13
      相关资源
      最近更新 更多