def create_streamline(x, y, u, v, density=1, angle=math.pi / 9, arrow_scale=.09, **kwargs): """ Returns data for a streamline plot. :param (list|ndarray) x: 1 dimensional, evenly spaced list or array :param (list|ndarray) y: 1 dimensional, evenly spaced list or array :param (ndarray) u: 2 dimensional array :param (ndarray) v: 2 dimensional array :param (float|int) density: controls the density of streamlines in plot. This is multiplied by 30 to scale similiarly to other available streamline functions such as matplotlib. Default = 1 :param (angle in radians) angle: angle of arrowhead. Default = pi/9 :param (float in [0,1]) arrow_scale: value to scale length of arrowhead Default = .09 :param kwargs: kwargs passed through plotly.graph_objs.Scatter for more information on valid kwargs call help(plotly.graph_objs.Scatter) :rtype (dict): returns a representation of streamline figure. Example 1: Plot simple streamline and increase arrow size ``` import plotly.plotly as py from plotly.figure_factory import create_streamline import numpy as np import math # Add data x = np.linspace(-3, 3, 100) y = np.linspace(-3, 3, 100) Y, X = np.meshgrid(x, y) u = -1 - X**2 + Y v = 1 + X - Y**2 u = u.T # Transpose v = v.T # Transpose # Create streamline fig = create_streamline(x, y, u, v, arrow_scale=.1) # Plot py.plot(fig, filename='streamline') ``` Example 2: from nbviewer.ipython.org/github/barbagroup/AeroPython ``` import plotly.plotly as py from plotly.figure_factory import create_streamline import numpy as np import math # Add data N = 50 x_start, x_end = -2.0, 2.0 y_start, y_end = -1.0, 1.0 x = np.linspace(x_start, x_end, N) y = np.linspace(y_start, y_end, N) X, Y = np.meshgrid(x, y) ss = 5.0 x_s, y_s = -1.0, 0.0 # Compute the velocity field on the mesh grid u_s = ss/(2*np.pi) * (X-x_s)/((X-x_s)**2 + (Y-y_s)**2) v_s = ss/(2*np.pi) * (Y-y_s)/((X-x_s)**2 + (Y-y_s)**2) # Create streamline fig = create_streamline(x, y, u_s, v_s, density=2, name='streamline') # Add source point point = Scatter(x=[x_s], y=[y_s], mode='markers', marker=Marker(size=14), name='source point') # Plot fig['data'].append(point) py.plot(fig, filename='streamline') ``` """ utils.validate_equal_length(x, y) utils.validate_equal_length(u, v) validate_streamline(x, y) utils.validate_positive_scalars(density=density, arrow_scale=arrow_scale) streamline_x, streamline_y = _Streamline(x, y, u, v, density, angle, arrow_scale).sum_streamlines() arrow_x, arrow_y = _Streamline(x, y, u, v, density, angle, arrow_scale).get_streamline_arrows() streamline = graph_objs.Scatter(x=streamline_x + arrow_x, y=streamline_y + arrow_y, mode='lines', **kwargs) data = [streamline] layout = graph_objs.Layout(hovermode='closest') return graph_objs.Figure(data=data, layout=layout)
def create_quiver(x, y, u, v, scale=0.1, arrow_scale=0.3, angle=math.pi / 9, scaleratio=None, **kwargs): """ Returns data for a quiver plot. :param (list|ndarray) x: x coordinates of the arrow locations :param (list|ndarray) y: y coordinates of the arrow locations :param (list|ndarray) u: x components of the arrow vectors :param (list|ndarray) v: y components of the arrow vectors :param (float in [0,1]) scale: scales size of the arrows(ideally to avoid overlap). Default = .1 :param (float in [0,1]) arrow_scale: value multiplied to length of barb to get length of arrowhead. Default = .3 :param (angle in radians) angle: angle of arrowhead. Default = pi/9 :param (positive float) scaleratio: the ratio between the scale of the y-axis and the scale of the x-axis (scale_y / scale_x). Default = None, the scale ratio is not fixed. :param kwargs: kwargs passed through plotly.graph_objs.Scatter for more information on valid kwargs call help(plotly.graph_objs.Scatter) :rtype (dict): returns a representation of quiver figure. Example 1: Trivial Quiver ``` import plotly.plotly as py from plotly.figure_factory import create_quiver import math # 1 Arrow from (0,0) to (1,1) fig = create_quiver(x=[0], y=[0], u=[1], v=[1], scale=1) py.plot(fig, filename='quiver') ``` Example 2: Quiver plot using meshgrid ``` import plotly.plotly as py from plotly.figure_factory import create_quiver import numpy as np import math # Add data x,y = np.meshgrid(np.arange(0, 2, .2), np.arange(0, 2, .2)) u = np.cos(x)*y v = np.sin(x)*y #Create quiver fig = create_quiver(x, y, u, v) # Plot py.plot(fig, filename='quiver') ``` Example 3: Styling the quiver plot ``` import plotly.plotly as py from plotly.figure_factory import create_quiver import numpy as np import math # Add data x, y = np.meshgrid(np.arange(-np.pi, math.pi, .5), np.arange(-math.pi, math.pi, .5)) u = np.cos(x)*y v = np.sin(x)*y # Create quiver fig = create_quiver(x, y, u, v, scale=.2, arrow_scale=.3, angle=math.pi/6, name='Wind Velocity', line=dict(width=1)) # Add title to layout fig['layout'].update(title='Quiver Plot') # Plot py.plot(fig, filename='quiver') ``` Example 4: Forcing a fix scale ratio to maintain the arrow length ``` import plotly.plotly as py from plotly.figure_factory import create_quiver import numpy as np # Add data x,y = np.meshgrid(np.arange(0.5, 3.5, .5), np.arange(0.5, 4.5, .5)) u = x v = y angle = np.arctan(v / u) norm = 0.25 u = norm * np.cos(angle) v = norm * np.sin(angle) # Create quiver with a fix scale ratio fig = create_quiver(x, y, u, v, scale = 1, scaleratio = 0.5) # Plot py.plot(fig, filename='quiver') ``` """ utils.validate_equal_length(x, y, u, v) utils.validate_positive_scalars(arrow_scale=arrow_scale, scale=scale) if scaleratio is None: quiver_obj = _Quiver(x, y, u, v, scale, arrow_scale, angle) else: quiver_obj = _Quiver(x, y, u, v, scale, arrow_scale, angle, scaleratio) barb_x, barb_y = quiver_obj.get_barbs() arrow_x, arrow_y = quiver_obj.get_quiver_arrows() quiver_plot = graph_objs.Scatter(x=barb_x + arrow_x, y=barb_y + arrow_y, mode="lines", **kwargs) data = [quiver_plot] if scaleratio is None: layout = graph_objs.Layout(hovermode="closest") else: layout = graph_objs.Layout(hovermode="closest", yaxis=dict(scaleratio=scaleratio, scaleanchor="x")) return graph_objs.Figure(data=data, layout=layout)
def create_quiver(x, y, u, v, scale=.1, arrow_scale=.3, angle=math.pi / 9, scaleratio=None, **kwargs): """ Returns data for a quiver plot. :param (list|ndarray) x: x coordinates of the arrow locations :param (list|ndarray) y: y coordinates of the arrow locations :param (list|ndarray) u: x components of the arrow vectors :param (list|ndarray) v: y components of the arrow vectors :param (float in [0,1]) scale: scales size of the arrows(ideally to avoid overlap). Default = .1 :param (float in [0,1]) arrow_scale: value multiplied to length of barb to get length of arrowhead. Default = .3 :param (angle in radians) angle: angle of arrowhead. Default = pi/9 :param (positive float) scaleratio: the ratio between the scale of the y-axis and the scale of the x-axis (scale_y / scale_x). Default = None, the scale ratio is not fixed. :param kwargs: kwargs passed through plotly.graph_objs.Scatter for more information on valid kwargs call help(plotly.graph_objs.Scatter) :rtype (dict): returns a representation of quiver figure. Example 1: Trivial Quiver ``` import plotly.plotly as py from plotly.figure_factory import create_quiver import math # 1 Arrow from (0,0) to (1,1) fig = create_quiver(x=[0], y=[0], u=[1], v=[1], scale=1) py.plot(fig, filename='quiver') ``` Example 2: Quiver plot using meshgrid ``` import plotly.plotly as py from plotly.figure_factory import create_quiver import numpy as np import math # Add data x,y = np.meshgrid(np.arange(0, 2, .2), np.arange(0, 2, .2)) u = np.cos(x)*y v = np.sin(x)*y #Create quiver fig = create_quiver(x, y, u, v) # Plot py.plot(fig, filename='quiver') ``` Example 3: Styling the quiver plot ``` import plotly.plotly as py from plotly.figure_factory import create_quiver import numpy as np import math # Add data x, y = np.meshgrid(np.arange(-np.pi, math.pi, .5), np.arange(-math.pi, math.pi, .5)) u = np.cos(x)*y v = np.sin(x)*y # Create quiver fig = create_quiver(x, y, u, v, scale=.2, arrow_scale=.3, angle=math.pi/6, name='Wind Velocity', line=dict(width=1)) # Add title to layout fig['layout'].update(title='Quiver Plot') # Plot py.plot(fig, filename='quiver') ``` Example 4: Forcing a fix scale ratio to maintain the arrow length ``` import plotly.plotly as py from plotly.figure_factory import create_quiver import numpy as np # Add data x,y = np.meshgrid(np.arange(0.5, 3.5, .5), np.arange(0.5, 4.5, .5)) u = x v = y angle = np.arctan(v / u) norm = 0.25 u = norm * np.cos(angle) v = norm * np.sin(angle) # Create quiver with a fix scale ratio fig = create_quiver(x, y, u, v, scale = 1, scaleratio = 0.5) # Plot py.plot(fig, filename='quiver') ``` """ utils.validate_equal_length(x, y, u, v) utils.validate_positive_scalars(arrow_scale=arrow_scale, scale=scale) if scaleratio is None: quiver_obj = _Quiver(x, y, u, v, scale, arrow_scale, angle) else: quiver_obj = _Quiver(x, y, u, v, scale, arrow_scale, angle, scaleratio) barb_x, barb_y = quiver_obj.get_barbs() arrow_x, arrow_y = quiver_obj.get_quiver_arrows() quiver_plot = graph_objs.Scatter(x=barb_x + arrow_x, y=barb_y + arrow_y, mode='lines', **kwargs) data = [quiver_plot] if scaleratio is None: layout = graph_objs.Layout(hovermode='closest') else: layout = graph_objs.Layout( hovermode='closest', yaxis=dict( scaleratio = scaleratio, scaleanchor = "x" ) ) return graph_objs.Figure(data=data, layout=layout)
def create_quiver(x, y, u, v, scale=.1, arrow_scale=.3, angle=math.pi / 9, **kwargs): """ Returns data for a quiver plot. :param (list|ndarray) x: x coordinates of the arrow locations :param (list|ndarray) y: y coordinates of the arrow locations :param (list|ndarray) u: x components of the arrow vectors :param (list|ndarray) v: y components of the arrow vectors :param (float in [0,1]) scale: scales size of the arrows(ideally to avoid overlap). Default = .1 :param (float in [0,1]) arrow_scale: value multiplied to length of barb to get length of arrowhead. Default = .3 :param (angle in radians) angle: angle of arrowhead. Default = pi/9 :param kwargs: kwargs passed through plotly.graph_objs.Scatter for more information on valid kwargs call help(plotly.graph_objs.Scatter) :rtype (dict): returns a representation of quiver figure. Example 1: Trivial Quiver ``` import plotly.plotly as py from plotly.figure_factory import create_quiver import math # 1 Arrow from (0,0) to (1,1) fig = create_quiver(x=[0], y=[0], u=[1], v=[1], scale=1) py.plot(fig, filename='quiver') ``` Example 2: Quiver plot using meshgrid ``` import plotly.plotly as py from plotly.figure_factory import create_quiver import numpy as np import math # Add data x,y = np.meshgrid(np.arange(0, 2, .2), np.arange(0, 2, .2)) u = np.cos(x)*y v = np.sin(x)*y #Create quiver fig = create_quiver(x, y, u, v) # Plot py.plot(fig, filename='quiver') ``` Example 3: Styling the quiver plot ``` import plotly.plotly as py from plotly.figure_factory import create_quiver import numpy as np import math # Add data x, y = np.meshgrid(np.arange(-np.pi, math.pi, .5), np.arange(-math.pi, math.pi, .5)) u = np.cos(x)*y v = np.sin(x)*y # Create quiver fig = create_quiver(x, y, u, v, scale=.2, arrow_scale=.3, angle=math.pi/6, name='Wind Velocity', line=Line(width=1)) # Add title to layout fig['layout'].update(title='Quiver Plot') # Plot py.plot(fig, filename='quiver') ``` """ utils.validate_equal_length(x, y, u, v) utils.validate_positive_scalars(arrow_scale=arrow_scale, scale=scale) barb_x, barb_y = _Quiver(x, y, u, v, scale, arrow_scale, angle).get_barbs() arrow_x, arrow_y = _Quiver(x, y, u, v, scale, arrow_scale, angle).get_quiver_arrows() quiver = graph_objs.Scatter(x=barb_x + arrow_x, y=barb_y + arrow_y, mode='lines', **kwargs) data = [quiver] layout = graph_objs.Layout(hovermode='closest') return graph_objs.Figure(data=data, layout=layout)
def create_streamline(x, y, u, v, density=1, angle=math.pi / 9, arrow_scale=0.09, **kwargs): """ Returns data for a streamline plot. :param (list|ndarray) x: 1 dimensional, evenly spaced list or array :param (list|ndarray) y: 1 dimensional, evenly spaced list or array :param (ndarray) u: 2 dimensional array :param (ndarray) v: 2 dimensional array :param (float|int) density: controls the density of streamlines in plot. This is multiplied by 30 to scale similiarly to other available streamline functions such as matplotlib. Default = 1 :param (angle in radians) angle: angle of arrowhead. Default = pi/9 :param (float in [0,1]) arrow_scale: value to scale length of arrowhead Default = .09 :param kwargs: kwargs passed through plotly.graph_objs.Scatter for more information on valid kwargs call help(plotly.graph_objs.Scatter) :rtype (dict): returns a representation of streamline figure. Example 1: Plot simple streamline and increase arrow size >>> from plotly.figure_factory import create_streamline >>> import plotly.graph_objects as go >>> import numpy as np >>> import math >>> # Add data >>> x = np.linspace(-3, 3, 100) >>> y = np.linspace(-3, 3, 100) >>> Y, X = np.meshgrid(x, y) >>> u = -1 - X**2 + Y >>> v = 1 + X - Y**2 >>> u = u.T # Transpose >>> v = v.T # Transpose >>> # Create streamline >>> fig = create_streamline(x, y, u, v, arrow_scale=.1) >>> fig.show() Example 2: from nbviewer.ipython.org/github/barbagroup/AeroPython >>> from plotly.figure_factory import create_streamline >>> import numpy as np >>> import math >>> # Add data >>> N = 50 >>> x_start, x_end = -2.0, 2.0 >>> y_start, y_end = -1.0, 1.0 >>> x = np.linspace(x_start, x_end, N) >>> y = np.linspace(y_start, y_end, N) >>> X, Y = np.meshgrid(x, y) >>> ss = 5.0 >>> x_s, y_s = -1.0, 0.0 >>> # Compute the velocity field on the mesh grid >>> u_s = ss/(2*np.pi) * (X-x_s)/((X-x_s)**2 + (Y-y_s)**2) >>> v_s = ss/(2*np.pi) * (Y-y_s)/((X-x_s)**2 + (Y-y_s)**2) >>> # Create streamline >>> fig = create_streamline(x, y, u_s, v_s, density=2, name='streamline') >>> # Add source point >>> point = go.Scatter(x=[x_s], y=[y_s], mode='markers', ... marker_size=14, name='source point') >>> fig.add_trace(point) # doctest: +SKIP >>> fig.show() """ utils.validate_equal_length(x, y) utils.validate_equal_length(u, v) validate_streamline(x, y) utils.validate_positive_scalars(density=density, arrow_scale=arrow_scale) streamline_x, streamline_y = _Streamline(x, y, u, v, density, angle, arrow_scale).sum_streamlines() arrow_x, arrow_y = _Streamline(x, y, u, v, density, angle, arrow_scale).get_streamline_arrows() streamline = graph_objs.Scatter(x=streamline_x + arrow_x, y=streamline_y + arrow_y, mode="lines", **kwargs) data = [streamline] layout = graph_objs.Layout(hovermode="closest") return graph_objs.Figure(data=data, layout=layout)