Python Pyplot:在一侧绘制带有记号的曲线

Python Pyplot:在一侧绘制带有记号的曲线,python,matplotlib,Python,Matplotlib,我可以使用matplotlib.pyplot中的plot函数来绘制这样的曲线吗?这些曲线的一侧有记号: 不幸的是,我还没有找到一个用于冷锋的半圆标记。但这里有一个关于暖锋的建议: def f(x): return x * np.exp(-x*x) nx=20; x=np.linspace(-1,2.5,nx); y=f(x) # the frontal line xp = 0.5*(x[1:nx] + x[0:nx-1]) # the points between yp

我可以使用matplotlib.pyplot中的plot函数来绘制这样的曲线吗?这些曲线的一侧有记号:


不幸的是,我还没有找到一个用于冷锋的半圆标记。但这里有一个关于暖锋的建议:

def f(x): return x * np.exp(-x*x)

nx=20; x=np.linspace(-1,2.5,nx); y=f(x)  # the frontal line
xp = 0.5*(x[1:nx] + x[0:nx-1])           # the points between
yp = 0.5*(y[1:nx] + y[0:nx-1])
dy = np.diff(y); dx = np.diff(x)         # the gradient
nn = 40*np.sqrt(dx*dx + dy*dy)           # nn=norm; 40 = empirical hack for the normal shift
dx = dx/nn;  dy = dy/nn                  # the components of the normals
alpha = 180*np.arctan(dy/dx)/np.pi       # the slope angel to the normal

plt.style.use('fast')  
fig, ax0 = plt.subplots(figsize=(20,20))
xh = np.zeros_like(xp); yh = np.zeros_like(yp)
for j in range(nx-1):
    xh[j]=xp[j]-dy[j];  yh[j]=yp[j]+dx[j] # shift in the normal direction
    plt.scatter(xh[j],yh[j],s=900,c='r',marker=(3, 0, alpha[j]))

ax0.set_aspect('equal')                   # this is important !
plt.plot(x,y,c='r',lw=5, label='this is the frontal line')
plt.plot(xh,yh,ls='--', label='here are the markers')
plt.title("Important: set_aspect('equal')",fontsize=25, fontweight='bold')
plt.text(-1,0.2,'The markers have to be \n shifted in the normal direction \n of the frontal line')
plt.margins(0.1); plt.legend(prop={'size': 20});plt.grid(); plt.show()

不幸的是,我还没有找到一个用于冷锋的半圆标记。但这里有一个关于暖锋的建议:

def f(x): return x * np.exp(-x*x)

nx=20; x=np.linspace(-1,2.5,nx); y=f(x)  # the frontal line
xp = 0.5*(x[1:nx] + x[0:nx-1])           # the points between
yp = 0.5*(y[1:nx] + y[0:nx-1])
dy = np.diff(y); dx = np.diff(x)         # the gradient
nn = 40*np.sqrt(dx*dx + dy*dy)           # nn=norm; 40 = empirical hack for the normal shift
dx = dx/nn;  dy = dy/nn                  # the components of the normals
alpha = 180*np.arctan(dy/dx)/np.pi       # the slope angel to the normal

plt.style.use('fast')  
fig, ax0 = plt.subplots(figsize=(20,20))
xh = np.zeros_like(xp); yh = np.zeros_like(yp)
for j in range(nx-1):
    xh[j]=xp[j]-dy[j];  yh[j]=yp[j]+dx[j] # shift in the normal direction
    plt.scatter(xh[j],yh[j],s=900,c='r',marker=(3, 0, alpha[j]))

ax0.set_aspect('equal')                   # this is important !
plt.plot(x,y,c='r',lw=5, label='this is the frontal line')
plt.plot(xh,yh,ls='--', label='here are the markers')
plt.title("Important: set_aspect('equal')",fontsize=25, fontweight='bold')
plt.text(-1,0.2,'The markers have to be \n shifted in the normal direction \n of the frontal line')
plt.margins(0.1); plt.legend(prop={'size': 20});plt.grid(); plt.show()
升级 根据答案,我可以扩展示例:

def f(x): return x,  x * np.exp(-x*x)

def get_parameters(x,y):
    xp = 0.5*(x[1:nx] + x[0:nx-1])           # the points between
    yp = 0.5*(y[1:nx] + y[0:nx-1])
    dy = np.diff(y); dx = np.diff(x)         # the gradient
    nn = 40*np.sqrt(dx*dx + dy*dy)           # nn=norm; 40 = empirical hack for the normal shift
    dx = dx/nn;  dy = dy/nn                  # the components of the normals
    alpha = 180*np.arctan(dy/dx)/np.pi       # the slope angel to the normal
    return xp,yp,dx,dy,alpha

nx = 20;
ip = np.linspace(0,1,nx)
xr,yr = f(3*ip-0.5)                           # red front line
xb,yb = f(3*ip-0.5); yb = 0.7*yb -0.3         # blue front line

xpb, ypb, dx, dy, alphaB = get_parameters(xb,yb) # red points between
xpr, ypr,  _,  _, alphaR = get_parameters(xr,yr) # blue points between

plt.style.use('fast')  
fig, ax0 = plt.subplots(figsize=(20,20))
plt.plot(xr,yr, c='r', lw=5, label='warm front')
plt.plot(xb,yb, c='b', lw=5, label='cold front')

for j in range(nx-1):
    #--- set the blue markers ---
    marker_size_B = 900
    plt.scatter(xpb[j]-dy[j], ypb[j]+dx[j],
                s=marker_size_B, c='b', marker=(3, 0, alphaB[j]) )    

    #--- set the red markers ---
    marker_size_R=0.05
    halfR = mpl.patches.Wedge((xpr[j], ypr[j]), marker_size_R, theta1=0+alphaB[j], theta2=180+alphaB[j], color='r')
    ax0.add_artist(halfR)
plt.legend(prop={'size': 20})
ax0.set_aspect('equal'); plt.grid(); plt.margins(0.1);plt.show()
标记的密度可以通过升级来控制。

根据答案,我可以扩展示例:

def f(x): return x,  x * np.exp(-x*x)

def get_parameters(x,y):
    xp = 0.5*(x[1:nx] + x[0:nx-1])           # the points between
    yp = 0.5*(y[1:nx] + y[0:nx-1])
    dy = np.diff(y); dx = np.diff(x)         # the gradient
    nn = 40*np.sqrt(dx*dx + dy*dy)           # nn=norm; 40 = empirical hack for the normal shift
    dx = dx/nn;  dy = dy/nn                  # the components of the normals
    alpha = 180*np.arctan(dy/dx)/np.pi       # the slope angel to the normal
    return xp,yp,dx,dy,alpha

nx = 20;
ip = np.linspace(0,1,nx)
xr,yr = f(3*ip-0.5)                           # red front line
xb,yb = f(3*ip-0.5); yb = 0.7*yb -0.3         # blue front line

xpb, ypb, dx, dy, alphaB = get_parameters(xb,yb) # red points between
xpr, ypr,  _,  _, alphaR = get_parameters(xr,yr) # blue points between

plt.style.use('fast')  
fig, ax0 = plt.subplots(figsize=(20,20))
plt.plot(xr,yr, c='r', lw=5, label='warm front')
plt.plot(xb,yb, c='b', lw=5, label='cold front')

for j in range(nx-1):
    #--- set the blue markers ---
    marker_size_B = 900
    plt.scatter(xpb[j]-dy[j], ypb[j]+dx[j],
                s=marker_size_B, c='b', marker=(3, 0, alphaB[j]) )    

    #--- set the red markers ---
    marker_size_R=0.05
    halfR = mpl.patches.Wedge((xpr[j], ypr[j]), marker_size_R, theta1=0+alphaB[j], theta2=180+alphaB[j], color='r')
    ax0.add_artist(halfR)
plt.legend(prop={'size': 20})
ax0.set_aspect('equal'); plt.grid(); plt.margins(0.1);plt.show()

标记的密度可以通过控制。

您可以尝试在需要标记的点处计算角度,然后使用应答器。单独绘制线和自定义标记。您可以尝试在需要标记的点处计算角度,然后使用应答器,这也可能是有意义的有趣。分别绘制线和自定义标记。很好!如果您想尝试半圆标记,请查看,显然可以使用顶点列表x、y对或路径来定义标记。Tx!我咨询过这个文档,但我不清楚如何从中得到半个圆。很好!如果您想尝试半圆标记,请查看,显然可以使用顶点列表x、y对或路径来定义标记。Tx!我已经咨询过这个文档,但我不清楚如何从中得到一个半圆。