是否可以通过 matplotlib 的 figure.text() 命令使用(新样式)python 字符串格式?
我尝试创建 2 列数据作为文本(它们应该整齐对齐)
import matplotlib.pyplot as plt
txt = '{0:50} {1:.4e}'.format('Row1:', 0.1542457) + '\n' + \
'{0:50} {1:.4e}'.format('Row2:', 0.00145744) + '\n' + \
'{0:50} {1:.4e}'.format('Long name for this row):', 0.00146655744) + '\n' + \
'{0:50} {1}'.format('medium size name):', 'some text')
fig = plt.figure()
ax1 = fig.add_axes((0.1, 0.3, 0.8, 0.65))
ax1.plot(range(10),range(10))
fig.text(0.17, 0.07,txt)
plt.show()
当我将 txt 变量打印到屏幕时看起来不错:
但在我的情节中没有对齐
请您参考如下方法:
您需要使用等宽字体才能保持格式:
import matplotlib.pyplot as plt
txt = '{0:50} {1:.4e}\n'.format('Row1:', 0.1542457) + \
'{0:50} {1:.4e}\n'.format('Row2:', 0.00145744) + \
'{0:50} {1:.4e}\n'.format('Long name for this row):', 0.00146655744) + \
'{0:50} {1}'.format('medium size name):', 'some text')
fig = plt.figure()
ax1 = fig.add_axes((0.1, 0.3, 0.8, 0.65))
ax1.plot(range(10),range(10))
fig.text(0.17, 0.07, txt, family='monospace')
plt.show()
