【问题标题】:Adding description box in MatplotLib在 MatplotLib 中添加描述框
【发布时间】:2021-07-11 07:16:46
【问题描述】:

我想在 matplotlib 中添加一个描述框,类似于图中所示。我在描述框中隐藏了敏感细节,因为它不能公开

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

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

谁能帮我解决这个问题?

【问题讨论】:

标签: python matplotlib pycharm


【解决方案1】:

你可以使用 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')

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

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

【讨论】:

  • 非常感谢您的详细回答:)
猜你喜欢
  • 1970-01-01
  • 2012-06-16
  • 1970-01-01
  • 2018-02-24
  • 2011-03-14
  • 1970-01-01
  • 2015-03-19
  • 2021-09-10
  • 2019-09-18
相关资源
最近更新 更多