1

How to edit this code to have the same width and colour map as in the following figure? The script is based on this question.

enter image description here

import numpy as np
import matplotlib.pyplot as plt

dpi = 100

def offset(x,y, o):
    """ Offset coordinates given by array x,y by o """
    X = np.c_[x,y].T
    m = np.array([[0,-1],[1,0]])
    R = np.zeros_like(X)
    S = X[:,2:]-X[:,:-2]
    R[:,1:-1] = np.dot(m, S)
    R[:,0] = np.dot(m, X[:,1]-X[:,0])
    R[:,-1] = np.dot(m, X[:,-1]-X[:,-2])
    On = R/np.sqrt(R[0,:]**2+R[1,:]**2)*o
    Out = On+X
    return Out[0,:], Out[1,:]


def offset_curve(ax, x,y, o):
    """ Offset array x,y in data coordinates
        by o in points """
    trans = ax.transData.transform
    inv = ax.transData.inverted().transform
    X = np.c_[x,y]
    Xt = trans(X)
    xto, yto = offset(Xt[:,0],Xt[:,1],o*dpi/72. )
    Xto = np.c_[xto, yto]
    Xo = inv(Xto)
    return Xo[:,0], Xo[:,1]

x = np.linspace(-3, 3, 100)
y = -(1/4*x**4 - 1.6*x**2) 


fig, ax=plt.subplots(figsize=(4,2.5), dpi=dpi)

cmap = plt.get_cmap('Greys_r')
lw = 2.
lines = []
width_l = ax.get_ylim()[1] - ax.get_ylim()[0]
for t in np.linspace(0, 1, 40):
    l, = ax.plot(x, y - t * 0.1 * width_l, color=cmap(t/2 + 0.25))
    lines.append(l)

def plot_rainbow(event=None):
    # initialization of lists
    xr,  yr = 6*[None], 6*[None]
    xr[0],yr[0] = offset_curve(ax, x,y, lw/2.)
    xr[1],yr[1] = offset_curve(ax, x,y, -lw/2.)
    xr[2],yr[2] = offset_curve(ax, xr[0],yr[0], lw)
    xr[3],yr[3] = offset_curve(ax, xr[1],yr[1], -lw)
    xr[4],yr[4] = offset_curve(ax, xr[2],yr[2], lw)
    xr[5],yr[5] = offset_curve(ax, xr[3],yr[3], -lw)

    for i  in range(6):     
        lines[i].set_data(xr[i], yr[i])

plot_rainbow()

fig.canvas.mpl_connect("resize_event", plot_rainbow)
fig.canvas.mpl_connect("button_release_event", plot_rainbow)

plt.show()

The figure above was created by the following script:

import numpy as np
import matplotlib.pyplot as plt
import math 

dpi = 100

# Function for plotting parallels to curves
def get_parallels(length=.1):
    
    px, py = [], []
    
    for idx in range(len(x)-1):
        x0, y0, xa, ya = x[idx], y[idx], x[idx+1], y[idx+1]
        dx, dy = xa-x0, ya-y0
        norm = math.hypot(dx, dy) * 1/length
        dx /= norm
        dy /= norm        
        px.append(x0-dy)
        py.append(y0+dx)
    return px, py

fig, ax=plt.subplots(figsize=(4,2.5), dpi=dpi)

cmap = plt.get_cmap('Greys_r')

x = np.linspace(-1, 1, 100)
y = -x**2
ax.set_ylim(-1.02, 0.3)
ax.scatter(1/2*(ax.get_xlim()[0] + ax.get_xlim()[1]), 0.145, marker = 'o', s=900, facecolors='none')
width_l = ax.get_ylim()[1] - ax.get_ylim()[0]
for t in np.linspace(0, 1, 40): 
    length =  -0.1*width_l*t
    ax.plot(*get_parallels(length=length), color=cmap(t/2 + 0.25))

plt.tight_layout()

plt.show()

Several curves are plotted in camp and the length is set.

I would like to have the same "shadow" for the curve in the first scrip. How to do that, please?

Elena Greg
  • 1,061
  • 1
  • 11
  • 26

0 Answers0