python - 在 matplotlib 中使用 TextArea 和 AnnotationBbox 绘制文本
问题描述
我正在尝试使用TextArea
和AnnotationBbox
在 matplotlib 中绘制文本。
我的代码应该绘制的结果文本如下所示:
这是我正在使用的代码:
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 \n",
textprops=dict(color="k", size=15, ha='center',va='center'))
ybox2 = TextArea("Liverpool",
textprops=dict(color="crimson", size=15,ha='center',va='center'))
ybox3 = TextArea("Man City",
textprops=dict(color="skyblue", size=15,ha='center',va='center'))
ybox4 = TextArea("and",
textprops=dict(color="k", size=15,ha='center',va='center'))
ybox5 = TextArea("Chelsea",
textprops=dict(color="blue", size=15,ha='center',va='center'))
ybox = HPacker(children=[ybox1, ybox2, ybox3, ybox4, ybox5],align="center", pad=0, sep=2)
text = AnnotationBbox(ybox, (0.5, 0.5), frameon=False)
ax.add_artist(text)
plt.show()
我应该在我的代码中添加/更新/更改什么,以便代码产生所需的结果。
解决方案
更改了水平对齐方式,删除了第一个框中的换行符,增加了填充,使用了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()
推荐阅读
- c# - 我是编程和 C# 的新手,我的学校作业输出错误
- c# - 如何在 C# 中检查 2 或 3 个应用程序是否打开并处于活动状态
- java - Oracle JDBC Thin:强制网络加密
- sharepoint-online - 如何使用 Power 自动化或 Rest API 向 SharePoint 文档库/列表上的用户授予访问权限
- r - 绘制统计功效与重复并计算系数的平均值
- python - 忽略命令中的异常无:discord.ext.commands.errors.CommandNotFound:找不到命令“加入”错误
- mysql - 使用 MySQL REGEXP 通过 Django ORM 过滤字符串
- plotly-dash - 从回调中选择数据表活动/选定单元格
- javascript - 在多行js字符串中插入重音
- javascript - React Hooks - useFetch 泛化