在 MatplotLib 中添加描述框

Mas*_*mar 1 python matplotlib pycharm

我想在 matplotlib 中添加一个描述框,类似于图中所示的内容。我在描述框中隐藏了敏感详细信息,因为它无法公开

有没有办法在图旁边添加这样的描述框(当然不隐藏细节)?

意图情节的描述

我尝试在互联网上搜索,但我得到的只是如何在情节中添加描述,而不是像我希望的那样

谁能帮我这个 ?

小智 5

您可以使用 matplotlib.pyplot.text。

如果您指定transform=ax.transAxes,以便坐标相对于轴边界框,并且verticalalignment='top',您可以获得与您发布的图片非常相似的内容。

import numpy as np
import matplotlib.pyplot as plt

x_val = np.random.uniform(low=-4, high=4, size=200)

fig, ax = plt.subplots()
ax.hist(x_val,bins=20)

textbox = '\n'.join([
    'Evaluation flow name: ',
    'Evaluation description: ',
    '',
    'Evaluation flow SVN Revision: ',
    'Dataset path: ',
    'Dataset name: ',
    '',
    'Filter: ',
    'Grouping: ',
    'Files selected with filter and grouping: ',
    'Files with all needed signals: ',
    '',
    '...',
    '...',
    '...',
])

bbox = dict(boxstyle='square', facecolor='lavender', alpha=0.5)
fig.text(1.1,1,textbox,fontsize=10,transform=ax.transAxes, bbox=bbox, 
verticalalignment='top')
Run Code Online (Sandbox Code Playgroud)

给出以下输出: https://i.stack.imgur.com/57rnI.png

对于文本,我使用 join() 只是为了更容易阅读和修改(相对于一个充满 '\n' 的长字符串,也许有更好的方法来做到这一点。