-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Closed
Description
First of all, thank you for such an amazing plotting library.
The 3D cone plot function is amazing. Creating a fast, informative and responsive presentation of 3D vector fields.
My feature request is a similar function for two dimensions, taking x- and y-coordinates, and the vector components as input and creates a similar plot. I have made my own version of this function with plotly, which produces plots like this:
which works, but it is very slow. The relevant code snippets used to produce the figure above is given below:
def plot_vector_field(x,y,u,v):
def get_colors(values, colorscale='Viridis'):
colorscale = pc.get_colorscale(colorscale)
unique_magnitudes = np.unique(values)
color_map = {val: pc.sample_colorscale(colorscale, val)[0] for val in unique_magnitudes}
return np.vectorize(color_map.get)(values)
def plot_triangle(fig, position,direction,size,color):
x = [position[0]+direction[0]*size/2,
position[0]-direction[0]*size/3 + direction[1]*size/4,
position[0]-direction[0]*size/3 - direction[1]*size/4]
y = [position[1]+direction[1]*size/2,
position[1]-direction[1]*size/3 - direction[0]*size/4,
position[1]-direction[1]*size/3 + direction[0]*size/4]
fig.add_trace(go.Scatter(
x=x,
y=y,
fill='toself',
mode='lines',
line=dict(color='rgba(0,0,0,0)'),
fillcolor=color,
showlegend=False,
name=''
))
magnitude = np.sqrt(u**2 + v**2)
magnitude_normalized = magnitude/np.max(magnitude)
colors = get_colors(magnitude_normalized, colorscale='viridis')
angle = np.arctan2(v, u)
direction = np.array([np.cos(angle), np.sin(angle)]).T
fig = go.Figure()
for i in range(len(x)):
plot_triangle(fig,
position=[x[i],y[i]],
direction=direction[i],
size=1.3*magnitude_normalized[i],
color=colors[i])
return fig
Metadata
Metadata
Assignees
Labels
No labels