Matplotlib:如何在多行上对齐文本

时间:2018-06-01 18:07:43

标签: python-2.7 matplotlib data-visualization

鉴于以下不那么严重的代码:

import numpy as np
import pylab
import matplotlib.pyplot as plt
from datetime import datetime

franco = [0.18, 0.72, 0.25, 0.62, 0.14, 0.41, 0.94, 0.94, 0.13, 0.12, 0.06, 0.12, 0.26, 0.32]
giammai = [0.16, 0.45, 0.25, 0.69, 0.11, 0.22, 0.97, 0.90, 0.19, 0.22, 1.16, 1.22, 1.56, 1.30]
danieleaceto = [0.5, 0.89, 0.86, 0.55, 0.37, 0.83, 1.04, 0.92, 1.0, 1.06, 1.27, 1.27, 1.69, 1.30]
andrea = [0.01, 0.04, 0.09, 0.15, 0.29, 0.35, 0.45, 0.65, 0.85, 1.09, 1.16, 1.22, 1.56, 1.65]
paja = [x + y for x, y in zip(andrea, [round(item,2) for item in 0.3*np.random.random(len(andrea))])]
benna = [x + y for x, y in zip(franco, [round(item,2) for item in 0.3*np.random.random(len(franco))])]
gioelipeanderson = [0.00, 0.00, 0.00, 0.00, 0.17, 0.24, 0.33, 0.57, 0.78, 1.03, 1.09, 1.17, 1.59, 2.01]
celeste_ = [x + y for x, y in zip(gioelipeanderson, [round(item,2) for item in 3*np.random.random(len(gioelipeanderson))])]
celeste = np.zeros(12).tolist()+[1.62, 2.00]

periodo_text = ['2012-01','2012-07','2013-01','2013-07','2014-01','2014-07','2015-01','2015-07','2016-01','2016-07','2017-01','2017-07','2018-02','2018-07']
periodo      = [datetime.strptime(item+'-15', '%Y-%m-%d') for item in periodo_text]

text_to_show = str('Andrea          : '  )    + '1.95 m' + \
               str('\nPaja            : ')    + '1.65 m' + \
               str('\nFranco          : ')    + '1.80 m' + \
               str('\nBenna           : ')    + '1.55 m' + \
               str('\nIandool         : ')    + '1.80 m' + \
               str('\nGiammai         : ')    + '1.82 m' + \
               str('\nCelestina       : ')    + '0.50 m' + \
               str('\nGioelipeAnderson: ')    + '1.00 m' 

我希望有一个情节,我在其中注释一些统计数据,如高度。

fig = plt.figure()
fig.suptitle('Serious stuff', fontsize=14, fontweight='bold')
ax = fig.add_subplot(111)
fig.subplots_adjust(top=0.95)
ax.set_title('')
ax.text(periodo[6], 1.45, str(text_to_show), style='italic', bbox={'facecolor': 'green', 'alpha': 0.2, 'pad': 10})
ax.set_xlabel('time running out..')
ax.set_ylabel('chunk')
ax.plot(periodo, franco           , 'o-', label='Franco',c='deeppink')
ax.plot(periodo, giammai          , 'o-', label='Giammai',c='darkgoldenrod')
ax.plot(periodo, danieleaceto     , 'o-', label='Iando',c='gold')
ax.plot(periodo, andrea           , 'o-', label='Andrea',c='yellowgreen')
ax.plot(periodo, paja             , 'o-', label='Paja',c='forestgreen')
ax.plot(periodo, benna            , 'o-', label='Benna',c='magenta')
ax.plot(periodo, gioelipeanderson , 'o--', label='Gioelipe Anderson', c='orange')
ax.plot(periodo, celeste          , 'o--', label='Celeste', c = 'deepskyblue')
plt.axvline(x=datetime(2014,1,11), color='g', linestyle='--')
plt.axvline(x=datetime(2018,2,12), color='r', linestyle='--')
ax.annotate('Every Data Science project begins \nwith the Data Visualization..', xy=(periodo[1], 0.45), xytext=(periodo[0], 1.25),arrowprops=dict(facecolor='darkgoldenrod', shrink=0.05))
ax.legend()
plt.show()

由于我们的名字长度非常不同,因此结果“不太好”: enter image description here

在绿色中央框中,我想要对齐列。我已经尝试使用“\ t”为对齐设置标签,但它不起作用。

1 个答案:

答案 0 :(得分:1)

您需要monospace font

ax.text(periodo[6], 1.45, str(text_to_show),
    family='monospace',
    style='italic', bbox={'facecolor': 'green', 'alpha': 0.2, 'pad': 10})