【发布时间】:2016-05-21 02:43:57
【问题描述】:
【问题讨论】:
-
这段代码运行良好:github.com/iruletheworld/matplotlib-curly-brace 不幸的是目前它不是一个包,但你只需要文件 curlyBrace.py
标签: python matplotlib
【问题讨论】:
标签: python matplotlib
获得你想要的东西(或非常接近它的东西)的一种方法是使用 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) 的参数以及一些常规定位和字体大小命令。
最受关注的可能是bbox 和arrowprops 参数。 bbox 参数在标签周围绘制一个白色背景的正方形。 arrowprops 涉及更多——arrowstyle 键设置箭头的头部(在本例中为括号)以及头部的宽度和长度。请注意,这是arrowstyle 下的所有字符串。最后稍微增加了箭头的线宽。
您可能需要调整 xy、xytext、widthB、lengthB 和箭头的 lw 才能获得所需的一切。
有一件事还不是很清楚(或者至少当我发现这一点时我不是很清楚)是当annotate 包含一个arrowstyle 参数时,matplotlib 使用FancyArrowPatch properties,但是当arrowstyle 是缺少,它使用YAArrow properties。 annotate 的文档中显然有更多细节(以及这种区别)。
【讨论】:
widthB和lengthB使用哪些单位来衡量?