【问题标题】:Drawing brackets over plot在绘图上绘制括号
【发布时间】:2016-05-21 02:43:57
【问题描述】:

如果我想像这样在情节的某些部分上画括号,我该怎么做:

目前我正在使用 powerpoint 添加它们,但我想完全在 python 中完成。

感谢您的帮助

【问题讨论】:

标签: python matplotlib


【解决方案1】:

获得你想要的东西(或非常接近它的东西)的一种方法是使用 matplotlib 的 annotate 函数,它允许你非常广泛地自定义箭头函数(参见 official tutorial)。这是一个示例,其中包含一些组成的绘图数据,以显示如何根据您的标签要求使用它:

import matplotlib.pyplot as plt
import numpy as np

# Make some fake data and a sample plot
x = np.arange(1,100)
y = x**2 * 1.0e6
ax = plt.axes()
ax.plot(x,y)

# Adjust the fontsizes on tick labels for this plot
fs = 14.0
[t.set_fontsize(fs) for t in ax.xaxis.get_majorticklabels()]
[t.set_fontsize(fs) for t in ax.yaxis.get_majorticklabels()]
ax.yaxis.get_offset_text().set_fontsize(fs)

# Here is the label and arrow code of interest
ax.annotate('SDL', xy=(0.5, 0.90), xytext=(0.5, 1.00), xycoords='axes fraction', 
            fontsize=fs*1.5, ha='center', va='bottom',
            bbox=dict(boxstyle='square', fc='white'),
            arrowprops=dict(arrowstyle='-[, widthB=7.0, lengthB=1.5', lw=2.0))

annotate 函数包含您想要的文本标签以及用于定位箭头 (xy) 和文本本身 (xycoords) 的参数以及一些常规定位和字体大小命令。

最受关注的可能是bboxarrowprops 参数。 bbox 参数在标签周围绘制一个白色背景的正方形。 arrowprops 涉及更多——arrowstyle 键设置箭头的头部(在本例中为括号)以及头部的宽度和长度。请注意,这是arrowstyle 下的所有字符串。最后稍微增加了箭头的线宽。

您可能需要调整 xyxytextwidthBlengthB 和箭头的 lw 才能获得所需的一切。

有一件事还不是很清楚(或者至少当我发现这一点时我不是很清楚)是当annotate 包含一个arrowstyle 参数时,matplotlib 使用FancyArrowPatch properties,但是当arrowstyle 是缺少,它使用YAArrow propertiesannotate 的文档中显然有更多细节(以及这种区别)。

【讨论】:

  • widthBlengthB使用哪些单位来衡量?
  • 或者,可以绘制括号,如answer所示。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-09
  • 1970-01-01
  • 1970-01-01
  • 2021-12-10
相关资源
最近更新 更多