Reputation: 1183
I've been struggling with this for a while and hoping it's a simple oversight, it usually is.
I just want to align two rows of text into columns. I'm attempting this with the .format string method.
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')
Plotting this shows that each element gets further out of alignment.
Upvotes: 1
Views: 300
Reputation: 420
You formatted the text correctly, but matpllotlib uses a serif font, so the spaces don't show up how you expect. Just add the following and it works:
ax.text( stuff, family='monospace' )
Upvotes: 4