Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotly: How to add quivers to an existing plot?

Tags:

I'd like to add quivers to an existing figure with plotly (python). But the only peace of documentation I could find either create only one quiver (here) or a brand new figure (there).

Here's the example on plotly doc :

import plotly.figure_factory as ff

import numpy as np

x,y = np.meshgrid(np.arange(0, 2, .2), np.arange(0, 2, .2))
u = np.cos(x)*y
v = np.sin(x)*y

fig = ff.create_quiver(x, y, u, v)
fig.show()

If anyone has a better understanding of plotly that I do, I'd appreciate a lot a few explanations!

Thanks a lot,

like image 289
hulyce Avatar asked Sep 26 '20 14:09

hulyce


People also ask

How do you add labels to a Plotly scatter plot?

You can include the text labels in the text attribute. To make sure that they are displayed on the scatter plot, set mode='lines+markers+text' . See the Plotly documentation on text and annotations.

What is Quiver plot?

Quiver plot is basically a type of 2D plot which shows vector lines as arrows. This type of plots are useful in Electrical engineers to visualize electrical potential and show stress gradients in Mechanical engineering.

What is XREF in Plotly?

By default, text annotations have xref and yref set to "x" and "y" , respectively, meaning that their x/y coordinates are with respect to the axes of the plot.

What does Add_trace do in Plotly?

Adding Traces New traces can be added to a plot_ly figure using the add_trace() method. This method accepts a plot_ly figure trace and adds it to the figure.


1 Answers

Assuming that you'd like to add quivers to an existing ff.create_quiver() figure, all you have to do is:

  1. Create fig1 = ff.create_quiver(x, y, u, v),
  2. create another figure with other attributes fig2 = ff.create_quiver(x, y, u*0.9, v*2),
  3. and add the resulting fig2.data to fig1 using fig1.add_traces(data = fig2.data)

Plot:

enter image description here

Complete code:

import plotly.figure_factory as ff
import numpy as np

x,y = np.meshgrid(np.arange(0, 2, .2), np.arange(0, 2, .2))
u = np.cos(x)*y
v = np.sin(x)*y

fig1 = ff.create_quiver(x, y, u, v)

fig2 = ff.create_quiver(x, y, u*0.9, v*2)
fig1.add_traces(data = fig2.data)
fig1.show()
like image 165
vestland Avatar answered Sep 28 '22 10:09

vestland