Matplotlib波浪箭

时间:2017-07-28 04:56:47

标签: matplotlib plot

有没有办法在matplotlib / python中创建'波浪'箭头?

理想情况下,我想重新创建以下内容: enter image description here

2 个答案:

答案 0 :(得分:1)

要从问题中重现波浪箭头,您可以使用折线图和三角形

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.path as mpath
import matplotlib.patches as mpatches

fig, ax = plt.subplots()

x = np.linspace(0,9*np.pi,151)
y = np.sin(x)
ax.plot(x,y, color="gray", lw="3")

verts = np.array([[0,1],[0,-1],[2,0],[0,1]]).astype(float)*1.3
verts[:,0] += 9*np.pi
path = mpath.Path(verts)
patch = mpatches.PathPatch(path, fc='gray', ec="gray")
ax.add_patch(patch)

ax.axis("off")
ax.set_aspect("equal",'datalim')
ax.relim()
ax.autoscale_view()
plt.show()

enter image description here

答案 1 :(得分:0)

我从前一个帖子片段中创建了一个通用版本(为任何给定的开始和结束坐标返回 > dput(df) structure(list(X1X2X3X4X5X6X7X8X9X10X11X12X13X14X15X16X17X18X19X20X21X22X23X24X25X26X27X28X29X30X31X32X33X34X35X36X37X38X39X40X41X42X43X44X45X46X47X48X49X50X51X52X53X54X55X56X57X58X59X60 = c("414.763714.211714.123713.663712.983713.323713.887715.099715.571716.515715.059713.531713.695713.263713.511713.423714.127713.843712.835713.627713.207714.983716.127715.619715.751716.855715.975715.967716.167717.155716.115716.355716.203716.807716.675716.483716.787716.103716.311716.063716.107716.247717.191718.123618.503618.295620.951618.063618.551619.175619.599619.203618.199616.711716.703716.787716.583717.663618.859618.3356", ="516.959715.903715.391715.679715.679715.839717.151718.079618.623620.479618.879616.287716.799715.615716.991716.831716.991717.535616.351715.135716.543717.407718.431617.055717.311719.167618.239616.703717.215719.167618.207616.767718.719619.423618.271617.535618.719617.887617.247716.959717.279718.399619.551619.263620.095620.447621.535618.431620.735622.143621.663620.767619.743618.559617.951617.887618.111619.295620.351619.4876" )), class = "data.frame", row.names = c(NA, -2L)) 的函数)。

Patch

def curly_arrow(start, end, arr_size = 1, n = 5, col='gray', linew=1., width = 0.1): xmin, ymin = start xmax, ymax = end dist = np.sqrt((xmin - xmax)**2 + (ymin - ymax)**2) n0 = dist / (2 * np.pi) x = np.linspace(0, dist, 151) + xmin y = width * np.sin(n * x / n0) + ymin line = plt.Line2D(x,y, color=col, lw=linew) del_x = xmax - xmin del_y = ymax - ymin ang = np.arctan2(del_y, del_x) line.set_transform(mpl.transforms.Affine2D().rotate_around(xmin, ymin, ang) + ax.transData) ax.add_line(line) verts = np.array([[0,1],[0,-1],[2,0],[0,1]]).astype(float) * arr_size verts[:,1] += ymax verts[:,0] += xmax path = mpath.Path(verts) patch = mpatches.PathPatch(path, fc=col, ec=col) patch.set_transform(mpl.transforms.Affine2D().rotate_around(xmax, ymax, ang) + ax.transData) return patch - 箭头的大小,arr_size - 箭头的线宽,linew - 摆动次数,n - “垂直”(纬度)大小晃。

使用示例:

width

PS。您还需要导入:

fig, ax = plt.subplots()    
ax.add_patch(curly_arrow((20, 20), (2, 10), n=10, arr_size=2))