在绘图上绘制括号

Ant*_*ier 16 python matplotlib

如果我想在我的情节的某些部分绘制括号,我将如何进行:

括号上的情节

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

谢谢你的帮助

Mic*_*ill 16

获得你想要的东西(或者非常接近它的东西)的一种方法是使用matplotlib的annotate功能,它允许你非常广泛地定制箭头功能(参见官方教程).下面是一个示例,其中包含一些组成的绘图数据,以显示如何根据您的标签要求使用它:

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))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

annotate函数包含您需要的文本标签以及用于定位arrow(xy)和文本本身(xycoords)以及一些常规定位和fontsize命令的参数.

可能最感兴趣的是bboxarrowprops论点.该bbox参数在标签周围绘制了一个带有白色背景的正方形.的arrowprops是更多地参与-的arrowstyle键设置的箭头的头部(在此情况下为托架)以及所述头部的宽度和长度.请注意,这是一个字符串arrowstyle.最后,箭头的线宽略有增加.

您可能需要调整xy,xytext,widthB,lengthB,箭头的lw,你想让它得到的一切.

有一件事情不是很清楚(或者至少在我弄清楚的时候不是我)是当annotate包含一个arrowstyle参数时,matplotlib使用这些FancyArrowPatch属性,但是当arrowstyle缺少它时,它会使用这些YAArrow属性.显然,文档中提到了更多细节(以及这种区别)annotate.