eco*_*coe 1 python string format text matplotlib
是否可以使用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()
Run Code Online (Sandbox Code Playgroud)
当我将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()
Run Code Online (Sandbox Code Playgroud)
