Friday 20 August 2021

Quiver: Set Vector Transparency Equal to Length

Data:

I have the following vector field:

enter image description here

Goal

I would like to have the arrow transparency proportional to the arrow length.

My Attempt:

I set the "alpha" value equal to the length:

import numpy as np
import matplotlib.pyplot as plt

# DATA FIELD (1,N,M)
dz = np.array([[[0.24884899, 0.24884899, 0.24884899, 0.24884899, 0.24884899,
         0.24884899],
        [0.248849  , 0.248849  , 0.248849  , 0.248849  , 0.248849  ,
         0.248849  ],
        [0.24885767, 0.24885513, 0.24885108, 0.24885113, 0.2488552 ,
         0.24885767],
        [0.2451304 , 0.24563262, 0.24642831, 0.24641793, 0.24561579,
         0.2451304 ],
        [0.0764377 , 0.12581053, 0.09866768, 0.10043774, 0.12461962,
         0.0764377 ],
        [0.03382106, 0.03394624, 0.03414449, 0.03414171, 0.03394174,
         0.03382106]]])

dx = np.zeros(np.shape(dz))
dy = np.zeros(np.shape(dz))

# DATA POINTS (N,)
X = np.array([0. , 0.2, 0.4, 0.6, 0.8, 1. ])
Y = X 
Z = np.array([-500., -360., -220.,  -80.,   60.,  200.])


# COMPUTE LENGTH OF VECTORS
length = np.sqrt(dx[0]**2+ dz[0]**2)

# PLOT 2D Cross-Section of vector field
fig = plt.figure(dpi=300)
Q = plt.quiver(X, Z, dx[0], dz[0], length, units='xy' ,angles='xy', scale=0.005,  
       pivot = "tail", headaxislength = 5, headlength = 5, cmap='jet', alpha = length)
fig.colorbar(Q)
plt.gca().invert_yaxis()
plt.ylabel("Z")
plt.xlabel("X")
plt.title("2D Cross-Section")

Problem:

... I get:

TypeError: alpha must be a float or None

Any help to solve this would be very much appreciated !

EDIT

3D Vector Field to test

import matplotlib.pyplot as plt
import numpy as np

plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True

fig = plt.figure(dpi = 300)

ax = fig.add_subplot(projection='3d')

x, y, z = np.meshgrid(np.arange(-0.8, 1, 0.2),
                     np.arange(-0.8, 1, 0.2),
                     np.arange(-0.8, 1, 0.2))

u = x
v = y
w = z

ax.quiver(x, y, z, u, v, w, length=0.2)
ax.axis('off')

plt.show()

enter image description here

EDIT 2

The resulting plot does not have consistent alpha values. It seems that some long vectors are still not transparent. The transparency should increase linearly from the center, which is not the case here:

import numpy as np, matplotlib.pyplot as plt


def plot_3d_quiver(x, y, z, u, v, w):
    c = np.sqrt(np.abs(v)**2 + np.abs(u)**2 + np.abs(w)**2)
    length = np.repeat(((c - c.min())/(c.max() - c.min())).ravel(), 3)
    c = (c.ravel() - c.min())/c.ptp()
    c = np.concatenate((c, np.repeat(c, 2)))
    c = plt.cm.jet(c)
    
    # INVERT -> Long vectors should be more transparent
    c[:, -1] = 1 - length

    fig = plt.figure(dpi = 300)
    ax = fig.gca(projection = '3d')
    ax.quiver(x, y, z, u, v, w, length = .5, arrow_length_ratio = 0.2, alpha=c)
    # ax.axis('off')


x, y, z = np.meshgrid(np.arange(-0.8, 1, 0.2),
                      np.arange(-0.8, 1, 0.2),
                      np.arange(-0.8, 1, 0.2))
u = x
v = y
w = z

plot_3d_quiver(x, y, z, u, v, w)

plt.show()

enter image description here



from Quiver: Set Vector Transparency Equal to Length

No comments:

Post a Comment