我想知道 matplotlib 中关键参数 mutational_scale 的目的;对于用于绘制箭头的 matplotlib.patches.FancyArrowPatch,mutational_scale 默认设置为 1。来自文档:
scalar, optional (default: 1)
Value with which attributes of arrowstyle (e.g., head_length) will be scaled.
当我在两个大小为 300 的散点之间绘制箭头时,如下所示:
import matplotlib.pyplot as plt
import matplotlib as mpl
fig, ax = plt.subplots()
ax.scatter([0,1],[0,1], s=300)
arrow = mpl.patches.FancyArrowPatch((0,0), (1,1), arrowstyle='-|>')
plt.show()
箭头不可见。
但是,当我使用:
arrow = mpl.patches.FancyArrowPatch((0,0), (1,1), arrowstyle='-|>', mutational_aspect=20)
我的猜测是整体图形按大于 1 的因子缩放,接近 20,使箭头在突变比例为 1 时不可见,但在使用 20 时可见。但这只是一个猜测。
如果是这样,是否有办法知道整体图形的突变缩放比例以确定箭头的适当缩放值?
请您参考如下方法:
在annotating-with-arrow小节 在文档中提供了 arrowstyle 参数。对于 -|> 样式,它表示
-|>head_length=0.4,head_width=0.2
这意味着箭头的头部是 0.4*mutation_scale 长和 2*0.2*mutation_scale 宽。单位是点。
由于 FancyArrowPatches 被设计为与注释一起使用,因此在绘制注释时,突变比例将设置为以磅为单位的字体大小。例如。对于 15 磅字体大小,mutation_scale=15。
当单独使用 FancyArrowPatch 时,请以磅为单位设置所需的大小
arrow = mpl.patches.FancyArrowPatch((0,1), (1,1), arrowstyle='-|>', mutation_scale=15)
这将创建一个具有完整箭头宽度的箭头 2*0.2*15=6 点。
import matplotlib.pyplot as plt
import matplotlib as mpl
fig, ax = plt.subplots(dpi=100)
ax.set_xlim([-0.6,5])
for i in [5,10,15,20,25]:
ax.scatter([0,1],[i,i], s=(2*.2*i)**2)
arrow = mpl.patches.FancyArrowPatch((0,i), (1,i), arrowstyle='-|>',
mutation_scale=i)
ax.add_patch(arrow)
ax.text(1.2,i,"mutation_scale = {}".format(i),
va="center")
plt.show()
或者,您可以直接以磅为单位指定 head_length 和 head_width,
arrow = mpl.patches.FancyArrowPatch((0,1), (1,1),
arrowstyle='-|>,head_width=3,head_length=6')
因此,以下将产生与上面相同的图像:
import matplotlib.pyplot as plt
import matplotlib as mpl
fig, ax = plt.subplots(dpi=100)
ax.set_xlim([-0.6,5])
for i in [5,10,15,20,25]:
ax.scatter([0,1],[i,i], s=(2*.2*i)**2)
arrow = mpl.patches.FancyArrowPatch((0,i), (1,i),
arrowstyle='-|>,head_width={},head_length={}'.format(i/5.,.4*i))
ax.add_patch(arrow)
ax.text(1.2,i,"mutation_scale = {}".format(i),
va="center")
plt.show()
