Python 在matplotlib中使用TextArea和AnnotationBbox打印文本

Python 在matplotlib中使用TextArea和AnnotationBbox打印文本,python,matplotlib,Python,Matplotlib,我正在尝试使用matplotlib中的TextArea和AnnotationBbox打印文本 代码应绘制的结果文本如下所示: 以下是我正在使用的代码: 将numpy导入为np 将matplotlib.pyplot作为plt导入 从matplotlib.offsetbox导入注释BBOX、TextArea、HPacker、VPacker 图,ax=plt.子批次(图尺寸=(12,8)) ybox1=TextArea(“2019/20赛季英超联赛排名”, textprops=dict(color=

我正在尝试使用matplotlib中的
TextArea
AnnotationBbox
打印文本

代码应绘制的结果文本如下所示:

以下是我正在使用的代码:

将numpy导入为np
将matplotlib.pyplot作为plt导入
从matplotlib.offsetbox导入注释BBOX、TextArea、HPacker、VPacker
图,ax=plt.子批次(图尺寸=(12,8))
ybox1=TextArea(“2019/20赛季英超联赛排名”,
textprops=dict(color=“k”,size=15,ha='center',va='center'))
ybox2=TextArea(“利物浦”,
textprops=dict(color=“crimson”,size=15,ha='center',va='center'))
ybox3=TextArea(“曼城”,
textprops=dict(color=“天蓝”,size=15,ha='center',va='center'))
ybox4=文本区域(“和”,
textprops=dict(color=“k”,size=15,ha='center',va='center'))
ybox5=TextArea(“切尔西”,
textprops=dict(color=“blue”,size=15,ha='center',va='center'))
ybox=HPacker(子项=[ybox1,ybox2,ybox3,ybox4,ybox5],align=“center”,pad=0,sep=2)
text=AnnotationBbox(ybox,(0.5,0.5),frameon=False)
ax.添加艺术家(文本)
plt.show()
它在下面的情节中产生


我应该在代码中添加/更新/更改哪些内容,以便代码产生所需的结果。

更改了水平对齐方式,删除了第一个框中的换行符,增加了pad,使用了
VPacker
。结果:

代码:

import numpy as np 
import matplotlib.pyplot as plt
from matplotlib.offsetbox import AnnotationBbox, TextArea, HPacker, VPacker

fig, ax = plt.subplots(figsize=(12,8))

ybox1 = TextArea("Premier League Standing 2019/20", 
                 textprops=dict(color="k", size=15, ha='left',va='baseline'))

ybox2 = TextArea("Liverpool", 
                    textprops=dict(color="crimson", size=15, ha='left',va='baseline'))
ybox3 = TextArea("Man City", 
                    textprops=dict(color="skyblue", size=15, ha='left',va='baseline'))
ybox4 = TextArea("and", 
                    textprops=dict(color="k", size=15, ha='left',va='baseline'))
ybox5 = TextArea("Chelsea", 
                    textprops=dict(color="blue", size=15, ha='left',va='baseline'))

yboxh = HPacker(children=[ybox2, ybox3, ybox4, ybox5], align="left", pad=0, sep=4)
ybox = VPacker(children=[ybox1, yboxh], pad=0, sep=4)

text = AnnotationBbox(ybox, (0.5, 0.5), frameon=False)
ax.add_artist(text)

plt.show()