From 1b1b7f68b249f3bab501b6f1aa4608be2e8d1351 Mon Sep 17 00:00:00 2001 From: Chelsea Date: 2015年5月25日 15:52:40 -0400 Subject: [PATCH 01/34] Add Quiver and Streamline Functions Quiver function plots velocity vectors and Streamline function plots streamlines with directional arrows --- plotly/tools.py | 511 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 511 insertions(+) diff --git a/plotly/tools.py b/plotly/tools.py index 00cfb51f66..a28ad7b36d 100644 --- a/plotly/tools.py +++ b/plotly/tools.py @@ -15,6 +15,8 @@ import six import requests +import numpy as np + from plotly import utils from plotly import exceptions from plotly import session @@ -1269,3 +1271,512 @@ def return_figure_from_figure_or_data(figure_or_data, validate_figure): ) return figure + +### Quiver Plots ### +def Quiver(X, Y, U, V, **kwargs): + """ Return data and layout objects for quiver plot + Example 1: + # X and Y are the coordinates of the arrow locations (created from numpy meshgrid), + # and U and V give the X and Y components of the arrow vectors. + X,Y = np.meshgrid(np.arange(0,np.pi,.2),np.arange(0,np.pi,.2) ) + U = np.cos(X) + V = np.sin(Y) + data, layout = Quiver(X,Y,U,V) + figure = Figure(data = data, layout = layout) + plot_url = py.plot(figure, filename = 'QuiverEx1') + Example 2: + X,Y = np.meshgrid(np.arange(0,2,.2),np.arange(0,2,.2)) + U = np.cos(X)*Y; + V = np.sin(X)*Y; + data, layout = Quiver(X, Y, U, V) + figure = Figure(data = data, layout = layout) + plot_url = py.plot(figure, filename = 'QuiverEx2') + + Keywords arguments with constant defaults: + angle (kwarg, angle in radians, default = np.pi/9): + Angle of arrowhead. + scale (kwarg, float in [0,1], default = .1): + Scales size of the arrows (avoid overlap). + arrow_length (kwarg, float in [0,1], default = 0.3): + Value multiplied to length of barb to get length of arrowhead. + color (kwarg, rgb vale, default = rgb(114, 132, 304)): + Set color of both barb and arrow. + barb_color (kwarg, rgb vale, default = color): + Set color of barbs. + arrow_color (kwarg, rgb vale, default = color): + Set color of arrow. + width (kwarg, int greater than or equal to 1, default = 1): + Set width of lines for barbs and arrows. + barb_width (kwarg, int greater than or equal to 1, default = width): + Change width of lines for barbs. + arrow_width (kwarg, int greater than or equal to 1, default = width): + Change width of lines for arrows. + title (kwarg, string, default=none): + Title of plot. + xlabel (kwarg, string, default=none): + Label of x-axis + ylabel (kwarg, string, default=none): + Label of y-axis + hover (kwarg, string, default=closest) + Hover options, default to show closest hover + """ + angle = kwargs.pop('angle', np.pi/9) + scale = kwargs.pop('scale', .1) + arrow_len = kwargs.pop('arrow_length', .3) + color = kwargs.pop('color', 'rgb(114, 132, 314)') + barb_col = kwargs.pop('barb_color', color) + arrow_col = kwargs.pop('arrow_color', color) + width = kwargs.pop('width', 1) + arrow_width = kwargs.pop('arrow_width', width) + barb_width = kwargs.pop('barb_width', width) + title = kwargs.pop('title','') + xlab = kwargs.pop('xlabel','') + ylab = kwargs.pop('ylabel','') + hover = kwargs.pop('hover','closest') + + # Make array of x start values + Xstart = np.array(X) + Xstart = Xstart.flatten('F') + + # Make array of x end values + Xend = X + U * scale + Xend = np.array(Xend) + Xend = Xend.flatten('F') + + # Make array of nans + ## These are used as spaces so the separate + ## arrows can be included in 1 trace + nnn = np.empty((len(Xstart))) + nnn[:] = np.NAN + + # Combine arrays into matrix + Xvals = np.matrix([Xstart, Xend, nnn]) + # Make matrix into array readable by plotly + Xvals = np.array(Xvals) + Xvals = Xvals.flatten('F') + + # Make array of y start values + Ystart = np.array(Y) + Ystart = Ystart.flatten('F') + + # Make array of y end values + Yend = Y + V * scale + Yend = np.array(Yend) + Yend = Yend.flatten('F') + + # Combine arrays into matrix + Yvals=np.matrix([Ystart, Yend, nnn]) + # Make matrix into array readable by plotly + Yvals = np.array(Yvals) + Yvals = Yvals.flatten('F') + + # Make Trace1: the lines + trace1 = Scatter(x = Xvals, y = Yvals, + mode = 'lines', name = 'Barb', + line = Line(width = barb_width, color = barb_col)) + + # ARROWS! + # Get line lengths + # Default arrow length = 30% of line length + Xdif = Xend - Xstart + Ydif = Yend - Ystart + LineLen = np.sqrt(np.square(Xdif) + np.square(Ydif)) + ArrowLen = LineLen * arrow_len + + # Get angle of line + LineAng = np.arctan(Ydif / Xdif) + + # Set angles to create arrow + # Default angle = +/- 20 degrees + Ang1 = LineAng + (angle) + Ang2 = LineAng - (angle) + + XSeg1 = np.cos(Ang1) * ArrowLen + YSeg1 = np.sin(Ang1) * ArrowLen + + XSeg2 = np.cos(Ang2) * ArrowLen + YSeg2 = np.sin(Ang2) * ArrowLen + + XPoint1 = np.empty((len(Xdif))) + YPoint1 = np.empty((len(Ydif))) + + XPoint2 = np.empty((len(Xdif))) + YPoint2 = np.empty((len(Ydif))) + + for index in range(len(Xdif)): + if Xdif[index]>= 0: + XPoint1[index] = Xend[index] - XSeg1[index] + YPoint1[index] = Yend[index] - YSeg1[index] + XPoint2[index] = Xend[index] - XSeg2[index] + YPoint2[index] = Yend[index] - YSeg2[index] + else: + XPoint1[index] = Xend[index] + XSeg1[index] + YPoint1[index] = Yend[index] + YSeg1[index] + XPoint2[index] = Xend[index] + XSeg2[index] + YPoint2[index] = Yend[index] + YSeg2[index] + + # Combine arrays into matrix + XArrows = np.matrix([XPoint1, Xend, XPoint2, nnn]) + # Make matrix into array readable by plotly + XArrows = np.array(XArrows) + XArrows = XArrows.flatten('F') + + # Combine arrays into matrix + YArrows = np.matrix([YPoint1, Yend, YPoint2, nnn]) + # Make matrix into array readable by plotly + YArrows = np.array(YArrows) + YArrows = YArrows.flatten('F') + + # Make trace2: the arrows + trace2 = Scatter(x = XArrows, y = YArrows, + mode = 'lines', name = 'Arrow', + line = Line(width = arrow_width, color = arrow_col)) + + # Data + data = Data([trace1, trace2]) + # Layout + layout = Layout(title = title, xaxis = XAxis(title = xlab), + yaxis = YAxis(title = ylab), hovermode = hover) + + return data, layout + +### Streamline Plots ### +def Streamline(x, y, u, v, **kwargs): + """Returns data and layout objects to plot streamlines of a vector flow. + + Example 1: + # x and y are 1d arrays defining an *evenly spaced* grid. + # u and v are 2d arrays (shape [y,x]) giving velocities. + # 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 + # Run streamline function + data, layout = Streamline(x, y, u, v, arrow_length = .1) + # Plot + figure=Figure(data=data, layout=layout) + url=py.plot(figure, filename = 'Streamline1') + + Example 2: + # from http://nbviewer.ipython.org/github/barbagroup/AeroPython/blob/master/lessons/01_Lesson01_sourceSink.ipynb + # Enter 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) + strength_source = 5.0 + x_source, y_source = -1.0, 0.0 + # Computes the velocity field on the mesh grid + u_source = strength_source/(2*np.pi) * (X-x_source)/((X-x_source)**2 + (Y-y_source)**2) + v_source = strength_source/(2*np.pi) * (Y-y_source)/((X-x_source)**2 + (Y-y_source)**2) + # Run streamline function + data, layout = Streamline(x, y, u_source, v_source) + # Add Source Point to data + trace2 = Scatter(x=[x_source], y=[y_source], mode='markers', marker=Marker(size=14), name='Source Point') + data = data + Data([trace2]) + # Plot + figure = Figure(data=data, layout=layout) + url = py.plot(figure, filename='Streamline_Source') + + Keywords arguments with constant defaults: + density (kwarg, integer, default=1): + density controls the closeness of the streamlines. + angle (kwarg, angle in radians, default = np.pi/9): + Angle of arrowhead. + arrow_length (kwarg, float in [0,1], default = 0.08): + Value multiplied to length of barb to get length of arrowhead. + color (kwarg, rgb vale, default = rgb(114, 132, 304)): + Set color of both barb and arrow. + barb_color (kwarg, rgb vale, default = color): + Set color of barbs. + arrow_color (kwarg, rgb vale, default = color): + Set color of arrow. + width (kwarg, int greater than or equal to 1, default = 1): + Set width of lines for barbs and arrows. + barb_width (kwarg, int greater than or equal to 1, default = width): + Change width of lines for barbs. + arrow_width (kwarg, int greater than or equal to 1, default = width): + Change width of lines for arrows. + title (kwarg, string, default=none): + Title of plot. + xlabel (kwarg, string, default=none): + Label of x-axis + ylabel (kwarg, string, default=none): + Label of y-axis + hover (kwarg, string, default=closest) + Hover options, default to show closest hover + """ + + density = kwargs.pop('density', 1) + angle = kwargs.pop('angle', np.pi/9) + arrow_len = kwargs.pop('arrow_length', .08) + color = kwargs.pop('color', 'rgb(114, 132, 304)') + stream_col = kwargs.pop('stream_color', color) + arrow_col = kwargs.pop('arrow_color', color) + width = kwargs.pop('width', 1) + arrow_width = kwargs.pop('arrow_width', width) + stream_width = kwargs.pop('stream_width', width) + title = kwargs.pop('title','') + xlab = kwargs.pop('xlabel','') + ylab = kwargs.pop('ylabel','') + hover = kwargs.pop('hover','closest') + + # Set up some constants - size of the grid used. + NGX = len(x) + NGY = len(y) + + # Constants used to convert between grid index coords and user coords. + DX = x[1] - x[0] + DY = y[1] - y[0] + XOFF = x[0] + YOFF = y[0] + + # Now rescale velocity onto axes-coordinates + u = u/(x[-1]-x[0]) + v = v/(y[-1]-y[0]) + speed = np.sqrt(u*u + v*v) + # s (path length) will now be in axes-coordinates + # rescale u for integrations. + u *= NGX + v *= NGY + # Now u and v in grid-coordinates. + + NBX = int(30*density) + NBY = int(30*density) + blank = np.zeros((NBY,NBX)) + + bx_spacing = NGX/float(NBX-1) + by_spacing = NGY/float(NBY-1) + + def blank_pos(xi, yi): + return int((xi/bx_spacing)+0.5), int((yi/by_spacing)+0.5) + + def value_at(a, xi, yi): + if type(xi) == np.ndarray: + x = xi.astype(np.int) + y = yi.astype(np.int) + else: + x = np.int(xi) + y = np.int(yi) + a00 = a[y, x] + a01 = a[y, x+1] + a10 = a[y+1, x] + a11 = a[y+1, x+1] + xt = xi-x + yt = yi-y + a0 = a00*(1-xt) + a01*xt + a1 = a10*(1-xt) + a11*xt + return a0*(1-yt) + a1*yt + + # RK4 forward and back trajectories from the initial conditions, + # with the odd 'blank array' termination conditions. + def rk4_integrate(x0, y0): + + def f(xi, yi): + dt_ds = 1./value_at(speed, xi, yi) + ui = value_at(u, xi, yi) + vi = value_at(v, xi, yi) + return ui*dt_ds, vi*dt_ds + + def g(xi, yi): + dt_ds = 1./value_at(speed, xi, yi) + ui = value_at(u, xi, yi) + vi = value_at(v, xi, yi) + return -ui*dt_ds, -vi*dt_ds + + check = lambda xi, yi: xi>=0 and xi=0 and yi 2: + break + return stotal, xf_traj, yf_traj + + integrator = rk4 + + sf, xf_traj, yf_traj = integrator(x0, y0, f) + sb, xb_traj, yb_traj = integrator(x0, y0, g) + stotal = sf + sb + x_traj = xb_traj[::-1] + xf_traj[1:] + y_traj = yb_traj[::-1] + yf_traj[1:] + + # Tests to check length of traj. Remember, s in units of axes. + if len(x_traj) < 1: return None + if stotal> .2: + initxb, inityb = blank_pos(x0, y0) + blank[inityb, initxb] = 1 + return x_traj, y_traj + else: + for xb, yb in zip(bx_changes, by_changes): + blank[yb, xb] = 0 + return None + + # A quick function for integrating trajectories if blank==0. + trajectories = [] + def traj(xb, yb): + if xb < 0 or xb>= NBX or yb < 0 or yb>= NBY: + return + if blank[yb, xb] == 0: + t = rk4_integrate(xb*bx_spacing, yb*by_spacing) + if t != None: + trajectories.append(t) + + # Build up the trajectory set. + for indent in range((max(NBX,NBY))//2): + for xi in range(max(NBX,NBY)-2*indent): + traj(xi+indent, indent) + traj(xi+indent, NBY-1-indent) + traj(indent, xi+indent) + traj(NBX-1-indent, xi+indent) + + xs = [np.array(t[0])*DX+XOFF for t in trajectories] + ys = [np.array(t[1])*DY+YOFF for t in trajectories] + + # Below edited to make a list readable by plotly + # and add one nan to the end of each list to plot + # multiple "streamlines" in one trace + + xspl = xs + yspl = ys + + for index in range(len(xspl)): + xspl[index] = xspl[index].tolist() + xspl[index].append(np.nan) + + + for index in range(len(yspl)): + yspl[index] = yspl[index].tolist() + yspl[index].append(np.nan) + + # xs & ys are lists of lists-> combine each into 1 list + # This makes all the steamlines into 1 trace + xspl = sum(xspl, []) + yspl = sum(yspl, []) + + + #ARROWS + + XMid = np.empty((len(xs))) + YMid = np.empty((len(ys))) + XStart = np.empty((len(xs))) + YStart = np.empty((len(ys))) + # Find slopes between point a (mid point + 2) and + # point b (midpoint -2) + for index in range(len(xs)): + XMid[index] = xs[index][(len(xs[index])/2)+2] + XStart[index] = xs[index][(len(xs[index])/2)-2] + YMid[index] = ys[index][(len(ys[index])/2)+2] + YStart[index] = ys[index][(len(ys[index])/2)-2] + + Xdif = XMid - XStart + Ydif = YMid - YStart + + # Get angle of line + LineAng = np.arctan(Ydif/Xdif) + + # Set angles to create arrow + # Currently set to +/- 20 degrees + Ang1 = LineAng + (angle) + Ang2 = LineAng - (angle) + + XSeg1 = np.cos(Ang1)*arrow_len + YSeg1 = np.sin(Ang1)*arrow_len + + XSeg2 = np.cos(Ang2)*arrow_len + YSeg2 = np.sin(Ang2)*arrow_len + + XPoint1 = np.empty((len(Xdif))) + YPoint1 = np.empty((len(Ydif))) + + XPoint2 = np.empty((len(Xdif))) + YPoint2 = np.empty((len(Ydif))) + + for index in range(len(Xdif)): + if Xdif[index]>= 0: + XPoint1[index] = XMid[index] - XSeg1[index] + YPoint1[index] = YMid[index] - YSeg1[index] + XPoint2[index] = XMid[index] - XSeg2[index] + YPoint2[index] = YMid[index] - YSeg2[index] + else: + XPoint1[index] = XMid[index] + XSeg1[index] + YPoint1[index] = YMid[index] + YSeg1[index] + XPoint2[index] = XMid[index] + XSeg2[index] + YPoint2[index] = YMid[index] + YSeg2[index] + + nnn = np.empty((len(YMid))) + nnn[:] = np.NAN + + # Combine arrays into matrix + XArrows = np.matrix([XPoint1, XMid, XPoint2, nnn]) + # Make matrix into array readable by plotly + XArrows = np.array(XArrows) + XArrows = XArrows.flatten('F') + + # Combine arrays into matrix + YArrows = np.matrix([YPoint1, YMid, YPoint2, nnn]) + # Make matrix into array readable by plotly + YArrows = np.array(YArrows) + YArrows = YArrows.flatten('F') + + # Data + trace1 = Scatter(x=xspl, y=yspl, + mode='lines', name='Streamline', + line=Line(width=stream_width, color=stream_col)) + trace2 = Scatter(x=XArrows, y=YArrows, + mode='lines', name='Arrow', + line=Line(width=arrow_width, color=arrow_col)) + data = Data([trace1, trace2]) + + # Layout + layout = Layout(title=title, xaxis=XAxis(title=xlab), + yaxis=YAxis(title=ylab), hovermode=hover) + + return data, layout + + From 67f825444e2a15704dd84ebd8223aab49056308b Mon Sep 17 00:00:00 2001 From: Chelsea Date: 2015年5月27日 15:58:22 -0400 Subject: [PATCH 02/34] Quiver and Streamline Edits clean up --- plotly/tools.py | 378 ++++++++++++++++++++++++------------------------ 1 file changed, 189 insertions(+), 189 deletions(-) diff --git a/plotly/tools.py b/plotly/tools.py index a28ad7b36d..0d6e41fa92 100644 --- a/plotly/tools.py +++ b/plotly/tools.py @@ -15,8 +15,6 @@ import six import requests -import numpy as np - from plotly import utils from plotly import exceptions from plotly import session @@ -1272,137 +1270,138 @@ def return_figure_from_figure_or_data(figure_or_data, validate_figure): return figure -### Quiver Plots ### -def Quiver(X, Y, U, V, **kwargs): - """ Return data and layout objects for quiver plot + +def Quiver(x, y, u, v, + angle=np.pi/9, scale=.1, + arrow_length=.3, color='rgb(114, 132, 314)', + barb_color=color, arrow_color=color, + width=1, arrow_width=width, + barb_width=width, **kwargs): + """Return a data object for a quiver plot where x and y are the coordinates + of the arrow locations and u and v give the x and y components of the arrow + vectors. + Example 1: - # X and Y are the coordinates of the arrow locations (created from numpy meshgrid), - # and U and V give the X and Y components of the arrow vectors. - X,Y = np.meshgrid(np.arange(0,np.pi,.2),np.arange(0,np.pi,.2) ) - U = np.cos(X) - V = np.sin(Y) - data, layout = Quiver(X,Y,U,V) - figure = Figure(data = data, layout = layout) - plot_url = py.plot(figure, filename = 'QuiverEx1') + x,y = np.meshgrid(np.arange(0,np.pi,.2),np.arange(0,np.pi,.2) ) + u = np.cos(x) + v = np.sin(y) + data = Quiver(x,y,u,v) + Example 2: - X,Y = np.meshgrid(np.arange(0,2,.2),np.arange(0,2,.2)) - U = np.cos(X)*Y; - V = np.sin(X)*Y; - data, layout = Quiver(X, Y, U, V) - figure = Figure(data = data, layout = layout) - plot_url = py.plot(figure, filename = 'QuiverEx2') + x,y = np.meshgrid(np.arange(0,2,.2),np.arange(0,2,.2)) + u = np.cos(x)*y; + v = np.sin(x)*y; + data = Quiver(x, y, u, v) Keywords arguments with constant defaults: + angle (kwarg, angle in radians, default = np.pi/9): Angle of arrowhead. + scale (kwarg, float in [0,1], default = .1): Scales size of the arrows (avoid overlap). + arrow_length (kwarg, float in [0,1], default = 0.3): Value multiplied to length of barb to get length of arrowhead. - color (kwarg, rgb vale, default = rgb(114, 132, 304)): + + color (kwarg, color string, default = rgb(114, 132, 304)): Set color of both barb and arrow. - barb_color (kwarg, rgb vale, default = color): + + barb_color (kwarg, color string, default = color): Set color of barbs. - arrow_color (kwarg, rgb vale, default = color): + + arrow_color (kwarg, color string, default = color): Set color of arrow. + width (kwarg, int greater than or equal to 1, default = 1): Set width of lines for barbs and arrows. + barb_width (kwarg, int greater than or equal to 1, default = width): Change width of lines for barbs. + arrow_width (kwarg, int greater than or equal to 1, default = width): Change width of lines for arrows. - title (kwarg, string, default=none): - Title of plot. - xlabel (kwarg, string, default=none): - Label of x-axis - ylabel (kwarg, string, default=none): - Label of y-axis - hover (kwarg, string, default=closest) - Hover options, default to show closest hover """ - angle = kwargs.pop('angle', np.pi/9) - scale = kwargs.pop('scale', .1) - arrow_len = kwargs.pop('arrow_length', .3) - color = kwargs.pop('color', 'rgb(114, 132, 314)') - barb_col = kwargs.pop('barb_color', color) - arrow_col = kwargs.pop('arrow_color', color) - width = kwargs.pop('width', 1) - arrow_width = kwargs.pop('arrow_width', width) - barb_width = kwargs.pop('barb_width', width) - title = kwargs.pop('title','') - xlab = kwargs.pop('xlabel','') - ylab = kwargs.pop('ylabel','') - hover = kwargs.pop('hover','closest') - - # Make array of x start values - Xstart = np.array(X) + + # angle = kwargs.pop('angle', np.pi/9) + # scale = kwargs.pop('scale', .1) + # arrow_length = kwargs.pop('arrow_length', .3) + # color = kwargs.pop('color', 'rgb(114, 132, 314)') + # barb_col = kwargs.pop('barb_color', color) + # arrow_col = kwargs.pop('arrow_color', color) + # width = kwargs.pop('width', 1) + # arrow_width = kwargs.pop('arrow_width', width) + # barb_width = kwargs.pop('barb_width', width) + + # Make array of x start values + Xstart = np.array(x) Xstart = Xstart.flatten('F') - - # Make array of x end values - Xend = X + U * scale + + # Make array of x end values + Xend = x + u * scale Xend = np.array(Xend) Xend = Xend.flatten('F') # Make array of nans - ## These are used as spaces so the separate - ## arrows can be included in 1 trace + # These are used as spaces so the separate + # arrows can be included in 1 trace nnn = np.empty((len(Xstart))) nnn[:] = np.NAN - + # Combine arrays into matrix Xvals = np.matrix([Xstart, Xend, nnn]) # Make matrix into array readable by plotly Xvals = np.array(Xvals) Xvals = Xvals.flatten('F') - - # Make array of y start values + + # Make array of y start values Ystart = np.array(Y) Ystart = Ystart.flatten('F') - # Make array of y end values - Yend = Y + V * scale + # Make array of y end values + Yend = y + v * scale Yend = np.array(Yend) Yend = Yend.flatten('F') # Combine arrays into matrix - Yvals=np.matrix([Ystart, Yend, nnn]) + Yvals = np.matrix([Ystart, Yend, nnn]) # Make matrix into array readable by plotly Yvals = np.array(Yvals) Yvals = Yvals.flatten('F') - + # Make Trace1: the lines - trace1 = Scatter(x = Xvals, y = Yvals, - mode = 'lines', name = 'Barb', - line = Line(width = barb_width, color = barb_col)) - + trace1 = Scatter(x=Xvals, y=Yvals, + mode='lines', name='Barb', + line=Line(width=barb_width, color=barb_color)) + # ARROWS! # Get line lengths # Default arrow length = 30% of line length Xdif = Xend - Xstart Ydif = Yend - Ystart LineLen = np.sqrt(np.square(Xdif) + np.square(Ydif)) - ArrowLen = LineLen * arrow_len - + ArrowLen = LineLen * arrow_length + # Get angle of line LineAng = np.arctan(Ydif / Xdif) - + # Set angles to create arrow # Default angle = +/- 20 degrees Ang1 = LineAng + (angle) Ang2 = LineAng - (angle) - + XSeg1 = np.cos(Ang1) * ArrowLen YSeg1 = np.sin(Ang1) * ArrowLen - + XSeg2 = np.cos(Ang2) * ArrowLen YSeg2 = np.sin(Ang2) * ArrowLen - + XPoint1 = np.empty((len(Xdif))) YPoint1 = np.empty((len(Ydif))) - + XPoint2 = np.empty((len(Xdif))) YPoint2 = np.empty((len(Ydif))) - + for index in range(len(Xdif)): if Xdif[index]>= 0: XPoint1[index] = Xend[index] - XSeg1[index] @@ -1414,39 +1413,41 @@ def Quiver(X, Y, U, V, **kwargs): YPoint1[index] = Yend[index] + YSeg1[index] XPoint2[index] = Xend[index] + XSeg2[index] YPoint2[index] = Yend[index] + YSeg2[index] - + # Combine arrays into matrix XArrows = np.matrix([XPoint1, Xend, XPoint2, nnn]) # Make matrix into array readable by plotly XArrows = np.array(XArrows) XArrows = XArrows.flatten('F') - + # Combine arrays into matrix YArrows = np.matrix([YPoint1, Yend, YPoint2, nnn]) # Make matrix into array readable by plotly YArrows = np.array(YArrows) YArrows = YArrows.flatten('F') - + # Make trace2: the arrows - trace2 = Scatter(x = XArrows, y = YArrows, - mode = 'lines', name = 'Arrow', - line = Line(width = arrow_width, color = arrow_col)) - + trace2 = Scatter(x=XArrows, y=YArrows, + mode='lines', name='Arrow', + line=Line(width=arrow_width, color=arrow_color)) + # Data data = Data([trace1, trace2]) - # Layout - layout = Layout(title = title, xaxis = XAxis(title = xlab), - yaxis = YAxis(title = ylab), hovermode = hover) - - return data, layout - -### Streamline Plots ### -def Streamline(x, y, u, v, **kwargs): - """Returns data and layout objects to plot streamlines of a vector flow. - + + return data + + +def Streamline(x, y, u, v, + density=1, angle=np.pi/9, + arrow_length=.08, color='rgb(114, 132, 304)', + stream_color=color, arrow_color=color, + width=1, arrow_width=width, + stream_width=width, **kwargs): + '''Return a data object to plot streamlines of a vector flow. + x and y are 1d arrays that define an EVENLY spaced grid. + u and v are 2d arrays (shape [y,x]) giving velocities. + Example 1: - # x and y are 1d arrays defining an *evenly spaced* grid. - # u and v are 2d arrays (shape [y,x]) giving velocities. # Add data x = np.linspace(-3, 3, 100) y = np.linspace(-3, 3, 100) @@ -1455,78 +1456,82 @@ def Streamline(x, y, u, v, **kwargs): v = 1 + X - Y**2 u = u.T #transpose v = v.T #transpose - # Run streamline function - data, layout = Streamline(x, y, u, v, arrow_length = .1) - # Plot - figure=Figure(data=data, layout=layout) - url=py.plot(figure, filename = 'Streamline1') - + + # Streamline function + data = Streamline(x, y, u, v, arrow_length = .1) + Example 2: - # from http://nbviewer.ipython.org/github/barbagroup/AeroPython/blob/master/lessons/01_Lesson01_sourceSink.ipynb - # Enter 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) - strength_source = 5.0 - x_source, y_source = -1.0, 0.0 - # Computes the velocity field on the mesh grid - u_source = strength_source/(2*np.pi) * (X-x_source)/((X-x_source)**2 + (Y-y_source)**2) - v_source = strength_source/(2*np.pi) * (Y-y_source)/((X-x_source)**2 + (Y-y_source)**2) - # Run streamline function - data, layout = Streamline(x, y, u_source, v_source) - # Add Source Point to data - trace2 = Scatter(x=[x_source], y=[y_source], mode='markers', marker=Marker(size=14), name='Source Point') + # from http://nbviewer.ipython.org/github/barbagroup/AeroPython + + # 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) + strength_source = 5.0 + x_source, y_source = -1.0, 0.0 + + # Compute the velocity field on the mesh grid + u_source = strength_source/(2*np.pi) * + (X-x_source)/((X-x_source)**2 + + (Y-y_source)**2) + v_source = strength_source/(2*np.pi) * + (Y-y_source)/((X-x_source)**2 + + (Y-y_source)**2) + + # Streamline function + data = Streamline(x, y, u_source, v_source) + + # Add Source Point to data + trace2 = Scatter(x=[x_source], y=[y_source], mode='markers', + marker=Marker(size=14), name='Source Point') data = data + Data([trace2]) + # Plot - figure = Figure(data=data, layout=layout) - url = py.plot(figure, filename='Streamline_Source') + url = py.plot(data, filename='Streamline_Source') Keywords arguments with constant defaults: + density (kwarg, integer, default=1): density controls the closeness of the streamlines. + angle (kwarg, angle in radians, default = np.pi/9): Angle of arrowhead. + arrow_length (kwarg, float in [0,1], default = 0.08): Value multiplied to length of barb to get length of arrowhead. - color (kwarg, rgb vale, default = rgb(114, 132, 304)): + + color (kwarg, color string, default = rgb(114, 132, 304)): Set color of both barb and arrow. - barb_color (kwarg, rgb vale, default = color): + + barb_color (kwarg, color string, default = color): Set color of barbs. - arrow_color (kwarg, rgb vale, default = color): + + arrow_color (kwarg, color string, default = color): Set color of arrow. + width (kwarg, int greater than or equal to 1, default = 1): Set width of lines for barbs and arrows. + barb_width (kwarg, int greater than or equal to 1, default = width): Change width of lines for barbs. + arrow_width (kwarg, int greater than or equal to 1, default = width): Change width of lines for arrows. - title (kwarg, string, default=none): - Title of plot. - xlabel (kwarg, string, default=none): - Label of x-axis - ylabel (kwarg, string, default=none): - Label of y-axis - hover (kwarg, string, default=closest) - Hover options, default to show closest hover - """ - - density = kwargs.pop('density', 1) - angle = kwargs.pop('angle', np.pi/9) - arrow_len = kwargs.pop('arrow_length', .08) - color = kwargs.pop('color', 'rgb(114, 132, 304)') - stream_col = kwargs.pop('stream_color', color) - arrow_col = kwargs.pop('arrow_color', color) - width = kwargs.pop('width', 1) - arrow_width = kwargs.pop('arrow_width', width) - stream_width = kwargs.pop('stream_width', width) - title = kwargs.pop('title','') - xlab = kwargs.pop('xlabel','') - ylab = kwargs.pop('ylabel','') - hover = kwargs.pop('hover','closest') - + ''' + + # density = kwargs.pop('density', 1) + # angle = kwargs.pop('angle', np.pi/9) + # arrow_len = kwargs.pop('arrow_length', .08) + # color = kwargs.pop('color', 'rgb(114, 132, 304)') + # stream_col = kwargs.pop('stream_color', color) + # arrow_col = kwargs.pop('arrow_color', color) + # width = kwargs.pop('width', 1) + # arrow_width = kwargs.pop('arrow_width', width) + # stream_width = kwargs.pop('stream_width', width) + # Set up some constants - size of the grid used. NGX = len(x) NGY = len(y) @@ -1549,7 +1554,7 @@ def Streamline(x, y, u, v, **kwargs): NBX = int(30*density) NBY = int(30*density) - blank = np.zeros((NBY,NBX)) + blank = np.zeros((NBY, NBX)) bx_spacing = NGX/float(NBX-1) by_spacing = NGY/float(NBY-1) @@ -1574,7 +1579,7 @@ def value_at(a, xi, yi): a1 = a10*(1-xt) + a11*xt return a0*(1-yt) + a1*yt - # RK4 forward and back trajectories from the initial conditions, + # RK4 forward and back trajectories from the initial conditions, # with the odd 'blank array' termination conditions. def rk4_integrate(x0, y0): @@ -1590,14 +1595,15 @@ def g(xi, yi): vi = value_at(v, xi, yi) return -ui*dt_ds, -vi*dt_ds - check = lambda xi, yi: xi>=0 and xi=0 and yi= 0 and xi < NGX-1 + and yi>= 0 and yi < NGY-1 bx_changes = [] by_changes = [] # Integrator function def rk4(x0, y0, f): - ds = 0.01 #min(1./NGX, 1./NGY, 0.01) + ds = 0.01 # Min(1./NGX, 1./NGY, 0.01) stotal = 0 xi = x0 yi = y0 @@ -1620,14 +1626,15 @@ def rk4(x0, y0, f): xi += ds*(k1x+2*k2x+2*k3x+k4x) / 6. yi += ds*(k1y+2*k2y+2*k3y+k4y) / 6. # Final position might be out of the domain - if not check(xi, yi): break + if not check(xi, yi): + break stotal += ds # Next, if s gets to thres, check blank. new_xb, new_yb = blank_pos(xi, yi) if new_xb != xb or new_yb != yb: # New square, so check and colour. Quit if required. - if blank[new_yb,new_xb] == 0: - blank[new_yb,new_xb] = 1 + if blank[new_yb, new_xb] == 0: + blank[new_yb, new_xb] = 1 bx_changes.append(new_xb) by_changes.append(new_yb) xb = new_xb @@ -1647,7 +1654,8 @@ def rk4(x0, y0, f): y_traj = yb_traj[::-1] + yf_traj[1:] # Tests to check length of traj. Remember, s in units of axes. - if len(x_traj) < 1: return None + if len(x_traj) < 1: + return None if stotal> .2: initxb, inityb = blank_pos(x0, y0) blank[inityb, initxb] = 1 @@ -1659,17 +1667,18 @@ def rk4(x0, y0, f): # A quick function for integrating trajectories if blank==0. trajectories = [] + def traj(xb, yb): if xb < 0 or xb>= NBX or yb < 0 or yb>= NBY: return if blank[yb, xb] == 0: t = rk4_integrate(xb*bx_spacing, yb*by_spacing) - if t != None: + if t is not None: trajectories.append(t) # Build up the trajectory set. - for indent in range((max(NBX,NBY))//2): - for xi in range(max(NBX,NBY)-2*indent): + for indent in range((max(NBX, NBY))//2): + for xi in range(max(NBX, NBY)-2*indent): traj(xi+indent, indent) traj(xi+indent, NBY-1-indent) traj(indent, xi+indent) @@ -1677,43 +1686,40 @@ def traj(xb, yb): xs = [np.array(t[0])*DX+XOFF for t in trajectories] ys = [np.array(t[1])*DY+YOFF for t in trajectories] - + # Below edited to make a list readable by plotly - # and add one nan to the end of each list to plot + # and add one nan to the end of each list to plot # multiple "streamlines" in one trace - + xspl = xs yspl = ys - + for index in range(len(xspl)): xspl[index] = xspl[index].tolist() xspl[index].append(np.nan) - - + for index in range(len(yspl)): yspl[index] = yspl[index].tolist() yspl[index].append(np.nan) - # xs & ys are lists of lists-> combine each into 1 list + # xs & ys are lists of lists-> combine each into 1 list # This makes all the steamlines into 1 trace xspl = sum(xspl, []) yspl = sum(yspl, []) - - #ARROWS - + # ARROWS XMid = np.empty((len(xs))) YMid = np.empty((len(ys))) XStart = np.empty((len(xs))) YStart = np.empty((len(ys))) - # Find slopes between point a (mid point + 2) and + # Find slopes between point a (mid point + 2) and # point b (midpoint -2) for index in range(len(xs)): XMid[index] = xs[index][(len(xs[index])/2)+2] XStart[index] = xs[index][(len(xs[index])/2)-2] YMid[index] = ys[index][(len(ys[index])/2)+2] YStart[index] = ys[index][(len(ys[index])/2)-2] - + Xdif = XMid - XStart Ydif = YMid - YStart @@ -1724,16 +1730,16 @@ def traj(xb, yb): # Currently set to +/- 20 degrees Ang1 = LineAng + (angle) Ang2 = LineAng - (angle) - - XSeg1 = np.cos(Ang1)*arrow_len - YSeg1 = np.sin(Ang1)*arrow_len - - XSeg2 = np.cos(Ang2)*arrow_len - YSeg2 = np.sin(Ang2)*arrow_len - + + XSeg1 = np.cos(Ang1)*arrow_length + YSeg1 = np.sin(Ang1)*arrow_length + + XSeg2 = np.cos(Ang2)*arrow_length + YSeg2 = np.sin(Ang2)*arrow_length + XPoint1 = np.empty((len(Xdif))) YPoint1 = np.empty((len(Ydif))) - + XPoint2 = np.empty((len(Xdif))) YPoint2 = np.empty((len(Ydif))) @@ -1748,35 +1754,29 @@ def traj(xb, yb): YPoint1[index] = YMid[index] + YSeg1[index] XPoint2[index] = XMid[index] + XSeg2[index] YPoint2[index] = YMid[index] + YSeg2[index] - + nnn = np.empty((len(YMid))) nnn[:] = np.NAN - + # Combine arrays into matrix XArrows = np.matrix([XPoint1, XMid, XPoint2, nnn]) # Make matrix into array readable by plotly XArrows = np.array(XArrows) XArrows = XArrows.flatten('F') - + # Combine arrays into matrix YArrows = np.matrix([YPoint1, YMid, YPoint2, nnn]) # Make matrix into array readable by plotly YArrows = np.array(YArrows) YArrows = YArrows.flatten('F') - + # Data - trace1 = Scatter(x=xspl, y=yspl, - mode='lines', name='Streamline', - line=Line(width=stream_width, color=stream_col)) - trace2 = Scatter(x=XArrows, y=YArrows, - mode='lines', name='Arrow', - line=Line(width=arrow_width, color=arrow_col)) + trace1 = Scatter(x=xspl, y=yspl, + mode='lines', name='Streamline', + line=Line(width=stream_width, color=stream_color)) + trace2 = Scatter(x=XArrows, y=YArrows, + mode='lines', name='Arrow', + line=Line(width=arrow_width, color=arrow_color)) data = Data([trace1, trace2]) - - # Layout - layout = Layout(title=title, xaxis=XAxis(title=xlab), - yaxis=YAxis(title=ylab), hovermode=hover) - - return data, layout - + return data From 0b73695ec4e27d09805a67138e589df6779e9197 Mon Sep 17 00:00:00 2001 From: Chelsea Date: 2015年5月27日 15:58:41 -0400 Subject: [PATCH 03/34] quiver and streamline tests draft of tests --- plotly/tests/test_core/test_tools/test_Quiver.py | 4 ++++ plotly/tests/test_core/test_tools/test_Streamline.py | 4 ++++ 2 files changed, 8 insertions(+) create mode 100644 plotly/tests/test_core/test_tools/test_Quiver.py create mode 100644 plotly/tests/test_core/test_tools/test_Streamline.py diff --git a/plotly/tests/test_core/test_tools/test_Quiver.py b/plotly/tests/test_core/test_tools/test_Quiver.py new file mode 100644 index 0000000000..948344baeb --- /dev/null +++ b/plotly/tests/test_core/test_tools/test_Quiver.py @@ -0,0 +1,4 @@ +import plotly +from plotly.graph_objs import * +import plotly.tools as tls +from nose.tools import raises diff --git a/plotly/tests/test_core/test_tools/test_Streamline.py b/plotly/tests/test_core/test_tools/test_Streamline.py new file mode 100644 index 0000000000..948344baeb --- /dev/null +++ b/plotly/tests/test_core/test_tools/test_Streamline.py @@ -0,0 +1,4 @@ +import plotly +from plotly.graph_objs import * +import plotly.tools as tls +from nose.tools import raises From e77ca807e3ae938bd527cd3513894660ac73fb05 Mon Sep 17 00:00:00 2001 From: Chelsea Date: 2015年5月28日 16:13:14 -0400 Subject: [PATCH 04/34] Quiver and Streamline edits Quiver: can plot 1 arrow --- plotly/tools.py | 191 +++++++++++++++++++++++++++++++----------------- 1 file changed, 122 insertions(+), 69 deletions(-) diff --git a/plotly/tools.py b/plotly/tools.py index 0d6e41fa92..c03166b715 100644 --- a/plotly/tools.py +++ b/plotly/tools.py @@ -20,6 +20,7 @@ from plotly import session from plotly.graph_objs import graph_objs +from plotly.graph_objs import Scatter, Data, Marker # Warning format def warning_on_one_line(message, category, filename, lineno, file=None, line=None): @@ -39,6 +40,12 @@ def warning_on_one_line(message, category, filename, lineno, file=None, line=Non except ImportError: _ipython_imported = False +try: + import numpy as np + _numpy_imported = True +except ImportError: + _numpy_imported = False + PLOTLY_DIR = os.path.join(os.path.expanduser("~"), ".plotly") CREDENTIALS_FILE = os.path.join(PLOTLY_DIR, ".credentials") CONFIG_FILE = os.path.join(PLOTLY_DIR, ".config") @@ -1272,36 +1279,48 @@ def return_figure_from_figure_or_data(figure_or_data, validate_figure): def Quiver(x, y, u, v, - angle=np.pi/9, scale=.1, - arrow_length=.3, color='rgb(114, 132, 314)', - barb_color=color, arrow_color=color, - width=1, arrow_width=width, - barb_width=width, **kwargs): - """Return a data object for a quiver plot where x and y are the coordinates - of the arrow locations and u and v give the x and y components of the arrow - vectors. + scale=.1, angle=np.pi/9, + arrow_scale=.3, color='rgb(114, 132, 314)', + barb_color='rgb(114, 132, 314)', arrow_color='rgb(114, 132, 314)', + width=1, arrow_width=1, + barb_width=1, **kwargs): + """Return a data object for a quiver plot. + x, y, u, and v can be np.ndarrays of equal dimmensions, + or lists where x & y are the same length and u & v are the same length. + x and y are the coordinates of the arrow locations. + u and v give the x and y components of the arrow vectors. Example 1: + ``` + # Just 1 Arrow from (0,0) to (1,1) + data = Quiver(x=[0], y=[0], u=[1], v=[1], scale=1) + ``` + + Example 2: + ``` x,y = np.meshgrid(np.arange(0,np.pi,.2),np.arange(0,np.pi,.2) ) u = np.cos(x) v = np.sin(y) - data = Quiver(x,y,u,v) + data = Quiver(x, y, u, v) + ``` - Example 2: + Example 3: + ``` x,y = np.meshgrid(np.arange(0,2,.2),np.arange(0,2,.2)) u = np.cos(x)*y; v = np.sin(x)*y; data = Quiver(x, y, u, v) + ``` Keywords arguments with constant defaults: + scale (kwarg, float in [0,1], default = .1): + Scales size of the arrows (ideally to avoid overlap). + angle (kwarg, angle in radians, default = np.pi/9): Angle of arrowhead. - scale (kwarg, float in [0,1], default = .1): - Scales size of the arrows (avoid overlap). - - arrow_length (kwarg, float in [0,1], default = 0.3): + arrow_scale (kwarg, float in [0,1], default = 0.3): Value multiplied to length of barb to get length of arrowhead. color (kwarg, color string, default = rgb(114, 132, 304)): @@ -1323,24 +1342,43 @@ def Quiver(x, y, u, v, Change width of lines for arrows. """ - # angle = kwargs.pop('angle', np.pi/9) - # scale = kwargs.pop('scale', .1) - # arrow_length = kwargs.pop('arrow_length', .3) - # color = kwargs.pop('color', 'rgb(114, 132, 314)') - # barb_col = kwargs.pop('barb_color', color) - # arrow_col = kwargs.pop('arrow_color', color) - # width = kwargs.pop('width', 1) - # arrow_width = kwargs.pop('arrow_width', width) - # barb_width = kwargs.pop('barb_width', width) - - # Make array of x start values - Xstart = np.array(x) - Xstart = Xstart.flatten('F') - - # Make array of x end values - Xend = x + u * scale - Xend = np.array(Xend) + if _numpy_imported is False: + raise Exception("To use Quiver() please import numpy as np") + + if len(x) != len(y): + raise Exception("x, y, u, and v should be ndarrays of equal dimmension" + "or lists where x and y are the same length" + "and u and v are the same length") + + if len(u) != len(v): + raise Exception("x, y, u, and v should be ndarrays of equal dimmension" + "or lists where x and y are the same length" + "and u and v are the same length") + + VALID_KWARGS = ['angle', 'scale', + 'arrow_scale', 'color', + 'barb_color', 'arrow_color', + 'width', 'barb_width', + 'arrow_width'] + for key in kwargs.keys(): + if key not in VALID_KWARGS: + raise Exception("Invalid keyword argument: '{0}'".format(key)) + + # Make x y u v into np.array in case user entered list + x = np.array(x) + y = np.array(y) + u = np.array(u) + v = np.array(v) + + # Make arrays of x & y start values + Xstart = x.flatten('F') + Ystart = y.flatten('F') + + # Make arrays of x & y end values + Xend = x + (u * scale) Xend = Xend.flatten('F') + Yend = y + (v * scale) + Yend = Yend.flatten('F') # Make array of nans # These are used as spaces so the separate @@ -1354,15 +1392,6 @@ def Quiver(x, y, u, v, Xvals = np.array(Xvals) Xvals = Xvals.flatten('F') - # Make array of y start values - Ystart = np.array(Y) - Ystart = Ystart.flatten('F') - - # Make array of y end values - Yend = y + v * scale - Yend = np.array(Yend) - Yend = Yend.flatten('F') - # Combine arrays into matrix Yvals = np.matrix([Ystart, Yend, nnn]) # Make matrix into array readable by plotly @@ -1374,13 +1403,13 @@ def Quiver(x, y, u, v, mode='lines', name='Barb', line=Line(width=barb_width, color=barb_color)) - # ARROWS! + # Arrows # Get line lengths # Default arrow length = 30% of line length Xdif = Xend - Xstart Ydif = Yend - Ystart LineLen = np.sqrt(np.square(Xdif) + np.square(Ydif)) - ArrowLen = LineLen * arrow_length + ArrowLen = LineLen * arrow_scale # Get angle of line LineAng = np.arctan(Ydif / Xdif) @@ -1439,15 +1468,17 @@ def Quiver(x, y, u, v, def Streamline(x, y, u, v, density=1, angle=np.pi/9, - arrow_length=.08, color='rgb(114, 132, 304)', - stream_color=color, arrow_color=color, - width=1, arrow_width=width, - stream_width=width, **kwargs): - '''Return a data object to plot streamlines of a vector flow. + arrow_scale=.08, color='rgb(114, 132, 304)', + stream_color='rgb(114, 132, 304)', + arrow_color='rgb(114, 132, 304)', + width=1, arrow_width=1, + stream_width=1, **kwargs): + """Return a data object to plot streamlines of a vector flow. x and y are 1d arrays that define an EVENLY spaced grid. u and v are 2d arrays (shape [y,x]) giving velocities. Example 1: + ``` # Add data x = np.linspace(-3, 3, 100) y = np.linspace(-3, 3, 100) @@ -1459,9 +1490,12 @@ def Streamline(x, y, u, v, # Streamline function data = Streamline(x, y, u, v, arrow_length = .1) + ``` Example 2: # from http://nbviewer.ipython.org/github/barbagroup/AeroPython + ``` + import plotly.plotly as py # Add data N = 50 @@ -1491,16 +1525,17 @@ def Streamline(x, y, u, v, # Plot url = py.plot(data, filename='Streamline_Source') + ``` - Keywords arguments with constant defaults: + Keywords arguments: density (kwarg, integer, default=1): - density controls the closeness of the streamlines. + density controls the closeness (density) of the streamlines. angle (kwarg, angle in radians, default = np.pi/9): Angle of arrowhead. - arrow_length (kwarg, float in [0,1], default = 0.08): + arrow_scale (kwarg, float in [0,1], default = 0.08): Value multiplied to length of barb to get length of arrowhead. color (kwarg, color string, default = rgb(114, 132, 304)): @@ -1520,17 +1555,35 @@ def Streamline(x, y, u, v, arrow_width (kwarg, int greater than or equal to 1, default = width): Change width of lines for arrows. - ''' - - # density = kwargs.pop('density', 1) - # angle = kwargs.pop('angle', np.pi/9) - # arrow_len = kwargs.pop('arrow_length', .08) - # color = kwargs.pop('color', 'rgb(114, 132, 304)') - # stream_col = kwargs.pop('stream_color', color) - # arrow_col = kwargs.pop('arrow_color', color) - # width = kwargs.pop('width', 1) - # arrow_width = kwargs.pop('arrow_width', width) - # stream_width = kwargs.pop('stream_width', width) + """ + + # Throw exception if numpy is not imported + if _numpy_imported is False: + raise Exception("To use Streamline() please import numpy as np") + + # Throw exception if x is not an evenly spaced array + for index in range(len(x)-1): + if (x[index + 1]-x[index])-(x[1]-x[0])> .0001: + raise Exception("x must be a 1 dimmensional evenly spaced array") + + # Throw exception if y is not an evenly spaced array + for index in range(len(y)-1): + if (y[index + 1]-y[index])-(y[1]-y[0])> .0001: + raise Exception("y must be a 1 dimmensional evenly spaced array") + + # Throw exception if u and v are not the same dimmensions + if u.shape != v.shape: + raise Exception("u and v should have the same dimmensions") + + # Throw exception for invalid kwarg + VALID_KWARGS = ['density', 'angle', + 'arrow_scale', 'color', + 'barb_color', 'arrow_color', + 'width', 'barb_width', + 'arrow_width'] + for key in kwargs.keys(): + if key not in VALID_KWARGS: + raise Exception("Invalid keyword argument: '{0}'".format(key)) # Set up some constants - size of the grid used. NGX = len(x) @@ -1715,10 +1768,10 @@ def traj(xb, yb): # Find slopes between point a (mid point + 2) and # point b (midpoint -2) for index in range(len(xs)): - XMid[index] = xs[index][(len(xs[index])/2)+2] - XStart[index] = xs[index][(len(xs[index])/2)-2] - YMid[index] = ys[index][(len(ys[index])/2)+2] - YStart[index] = ys[index][(len(ys[index])/2)-2] + XMid[index] = xs[index][(len(xs[index])/2)+1] + XStart[index] = xs[index][(len(xs[index])/2)-1] + YMid[index] = ys[index][(len(ys[index])/2)+1] + YStart[index] = ys[index][(len(ys[index])/2)-1] Xdif = XMid - XStart Ydif = YMid - YStart @@ -1731,11 +1784,11 @@ def traj(xb, yb): Ang1 = LineAng + (angle) Ang2 = LineAng - (angle) - XSeg1 = np.cos(Ang1)*arrow_length - YSeg1 = np.sin(Ang1)*arrow_length + XSeg1 = np.cos(Ang1)*arrow_scale + YSeg1 = np.sin(Ang1)*arrow_scale - XSeg2 = np.cos(Ang2)*arrow_length - YSeg2 = np.sin(Ang2)*arrow_length + XSeg2 = np.cos(Ang2)*arrow_scale + YSeg2 = np.sin(Ang2)*arrow_scale XPoint1 = np.empty((len(Xdif))) YPoint1 = np.empty((len(Ydif))) From bfbc83e7cc0c1c46c2bb6d1a3eb901d870ef742a Mon Sep 17 00:00:00 2001 From: Chelsea Date: 2015年5月28日 16:13:57 -0400 Subject: [PATCH 05/34] Quiver Test --- .../tests/test_core/test_tools/test_Quiver.py | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/plotly/tests/test_core/test_tools/test_Quiver.py b/plotly/tests/test_core/test_tools/test_Quiver.py index 948344baeb..2d9aa7ebf0 100644 --- a/plotly/tests/test_core/test_tools/test_Quiver.py +++ b/plotly/tests/test_core/test_tools/test_Quiver.py @@ -2,3 +2,38 @@ from plotly.graph_objs import * import plotly.tools as tls from nose.tools import raises +import numpy as np + + +@raises(Exception) +def unequal_xy_length(): + data = tls.Quiver(x=[1, 2], y=[1], u=[1, 2], v=[1, 2]) + + +@raises(Exception) +def unequal_uv_length(): + data = tls.Quiver(x=[1, 2], y=[1, 3], u=[1], v=[1, 2]) + + +@raises(Exception) +def test_wrong_kwarg(): + data = tls.Quiver(stuff='not gonna work') + + +def test_one_arrow(): + trace1 = Scatter( + x=np.array([0., 1., np.nan]), + y=np.array([0., 1., np.nan]), + mode='lines', + name='Barb', + line=Line(color='rgb(114, 132, 314)', width=1) + ) + trace2 = Scatter( + x=np.array([0.82069826, 1, 0.61548617, np.nan]), + y=np.array([0.61548617, 1, 0.82069826, np.nan]), + mode='lines', + name='Arrow', + line=Line(color='rgb(114, 132, 314)', width=1) + ) + expected = Data([trace1, trace2]) + assert tls.Quiver(x=[0], y=[0], u=[1], v=[1], scale=1)) == expected From aef86ba1de660148039cbdf0d934683681f48b7d Mon Sep 17 00:00:00 2001 From: Chelsea Date: 2015年5月28日 16:27:51 -0400 Subject: [PATCH 06/34] Quiver Test --- plotly/tests/test_core/test_tools/test_Quiver.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plotly/tests/test_core/test_tools/test_Quiver.py b/plotly/tests/test_core/test_tools/test_Quiver.py index 2d9aa7ebf0..12aef9a0e1 100644 --- a/plotly/tests/test_core/test_tools/test_Quiver.py +++ b/plotly/tests/test_core/test_tools/test_Quiver.py @@ -36,4 +36,4 @@ def test_one_arrow(): line=Line(color='rgb(114, 132, 314)', width=1) ) expected = Data([trace1, trace2]) - assert tls.Quiver(x=[0], y=[0], u=[1], v=[1], scale=1)) == expected + assert tls.Quiver(x=[0], y=[0], u=[1], v=[1], scale=1) == expected From c28e1a32008469cab4be6f5d6517cd72c97e86bf Mon Sep 17 00:00:00 2001 From: Chelsea Date: 2015年5月28日 16:30:24 -0400 Subject: [PATCH 07/34] Quiver Test --- plotly/tests/test_core/test_tools/test_Quiver.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/plotly/tests/test_core/test_tools/test_Quiver.py b/plotly/tests/test_core/test_tools/test_Quiver.py index 12aef9a0e1..5a7c9fdc91 100644 --- a/plotly/tests/test_core/test_tools/test_Quiver.py +++ b/plotly/tests/test_core/test_tools/test_Quiver.py @@ -37,3 +37,8 @@ def test_one_arrow(): ) expected = Data([trace1, trace2]) assert tls.Quiver(x=[0], y=[0], u=[1], v=[1], scale=1) == expected + + +def test_two_arrow(): + expected = 1 + assert 1 == expected From 0af88c5298a97b4b2d2eacdca07ba3316777ea9c Mon Sep 17 00:00:00 2001 From: Chelsea Date: 2015年5月28日 16:30:55 -0400 Subject: [PATCH 08/34] Quiv test --- plotly/tests/test_core/test_tools/test_Quiver.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plotly/tests/test_core/test_tools/test_Quiver.py b/plotly/tests/test_core/test_tools/test_Quiver.py index 5a7c9fdc91..30c45bbe3b 100644 --- a/plotly/tests/test_core/test_tools/test_Quiver.py +++ b/plotly/tests/test_core/test_tools/test_Quiver.py @@ -41,4 +41,4 @@ def test_one_arrow(): def test_two_arrow(): expected = 1 - assert 1 == expected + assert 2 == expected From 95442656dff8d3295339059899b4861a5984d951 Mon Sep 17 00:00:00 2001 From: Chelsea Date: 2015年5月28日 16:36:55 -0400 Subject: [PATCH 09/34] Quiver Test --- plotly/tests/test_core/test_tools/test_Quiver.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/plotly/tests/test_core/test_tools/test_Quiver.py b/plotly/tests/test_core/test_tools/test_Quiver.py index 30c45bbe3b..12aef9a0e1 100644 --- a/plotly/tests/test_core/test_tools/test_Quiver.py +++ b/plotly/tests/test_core/test_tools/test_Quiver.py @@ -37,8 +37,3 @@ def test_one_arrow(): ) expected = Data([trace1, trace2]) assert tls.Quiver(x=[0], y=[0], u=[1], v=[1], scale=1) == expected - - -def test_two_arrow(): - expected = 1 - assert 2 == expected From f12ac2d3b0dc44dcd0505ebf980ad99d3ca28522 Mon Sep 17 00:00:00 2001 From: Chelsea Date: 2015年5月28日 16:49:06 -0400 Subject: [PATCH 10/34] streamline test --- plotly/tests/test_core/test_tools/test_Streamline.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/plotly/tests/test_core/test_tools/test_Streamline.py b/plotly/tests/test_core/test_tools/test_Streamline.py index 948344baeb..2387b54b19 100644 --- a/plotly/tests/test_core/test_tools/test_Streamline.py +++ b/plotly/tests/test_core/test_tools/test_Streamline.py @@ -2,3 +2,14 @@ from plotly.graph_objs import * import plotly.tools as tls from nose.tools import raises +import numpy as np + + +@raises(Exception) +def test_wrong_kwarg(): + fig = tls.make_subplots(stuff='not gonna work') + + +def test_two_arrow(): + expected = 2 + assert 1 == expected From 850a5c7bf3eb8a7b85abbb6c41198d13f37d9871 Mon Sep 17 00:00:00 2001 From: Chelsea Date: 2015年5月28日 18:33:45 -0400 Subject: [PATCH 11/34] change x,y from array to list --- plotly/tests/test_core/test_tools/test_Streamline.py | 5 ----- plotly/tools.py | 12 ++++++++---- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/plotly/tests/test_core/test_tools/test_Streamline.py b/plotly/tests/test_core/test_tools/test_Streamline.py index 2387b54b19..451db6e9b6 100644 --- a/plotly/tests/test_core/test_tools/test_Streamline.py +++ b/plotly/tests/test_core/test_tools/test_Streamline.py @@ -8,8 +8,3 @@ @raises(Exception) def test_wrong_kwarg(): fig = tls.make_subplots(stuff='not gonna work') - - -def test_two_arrow(): - expected = 2 - assert 1 == expected diff --git a/plotly/tools.py b/plotly/tools.py index c03166b715..44d10fb911 100644 --- a/plotly/tools.py +++ b/plotly/tools.py @@ -20,7 +20,7 @@ from plotly import session from plotly.graph_objs import graph_objs -from plotly.graph_objs import Scatter, Data, Marker +from plotly.graph_objs import Scatter, Data, Marker, Line # Warning format def warning_on_one_line(message, category, filename, lineno, file=None, line=None): @@ -1391,12 +1391,14 @@ def Quiver(x, y, u, v, # Make matrix into array readable by plotly Xvals = np.array(Xvals) Xvals = Xvals.flatten('F') + Xvals = Xvals.tolist() # Combine arrays into matrix Yvals = np.matrix([Ystart, Yend, nnn]) # Make matrix into array readable by plotly Yvals = np.array(Yvals) Yvals = Yvals.flatten('F') + Yvals = Yvals.tolist() # Make Trace1: the lines trace1 = Scatter(x=Xvals, y=Yvals, @@ -1448,12 +1450,14 @@ def Quiver(x, y, u, v, # Make matrix into array readable by plotly XArrows = np.array(XArrows) XArrows = XArrows.flatten('F') + XArrows = XArrows.tolist() # Combine arrays into matrix YArrows = np.matrix([YPoint1, Yend, YPoint2, nnn]) # Make matrix into array readable by plotly YArrows = np.array(YArrows) YArrows = YArrows.flatten('F') + YArrows = YArrows.tolist() # Make trace2: the arrows trace2 = Scatter(x=XArrows, y=YArrows, @@ -1573,7 +1577,8 @@ def Streamline(x, y, u, v, # Throw exception if u and v are not the same dimmensions if u.shape != v.shape: - raise Exception("u and v should have the same dimmensions") + raise Exception("u and v should have the same dimmensions" + "try using np.ndarrays with the same dimmensions") # Throw exception for invalid kwarg VALID_KWARGS = ['density', 'angle', @@ -1648,8 +1653,7 @@ def g(xi, yi): vi = value_at(v, xi, yi) return -ui*dt_ds, -vi*dt_ds - check = lambda xi, yi: xi>= 0 and xi < NGX-1 - and yi>= 0 and yi < NGY-1 + check = lambda xi, yi: xi>= 0 and xi < NGX-1 and yi>= 0 and yi < NGY-1 bx_changes = [] by_changes = [] From dc8092b7abe07718e9dd21753cd42adf3636aef5 Mon Sep 17 00:00:00 2001 From: Chelsea Date: 2015年5月28日 18:36:48 -0400 Subject: [PATCH 12/34] test one arrow assertion --- plotly/tests/test_core/test_tools/test_Quiver.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/plotly/tests/test_core/test_tools/test_Quiver.py b/plotly/tests/test_core/test_tools/test_Quiver.py index 12aef9a0e1..741ebde92b 100644 --- a/plotly/tests/test_core/test_tools/test_Quiver.py +++ b/plotly/tests/test_core/test_tools/test_Quiver.py @@ -21,16 +21,17 @@ def test_wrong_kwarg(): def test_one_arrow(): + nan = np.nan trace1 = Scatter( - x=np.array([0., 1., np.nan]), - y=np.array([0., 1., np.nan]), + x=np.array([0., 1., nan]), + y=np.array([0., 1., nan]), mode='lines', name='Barb', line=Line(color='rgb(114, 132, 314)', width=1) ) trace2 = Scatter( - x=np.array([0.82069826, 1, 0.61548617, np.nan]), - y=np.array([0.61548617, 1, 0.82069826, np.nan]), + x=np.array([0.82069826, 1., 0.61548617, nan]), + y=np.array([0.61548617, 1., 0.82069826, nan]), mode='lines', name='Arrow', line=Line(color='rgb(114, 132, 314)', width=1) From 2c7474733fad86b52be158438d3dfdf671f86682 Mon Sep 17 00:00:00 2001 From: Chelsea Date: 2015年5月28日 19:09:03 -0400 Subject: [PATCH 13/34] change from arrays to lists --- plotly/tests/test_core/test_tools/test_Quiver.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plotly/tests/test_core/test_tools/test_Quiver.py b/plotly/tests/test_core/test_tools/test_Quiver.py index 741ebde92b..6c0fea4e67 100644 --- a/plotly/tests/test_core/test_tools/test_Quiver.py +++ b/plotly/tests/test_core/test_tools/test_Quiver.py @@ -23,8 +23,8 @@ def test_wrong_kwarg(): def test_one_arrow(): nan = np.nan trace1 = Scatter( - x=np.array([0., 1., nan]), - y=np.array([0., 1., nan]), + x=[0., 1., nan], + y=[0., 1., nan], mode='lines', name='Barb', line=Line(color='rgb(114, 132, 314)', width=1) From da055a4183e74f5e952ca9d772bb90dc570ca01c Mon Sep 17 00:00:00 2001 From: Chelsea Date: 2015年5月28日 19:24:19 -0400 Subject: [PATCH 14/34] fix assertion error --- plotly/tests/test_core/test_tools/test_Quiver.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/plotly/tests/test_core/test_tools/test_Quiver.py b/plotly/tests/test_core/test_tools/test_Quiver.py index 6c0fea4e67..f310489859 100644 --- a/plotly/tests/test_core/test_tools/test_Quiver.py +++ b/plotly/tests/test_core/test_tools/test_Quiver.py @@ -37,4 +37,10 @@ def test_one_arrow(): line=Line(color='rgb(114, 132, 314)', width=1) ) expected = Data([trace1, trace2]) - assert tls.Quiver(x=[0], y=[0], u=[1], v=[1], scale=1) == expected + + data = tls.Quiver(x=[0], y=[0], u=[1], v=[1], scale=1) + + np.testing.assert_almost_equal(expected[0]['y'], data[0]['y']) + np.testing.assert_almost_equal(expected[0]['x'], data[0]['x']) + np.testing.assert_almost_equal(expected[1]['y'], data[1]['y']) + np.testing.assert_almost_equal(expected[0]['x'], data[0]['x']) From fff3a1ff84e020461e37d29b6ec8c549f2b6d862 Mon Sep 17 00:00:00 2001 From: Chelsea Date: 2015年5月28日 20:02:51 -0400 Subject: [PATCH 15/34] update to quiver tests --- .../tests/test_core/test_tools/test_Quiver.py | 92 +++++++++++++++++-- plotly/tools.py | 35 ++----- 2 files changed, 95 insertions(+), 32 deletions(-) diff --git a/plotly/tests/test_core/test_tools/test_Quiver.py b/plotly/tests/test_core/test_tools/test_Quiver.py index f310489859..bcf5725ac6 100644 --- a/plotly/tests/test_core/test_tools/test_Quiver.py +++ b/plotly/tests/test_core/test_tools/test_Quiver.py @@ -30,8 +30,8 @@ def test_one_arrow(): line=Line(color='rgb(114, 132, 314)', width=1) ) trace2 = Scatter( - x=np.array([0.82069826, 1., 0.61548617, nan]), - y=np.array([0.61548617, 1., 0.82069826, nan]), + x=[0.82069826, 1., 0.61548617, nan], + y=[0.61548617, 1., 0.82069826, nan], mode='lines', name='Arrow', line=Line(color='rgb(114, 132, 314)', width=1) @@ -40,7 +40,87 @@ def test_one_arrow(): data = tls.Quiver(x=[0], y=[0], u=[1], v=[1], scale=1) - np.testing.assert_almost_equal(expected[0]['y'], data[0]['y']) - np.testing.assert_almost_equal(expected[0]['x'], data[0]['x']) - np.testing.assert_almost_equal(expected[1]['y'], data[1]['y']) - np.testing.assert_almost_equal(expected[0]['x'], data[0]['x']) + np.testing.assert_almost_equal(data[0]['y'], expected[0]['y']) + np.testing.assert_almost_equal(data[0]['x'], expected[0]['x']) + np.testing.assert_almost_equal(data[1]['y'], expected[1]['y']) + np.testing.assert_almost_equal(data[1]['y'], expected[0]['x']) + assert data[0].keys() == expected[0].keys() + + +def test_complicated(): + nan = np.nan + trace1 = Scatter( + x=[0.0, 0.1, nan, 0.0, 0.1, nan, 0.0, 0.1, nan, 0.0, 0.1, nan, + 0.7853981633974483, 0.856108841516103, nan, 0.7853981633974483, + 0.856108841516103, nan, 0.7853981633974483, 0.856108841516103, + nan, 0.7853981633974483, 0.856108841516103, nan, + 1.5707963267948966, 1.5707963267948966, nan, 1.5707963267948966, + 1.5707963267948966, nan, 1.5707963267948966, 1.5707963267948966, + nan, 1.5707963267948966, 1.5707963267948966, nan, + 2.356194490192345, 2.2854838120736902, nan, 2.356194490192345, + 2.2854838120736902, nan, 2.356194490192345, 2.2854838120736902, + nan, 2.356194490192345, 2.2854838120736902, nan], + y=[0.0, 0.0, nan, 0.7853981633974483, 0.856108841516103, nan, + 1.5707963267948966, 1.6707963267948966, nan, 2.356194490192345, + 2.4269051683109994, nan, 0.0, 0.0, nan, 0.7853981633974483, + 0.856108841516103, nan, 1.5707963267948966, 1.6707963267948966, + nan, 2.356194490192345, 2.4269051683109994, nan, 0.0, 0.0, nan, + 0.7853981633974483, 0.856108841516103, nan, 1.5707963267948966, + 1.6707963267948966, nan, 2.356194490192345, 2.4269051683109994, + nan, 0.0, 0.0, nan, 0.7853981633974483, 0.856108841516103, nan, + 1.5707963267948966, 1.6707963267948966, nan, 2.356194490192345, + 2.4269051683109994, nan], + mode='lines', + name='Barb', + line=Line(color='rgb(114, 132, 314)', barb_width=2) + ) + trace2 = Scatter( + x=[0.07690301168721783, 0.1, 0.07690301168721783, nan, + 0.0836679629390453, 0.1, 0.07013806043539038, nan, + 0.08647009749634509, 0.1, 0.06733592587809059, nan, + 0.08366796293904528, 0.1, 0.07013806043539039, nan, + 0.8397768044551484, 0.856108841516103, 0.8397768044551484, nan, + 0.8465417557069758, 0.856108841516103, 0.8330118532033208, nan, + 0.8493438902642756, 0.856108841516103, 0.830209718646021, nan, + 0.8465417557069758, 0.856108841516103, 0.8330118532033208, nan, + nan, 1.5707963267948966, nan, nan, 1.577561278046724, + 1.5707963267948966, 1.564031375543069, nan, 1.5803634126040238, + 1.5707963267948966, 1.5612292409857693, nan, 1.577561278046724, + 1.5707963267948966, 1.564031375543069, nan, 2.3018158491346448, + 2.2854838120736902, 2.3018158491346448, nan, 2.3085808003864723, + 2.2854838120736902, 2.2950508978828172, nan, 2.3113829349437722, + 2.2854838120736902, 2.2922487633255177, nan, 2.3085808003864723, + 2.2854838120736902, 2.2950508978828172, nan], + y=[-0.009567085809127246, 0.0, 0.009567085809127246, nan, + 0.8302097186460211, 0.856108841516103, 0.8493438902642756, nan, + 1.6381322526729871, 1.6707963267948966, 1.6572664242912416, nan, + .4010060454409174, 2.4269051683109994, 2.420140217059172, nan, + -0.00676495125182746, 0.0, 0.00676495125182746, nan, + 0.8330118532033208, 0.856108841516103, 0.8465417557069758, nan, + 1.640934387230287, 1.6707963267948966, 1.6544642897339419, nan, + 2.4038081799982174, 2.4269051683109994, 2.4173380825018724, nan, + nan, 0.0, nan, nan, 0.8397768044551484, 0.856108841516103, + 0.8397768044551484, nan, 1.6476993384821144, 1.6707963267948966, + 1.6476993384821144, nan, 2.410573131250045, 2.4269051683109994, + 2.410573131250045, nan, 0.0067649512518274495, 0.0, + -0.0067649512518274495, nan, 0.8465417557069758, + 0.856108841516103, 0.8330118532033208, nan, 1.6544642897339419, + 1.6707963267948966, 1.640934387230287, nan, 2.4173380825018724, + 2.4269051683109994, 2.4038081799982174, nan], + mode='lines', + name='Arrow', + line=Line(color='rgb(114, 132, 314)', width=1) + ) + expected = Data([trace1, trace2]) + + x, y = np.meshgrid(np.arange(0, np.pi, .5), np.arange(0, np.pi, .5)) + u = np.cos(x) + v = np.sin(y) + data = tls.Quiver(x, y, u, v, scale=.1, angle=np.pi/8, + arrow_scale=.25, barb_width=2) + + np.testing.assert_almost_equal(data[0]['y'], expected[0]['y']) + np.testing.assert_almost_equal(data[0]['x'], expected[0]['x']) + np.testing.assert_almost_equal(data[1]['y'], expected[1]['y']) + np.testing.assert_almost_equal(data[1]['y'], expected[0]['x']) + assert data[0].keys() == expected[0].keys() diff --git a/plotly/tools.py b/plotly/tools.py index 44d10fb911..a1c57b8008 100644 --- a/plotly/tools.py +++ b/plotly/tools.py @@ -1280,9 +1280,8 @@ def return_figure_from_figure_or_data(figure_or_data, validate_figure): def Quiver(x, y, u, v, scale=.1, angle=np.pi/9, - arrow_scale=.3, color='rgb(114, 132, 314)', - barb_color='rgb(114, 132, 314)', arrow_color='rgb(114, 132, 314)', - width=1, arrow_width=1, + arrow_scale=.3, barb_color='rgb(114, 132, 314)', + arrow_color='rgb(114, 132, 314)', arrow_width=1, barb_width=1, **kwargs): """Return a data object for a quiver plot. x, y, u, and v can be np.ndarrays of equal dimmensions, @@ -1323,18 +1322,12 @@ def Quiver(x, y, u, v, arrow_scale (kwarg, float in [0,1], default = 0.3): Value multiplied to length of barb to get length of arrowhead. - color (kwarg, color string, default = rgb(114, 132, 304)): - Set color of both barb and arrow. - barb_color (kwarg, color string, default = color): Set color of barbs. arrow_color (kwarg, color string, default = color): Set color of arrow. - width (kwarg, int greater than or equal to 1, default = 1): - Set width of lines for barbs and arrows. - barb_width (kwarg, int greater than or equal to 1, default = width): Change width of lines for barbs. @@ -1356,9 +1349,8 @@ def Quiver(x, y, u, v, "and u and v are the same length") VALID_KWARGS = ['angle', 'scale', - 'arrow_scale', 'color', - 'barb_color', 'arrow_color', - 'width', 'barb_width', + 'arrow_scale', 'barb_color', + 'arrow_color', 'barb_width', 'arrow_width'] for key in kwargs.keys(): if key not in VALID_KWARGS: @@ -1472,10 +1464,8 @@ def Quiver(x, y, u, v, def Streamline(x, y, u, v, density=1, angle=np.pi/9, - arrow_scale=.08, color='rgb(114, 132, 304)', - stream_color='rgb(114, 132, 304)', - arrow_color='rgb(114, 132, 304)', - width=1, arrow_width=1, + arrow_scale=.08, stream_color='rgb(114, 132, 304)', + arrow_color='rgb(114, 132, 304)', arrow_width=1, stream_width=1, **kwargs): """Return a data object to plot streamlines of a vector flow. x and y are 1d arrays that define an EVENLY spaced grid. @@ -1542,18 +1532,12 @@ def Streamline(x, y, u, v, arrow_scale (kwarg, float in [0,1], default = 0.08): Value multiplied to length of barb to get length of arrowhead. - color (kwarg, color string, default = rgb(114, 132, 304)): - Set color of both barb and arrow. - barb_color (kwarg, color string, default = color): Set color of barbs. arrow_color (kwarg, color string, default = color): Set color of arrow. - width (kwarg, int greater than or equal to 1, default = 1): - Set width of lines for barbs and arrows. - barb_width (kwarg, int greater than or equal to 1, default = width): Change width of lines for barbs. @@ -1582,10 +1566,9 @@ def Streamline(x, y, u, v, # Throw exception for invalid kwarg VALID_KWARGS = ['density', 'angle', - 'arrow_scale', 'color', - 'barb_color', 'arrow_color', - 'width', 'barb_width', - 'arrow_width'] + 'arrow_scale', 'barb_color', + 'arrow_color', 'width', + 'barb_width', 'arrow_width'] for key in kwargs.keys(): if key not in VALID_KWARGS: raise Exception("Invalid keyword argument: '{0}'".format(key)) From 3fc00eae7a5073704920da449fd2be963b8a9918 Mon Sep 17 00:00:00 2001 From: Chelsea Date: 2015年5月28日 20:08:33 -0400 Subject: [PATCH 16/34] fix quiver test --- plotly/tests/test_core/test_tools/test_Quiver.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plotly/tests/test_core/test_tools/test_Quiver.py b/plotly/tests/test_core/test_tools/test_Quiver.py index bcf5725ac6..d59beddff0 100644 --- a/plotly/tests/test_core/test_tools/test_Quiver.py +++ b/plotly/tests/test_core/test_tools/test_Quiver.py @@ -43,7 +43,7 @@ def test_one_arrow(): np.testing.assert_almost_equal(data[0]['y'], expected[0]['y']) np.testing.assert_almost_equal(data[0]['x'], expected[0]['x']) np.testing.assert_almost_equal(data[1]['y'], expected[1]['y']) - np.testing.assert_almost_equal(data[1]['y'], expected[0]['x']) + np.testing.assert_almost_equal(data[1]['x'], expected[1]['x']) assert data[0].keys() == expected[0].keys() @@ -122,5 +122,5 @@ def test_complicated(): np.testing.assert_almost_equal(data[0]['y'], expected[0]['y']) np.testing.assert_almost_equal(data[0]['x'], expected[0]['x']) np.testing.assert_almost_equal(data[1]['y'], expected[1]['y']) - np.testing.assert_almost_equal(data[1]['y'], expected[0]['x']) + np.testing.assert_almost_equal(data[1]['x'], expected[1]['x']) assert data[0].keys() == expected[0].keys() From dc55e05b69effe9da470167ca6a8e3103233ef0d Mon Sep 17 00:00:00 2001 From: Chelsea Date: 2015年5月28日 20:09:34 -0400 Subject: [PATCH 17/34] fix quiv test --- plotly/tests/test_core/test_tools/test_Quiver.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plotly/tests/test_core/test_tools/test_Quiver.py b/plotly/tests/test_core/test_tools/test_Quiver.py index d59beddff0..f046437df4 100644 --- a/plotly/tests/test_core/test_tools/test_Quiver.py +++ b/plotly/tests/test_core/test_tools/test_Quiver.py @@ -72,7 +72,7 @@ def test_complicated(): 2.4269051683109994, nan], mode='lines', name='Barb', - line=Line(color='rgb(114, 132, 314)', barb_width=2) + line=Line(color='rgb(114, 132, 314)', width=2) ) trace2 = Scatter( x=[0.07690301168721783, 0.1, 0.07690301168721783, nan, From 76ec94fb4b80ba98f238719a0d95c77675e2c34a Mon Sep 17 00:00:00 2001 From: Chelsea Date: 2015年5月28日 20:21:51 -0400 Subject: [PATCH 18/34] quiver test --- plotly/tests/test_core/test_tools/test_Quiver.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plotly/tests/test_core/test_tools/test_Quiver.py b/plotly/tests/test_core/test_tools/test_Quiver.py index f046437df4..20b2d9c336 100644 --- a/plotly/tests/test_core/test_tools/test_Quiver.py +++ b/plotly/tests/test_core/test_tools/test_Quiver.py @@ -121,6 +121,6 @@ def test_complicated(): np.testing.assert_almost_equal(data[0]['y'], expected[0]['y']) np.testing.assert_almost_equal(data[0]['x'], expected[0]['x']) - np.testing.assert_almost_equal(data[1]['y'], expected[1]['y']) - np.testing.assert_almost_equal(data[1]['x'], expected[1]['x']) + # np.testing.assert_almost_equal(data[1]['y'], expected[1]['y']) + # np.testing.assert_almost_equal(data[1]['x'], expected[1]['x']) assert data[0].keys() == expected[0].keys() From 72a3b437254745643c24cba61c9e44b0820a1ba9 Mon Sep 17 00:00:00 2001 From: Chelsea Date: 2015年5月28日 20:24:45 -0400 Subject: [PATCH 19/34] test quiv --- plotly/tests/test_core/test_tools/test_Quiver.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plotly/tests/test_core/test_tools/test_Quiver.py b/plotly/tests/test_core/test_tools/test_Quiver.py index 20b2d9c336..1ac3d95da6 100644 --- a/plotly/tests/test_core/test_tools/test_Quiver.py +++ b/plotly/tests/test_core/test_tools/test_Quiver.py @@ -119,8 +119,8 @@ def test_complicated(): data = tls.Quiver(x, y, u, v, scale=.1, angle=np.pi/8, arrow_scale=.25, barb_width=2) - np.testing.assert_almost_equal(data[0]['y'], expected[0]['y']) - np.testing.assert_almost_equal(data[0]['x'], expected[0]['x']) + # np.testing.assert_almost_equal(data[0]['y'], expected[0]['y']) + # np.testing.assert_almost_equal(data[0]['x'], expected[0]['x']) # np.testing.assert_almost_equal(data[1]['y'], expected[1]['y']) # np.testing.assert_almost_equal(data[1]['x'], expected[1]['x']) assert data[0].keys() == expected[0].keys() From f4d09738fe773d1a67ca3ad1f56441f11b6f4d69 Mon Sep 17 00:00:00 2001 From: Chelsea Date: 2015年5月28日 20:25:47 -0400 Subject: [PATCH 20/34] test quiv --- plotly/tests/test_core/test_tools/test_Quiver.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plotly/tests/test_core/test_tools/test_Quiver.py b/plotly/tests/test_core/test_tools/test_Quiver.py index 1ac3d95da6..a968f94442 100644 --- a/plotly/tests/test_core/test_tools/test_Quiver.py +++ b/plotly/tests/test_core/test_tools/test_Quiver.py @@ -121,6 +121,6 @@ def test_complicated(): # np.testing.assert_almost_equal(data[0]['y'], expected[0]['y']) # np.testing.assert_almost_equal(data[0]['x'], expected[0]['x']) - # np.testing.assert_almost_equal(data[1]['y'], expected[1]['y']) - # np.testing.assert_almost_equal(data[1]['x'], expected[1]['x']) + np.testing.assert_almost_equal(data[1]['y'], expected[1]['y']) + np.testing.assert_almost_equal(data[1]['x'], expected[1]['x']) assert data[0].keys() == expected[0].keys() From 59e9ca0910e4f778f0ef7bff43384cdc0d695e66 Mon Sep 17 00:00:00 2001 From: Chelsea Date: 2015年5月28日 20:30:54 -0400 Subject: [PATCH 21/34] test quiv --- .../tests/test_core/test_tools/test_Quiver.py | 56 ++----------------- 1 file changed, 4 insertions(+), 52 deletions(-) diff --git a/plotly/tests/test_core/test_tools/test_Quiver.py b/plotly/tests/test_core/test_tools/test_Quiver.py index a968f94442..623c403a81 100644 --- a/plotly/tests/test_core/test_tools/test_Quiver.py +++ b/plotly/tests/test_core/test_tools/test_Quiver.py @@ -50,63 +50,15 @@ def test_one_arrow(): def test_complicated(): nan = np.nan trace1 = Scatter( - x=[0.0, 0.1, nan, 0.0, 0.1, nan, 0.0, 0.1, nan, 0.0, 0.1, nan, - 0.7853981633974483, 0.856108841516103, nan, 0.7853981633974483, - 0.856108841516103, nan, 0.7853981633974483, 0.856108841516103, - nan, 0.7853981633974483, 0.856108841516103, nan, - 1.5707963267948966, 1.5707963267948966, nan, 1.5707963267948966, - 1.5707963267948966, nan, 1.5707963267948966, 1.5707963267948966, - nan, 1.5707963267948966, 1.5707963267948966, nan, - 2.356194490192345, 2.2854838120736902, nan, 2.356194490192345, - 2.2854838120736902, nan, 2.356194490192345, 2.2854838120736902, - nan, 2.356194490192345, 2.2854838120736902, nan], - y=[0.0, 0.0, nan, 0.7853981633974483, 0.856108841516103, nan, - 1.5707963267948966, 1.6707963267948966, nan, 2.356194490192345, - 2.4269051683109994, nan, 0.0, 0.0, nan, 0.7853981633974483, - 0.856108841516103, nan, 1.5707963267948966, 1.6707963267948966, - nan, 2.356194490192345, 2.4269051683109994, nan, 0.0, 0.0, nan, - 0.7853981633974483, 0.856108841516103, nan, 1.5707963267948966, - 1.6707963267948966, nan, 2.356194490192345, 2.4269051683109994, - nan, 0.0, 0.0, nan, 0.7853981633974483, 0.856108841516103, nan, - 1.5707963267948966, 1.6707963267948966, nan, 2.356194490192345, - 2.4269051683109994, nan], + x=[0.0, 0.1, nan, 0.0, 0.1, nan, 0.0, 0.1, nan, 0.0, 0.1, nan, 0.0, 0.1, nan, 0.0, 0.1, nan, 0.0, 0.1, nan, 0.5, 0.5877582561890373, nan, 0.5, 0.5877582561890373, nan, 0.5, 0.5877582561890373, nan, 0.5, 0.5877582561890373, nan, 0.5, 0.5877582561890373, nan, 0.5, 0.5877582561890373, nan, 0.5, 0.5877582561890373, nan, 1.0, 1.0540302305868139, nan, 1.0, 1.0540302305868139, nan, 1.0, 1.0540302305868139, nan, 1.0, 1.0540302305868139, nan, 1.0, 1.0540302305868139, nan, 1.0, 1.0540302305868139, nan, 1.0, 1.0540302305868139, nan, 1.5, 1.5070737201667703, nan, 1.5, 1.5070737201667703, nan, 1.5, 1.5070737201667703, nan, 1.5, 1.5070737201667703, nan, 1.5, 1.5070737201667703, nan, 1.5, 1.5070737201667703, nan, 1.5, 1.5070737201667703, nan, 2.0, 1.9583853163452858, nan, 2.0, 1.9583853163452858, nan, 2.0, 1.9583853163452858, nan, 2.0, 1.9583853163452858, nan, 2.0, 1.9583853163452858, nan, 2.0, 1.9583853163452858, nan, 2.0, 1.9583853163452858, nan, 2.5, 2.4198856384453067, nan, 2.5, 2.4198856384453067, nan, 2.5, 2.4198856384453067, nan, 2.5, 2.4198856384453067, nan, 2.5, 2.4198856384453067, nan, 2.5, 2.4198856384453067, nan, 2.5, 2.4198856384453067, nan, 3.0, 2.9010007503399553, nan, 3.0, 2.9010007503399553, nan, 3.0, 2.9010007503399553, nan, 3.0, 2.9010007503399553, nan, 3.0, 2.9010007503399553, nan, 3.0, 2.9010007503399553, nan, 3.0, 2.9010007503399553, nan], + y=[0.0, 0.0, nan, 0.5, 0.5479425538604203, nan, 1.0, 1.0841470984807897, nan, 1.5, 1.5997494986604055, nan, 2.0, 2.0909297426825684, nan, 2.5, 2.5598472144103956, nan, 3.0, 3.0141120008059867, nan, 0.0, 0.0, nan, 0.5, 0.5479425538604203, nan, 1.0, 1.0841470984807897, nan, 1.5, 1.5997494986604055, nan, 2.0, 2.0909297426825684, nan, 2.5, 2.5598472144103956, nan, 3.0, 3.0141120008059867, nan, 0.0, 0.0, nan, 0.5, 0.5479425538604203, nan, 1.0, 1.0841470984807897, nan, 1.5, 1.5997494986604055, nan, 2.0, 2.0909297426825684, nan, 2.5, 2.5598472144103956, nan, 3.0, 3.0141120008059867, nan, 0.0, 0.0, nan, 0.5, 0.5479425538604203, nan, 1.0, 1.0841470984807897, nan, 1.5, 1.5997494986604055, nan, 2.0, 2.0909297426825684, nan, 2.5, 2.5598472144103956, nan, 3.0, 3.0141120008059867, nan, 0.0, 0.0, nan, 0.5, 0.5479425538604203, nan, 1.0, 1.0841470984807897, nan, 1.5, 1.5997494986604055, nan, 2.0, 2.0909297426825684, nan, 2.5, 2.5598472144103956, nan, 3.0, 3.0141120008059867, nan, 0.0, 0.0, nan, 0.5, 0.5479425538604203, nan, 1.0, 1.0841470984807897, nan, 1.5, 1.5997494986604055, nan, 2.0, 2.0909297426825684, nan, 2.5, 2.5598472144103956, nan, 3.0, 3.0141120008059867, nan, 0.0, 0.0, nan, 0.5, 0.5479425538604203, nan, 1.0, 1.0841470984807897, nan, 1.5, 1.5997494986604055, nan, 2.0, 2.0909297426825684, nan, 2.5, 2.5598472144103956, nan, 3.0, 3.0141120008059867, nan], mode='lines', name='Barb', line=Line(color='rgb(114, 132, 314)', width=2) ) trace2 = Scatter( - x=[0.07690301168721783, 0.1, 0.07690301168721783, nan, - 0.0836679629390453, 0.1, 0.07013806043539038, nan, - 0.08647009749634509, 0.1, 0.06733592587809059, nan, - 0.08366796293904528, 0.1, 0.07013806043539039, nan, - 0.8397768044551484, 0.856108841516103, 0.8397768044551484, nan, - 0.8465417557069758, 0.856108841516103, 0.8330118532033208, nan, - 0.8493438902642756, 0.856108841516103, 0.830209718646021, nan, - 0.8465417557069758, 0.856108841516103, 0.8330118532033208, nan, - nan, 1.5707963267948966, nan, nan, 1.577561278046724, - 1.5707963267948966, 1.564031375543069, nan, 1.5803634126040238, - 1.5707963267948966, 1.5612292409857693, nan, 1.577561278046724, - 1.5707963267948966, 1.564031375543069, nan, 2.3018158491346448, - 2.2854838120736902, 2.3018158491346448, nan, 2.3085808003864723, - 2.2854838120736902, 2.2950508978828172, nan, 2.3113829349437722, - 2.2854838120736902, 2.2922487633255177, nan, 2.3085808003864723, - 2.2854838120736902, 2.2950508978828172, nan], - y=[-0.009567085809127246, 0.0, 0.009567085809127246, nan, - 0.8302097186460211, 0.856108841516103, 0.8493438902642756, nan, - 1.6381322526729871, 1.6707963267948966, 1.6572664242912416, nan, - .4010060454409174, 2.4269051683109994, 2.420140217059172, nan, - -0.00676495125182746, 0.0, 0.00676495125182746, nan, - 0.8330118532033208, 0.856108841516103, 0.8465417557069758, nan, - 1.640934387230287, 1.6707963267948966, 1.6544642897339419, nan, - 2.4038081799982174, 2.4269051683109994, 2.4173380825018724, nan, - nan, 0.0, nan, nan, 0.8397768044551484, 0.856108841516103, - 0.8397768044551484, nan, 1.6476993384821144, 1.6707963267948966, - 1.6476993384821144, nan, 2.410573131250045, 2.4269051683109994, - 2.410573131250045, nan, 0.0067649512518274495, 0.0, - -0.0067649512518274495, nan, 0.8465417557069758, - 0.856108841516103, 0.8330118532033208, nan, 1.6544642897339419, - 1.6707963267948966, 1.640934387230287, nan, 2.4173380825018724, - 2.4269051683109994, 2.4038081799982174, nan], + x=[0.07690301168721783, 0.1, 0.07690301168721783, nan, 0.08148971695413129, 0.1, 0.07231630642030437, nan, 0.0849534368047658, 0.1, 0.06885258656966987, nan, 0.08644613181823306, 0.1, 0.06735989155620262, nan, 0.08560233819567775, 0.1, 0.06820368517875791, nan, 0.08262864604423276, 0.1, 0.0711773773302029, nan, 0.07825311891371131, 0.1, 0.07555290446072435, nan, 0.5674887420135539, 0.5877582561890373, 0.5674887420135539, nan, 0.5720754472804674, 0.5877582561890373, 0.5629020367466405, nan, 0.5755391671311019, 0.5877582561890373, 0.559438316896006, nan, 0.5770318621445691, 0.5877582561890373, 0.5579456218825387, nan, 0.5761880685220139, 0.5877582561890373, 0.558789415505094, nan, 0.5732143763705688, 0.5877582561890373, 0.561763107656539, nan, 0.5688388492400475, 0.5877582561890373, 0.5661386347870605, nan, 1.0415508745428081, 1.0540302305868139, 1.0415508745428081, nan, 1.0461375798097217, 1.0540302305868139, 1.0369641692758949, nan, 1.049601299660356, 1.0540302305868139, 1.0335004494252602, nan, 1.0510939946738234, 1.0540302305868139, 1.032007754411793, nan, 1.0502502010512682, 1.0540302305868139, 1.0328515480343483, nan, 1.047276508899823, 1.0540302305868139, 1.0358252401857933, nan, 1.0429009817693018, 1.0540302305868139, 1.0402007673163147, nan, 1.5054399038465724, 1.5070737201667703, 1.5054399038465724, nan, 1.510026609113486, 1.5070737201667703, 1.500853198579659, nan, 1.5134903289641204, 1.5070737201667703, 1.4973894787290245, nan, 1.5149830239775877, 1.5070737201667703, 1.4958967837155572, nan, 1.5141392303550323, 1.5070737201667703, 1.4967405773381126, nan, 1.5111655382035873, 1.5070737201667703, 1.4997142694895575, nan, 1.5067900110730659, 1.5070737201667703, 1.504089796620079, nan, 1.9679970549654164, 1.9583853163452858, 1.9679970549654164, nan, 1.97258376023233, 1.9583853163452858, 1.963410349698503, nan, 1.9760474800829644, 1.9583853163452858, 1.9599466298478685, nan, 1.9775401750964317, 1.9583853163452858, 1.9584539348344012, nan, 1.9766963814738763, 1.9583853163452858, 1.9592977284569564, nan, 1.9737226893224313, 1.9583853163452858, 1.9622714206084015, nan, 1.9693471621919099, 1.9583853163452858, 1.966646947738923, nan, 2.438389643170454, 2.4198856384453067, 2.438389643170454, nan, 2.4429763484373677, 2.4198856384453067, 2.4338029379035406, nan, 2.4464400682880023, 2.4198856384453067, 2.4303392180529064, nan, 2.4479327633014694, 2.4198856384453067, 2.428846523039439, nan, 2.447088969678914, 2.4198856384453067, 2.4296903166619943, nan, 2.444115277527469, 2.4198856384453067, 2.4326640088134392, nan, 2.4397397503969476, 2.4198856384453067, 2.4370395359439607, nan, 2.9238665954636778, 2.9010007503399553, 2.9238665954636778, nan, 2.9284533007305913, 2.9010007503399553, 2.9192798901967643, nan, 2.931917020581226, 2.9010007503399553, 2.91581617034613, nan, 2.933409715594693, 2.9010007503399553, 2.9143234753326626, nan, 2.9325659219721376, 2.9010007503399553, 2.915167268955218, nan, 2.9295922298206927, 2.9010007503399553, 2.918140961106663, nan, 2.925216702690171, 2.9010007503399553, 2.9225164882371844, nan], + y=[-0.009567085809127246, 0.0, 0.009567085809127246, nan, 0.5273021819893026, 0.5479425538604203, 0.546436353607557, nan, 1.0551445671700093, 1.0841470984807897, 1.0742787387882637, nan, 1.5671432828036256, 1.5997494986604055, 1.58627745442188, nan, 2.0603606248332054, 2.0909297426825684, 2.07949479645146, nan, 2.5364572244833736, 2.5598472144103956, 2.555591396101628, nan, 3.001285467820001, 3.0141120008059867, 3.0204196394382556, nan, -0.008395907674198922, 0.0, 0.008395907674198922, nan, 0.5284733601242309, 0.5479425538604203, 0.5452651754726286, nan, 1.0563157453049374, 1.0841470984807897, 1.0731075606533353, nan, 1.568314460938554, 1.5997494986604055, 1.5851062762869517, nan, 2.0615318029681338, 2.0909297426825684, 2.0783236183165315, nan, 2.537628402618302, 2.5598472144103956, 2.5544202179666997, nan, 3.002456645954929, 3.0141120008059867, 3.0192484613033272, nan, -0.0051691185231098, 0.0, 0.0051691185231098, nan, 0.5317001492753199, 0.5479425538604203, 0.5420383863215396, nan, 1.0595425344560265, 1.0841470984807897, 1.0698807715022463, nan, 1.571541250089643, 1.5997494986604055, 1.5818794871358626, nan, 2.064758592119223, 2.0909297426825684, 2.0750968291654424, nan, 2.540855191769391, 2.5598472144103956, 2.5511934288156106, nan, 3.0056834351060187, 3.0141120008059867, 3.016021672152238, nan, -0.0006767488782524558, 0.0, 0.0006767488782524558, nan, 0.5361925189201773, 0.5479425538604203, 0.5375460166766822, nan, 1.0640349041008839, 1.0841470984807897, 1.065388401857389, nan, 1.5760336197345004, 1.5997494986604055, 1.5773871174910052, nan, 2.06925096176408, 2.0909297426825684, 2.070604459520585, nan, 2.5453475614142484, 2.5598472144103956, 2.5467010591707533, nan, 3.010175804750876, 3.0141120008059867, 3.011529302507381, nan, 0.0039813124944433585, 0.0, -0.0039813124944433585, nan, 0.5408505802928731, 0.5479425538604203, 0.5328879553039864, nan, 1.0686929654735797, 1.0841470984807897, 1.060730340484693, nan, 1.5806916811071963, 1.5997494986604055, 1.5727290561183094, nan, 2.073909023136776, 2.0909297426825684, 2.065946398147889, nan, 2.5500056227869443, 2.5598472144103956, 2.5420429977980574, nan, 3.0148338661235714, 3.0141120008059867, 3.006871241134685, nan, 0.007664609715371957, 0.0, -0.007664609715371957, nan, 0.5445338775138017, 0.5479425538604203, 0.5292046580830578, nan, 1.0723762626945084, 1.0841470984807897, 1.0570470432637644, nan, 1.5843749783281247, 1.5997494986604055, 1.569045758897381, nan, 2.0775923203577045, 2.0909297426825684, 2.0622631009269607, nan, 2.5536889200078727, 2.5598472144103956, 2.538359700577129, nan, 3.0185171633445003, 3.0141120008059867, 3.003187943913756, nan, 0.009471343165368589, 0.0, -0.009471343165368589, nan, 0.5463406109637984, 0.5479425538604203, 0.5273979246330611, nan, 1.074182996144505, 1.0841470984807897, 1.055240309813768, nan, 1.5861817117781214, 1.5997494986604055, 1.5672390254473842, nan, 2.0793990538077014, 2.0909297426825684, 2.0604563674769643, nan, 2.5554956534578697, 2.5598472144103956, 2.536552967127132, nan, 3.0203238967944968, 3.0141120008059867, 3.0013812104637596, nan], mode='lines', name='Arrow', line=Line(color='rgb(114, 132, 314)', width=1) From ca33267d8dbb265898a05e28356b49b555761d23 Mon Sep 17 00:00:00 2001 From: Chelsea Date: 2015年5月28日 20:43:19 -0400 Subject: [PATCH 22/34] test quiver --- .../tests/test_core/test_tools/test_Quiver.py | 148 +++++++++++++++++- 1 file changed, 144 insertions(+), 4 deletions(-) diff --git a/plotly/tests/test_core/test_tools/test_Quiver.py b/plotly/tests/test_core/test_tools/test_Quiver.py index 623c403a81..0cf4b5a41e 100644 --- a/plotly/tests/test_core/test_tools/test_Quiver.py +++ b/plotly/tests/test_core/test_tools/test_Quiver.py @@ -50,15 +50,155 @@ def test_one_arrow(): def test_complicated(): nan = np.nan trace1 = Scatter( - x=[0.0, 0.1, nan, 0.0, 0.1, nan, 0.0, 0.1, nan, 0.0, 0.1, nan, 0.0, 0.1, nan, 0.0, 0.1, nan, 0.0, 0.1, nan, 0.5, 0.5877582561890373, nan, 0.5, 0.5877582561890373, nan, 0.5, 0.5877582561890373, nan, 0.5, 0.5877582561890373, nan, 0.5, 0.5877582561890373, nan, 0.5, 0.5877582561890373, nan, 0.5, 0.5877582561890373, nan, 1.0, 1.0540302305868139, nan, 1.0, 1.0540302305868139, nan, 1.0, 1.0540302305868139, nan, 1.0, 1.0540302305868139, nan, 1.0, 1.0540302305868139, nan, 1.0, 1.0540302305868139, nan, 1.0, 1.0540302305868139, nan, 1.5, 1.5070737201667703, nan, 1.5, 1.5070737201667703, nan, 1.5, 1.5070737201667703, nan, 1.5, 1.5070737201667703, nan, 1.5, 1.5070737201667703, nan, 1.5, 1.5070737201667703, nan, 1.5, 1.5070737201667703, nan, 2.0, 1.9583853163452858, nan, 2.0, 1.9583853163452858, nan, 2.0, 1.9583853163452858, nan, 2.0, 1.9583853163452858, nan, 2.0, 1.9583853163452858, nan, 2.0, 1.9583853163452858, nan, 2.0, 1.9583853163452858, nan, 2.5, 2.4198856384453067, nan, 2.5, 2.4198856384453067, nan, 2.5, 2.4198856384453067, nan, 2.5, 2.4198856384453067, nan, 2.5, 2.4198856384453067, nan, 2.5, 2.4198856384453067, nan, 2.5, 2.4198856384453067, nan, 3.0, 2.9010007503399553, nan, 3.0, 2.9010007503399553, nan, 3.0, 2.9010007503399553, nan, 3.0, 2.9010007503399553, nan, 3.0, 2.9010007503399553, nan, 3.0, 2.9010007503399553, nan, 3.0, 2.9010007503399553, nan], - y=[0.0, 0.0, nan, 0.5, 0.5479425538604203, nan, 1.0, 1.0841470984807897, nan, 1.5, 1.5997494986604055, nan, 2.0, 2.0909297426825684, nan, 2.5, 2.5598472144103956, nan, 3.0, 3.0141120008059867, nan, 0.0, 0.0, nan, 0.5, 0.5479425538604203, nan, 1.0, 1.0841470984807897, nan, 1.5, 1.5997494986604055, nan, 2.0, 2.0909297426825684, nan, 2.5, 2.5598472144103956, nan, 3.0, 3.0141120008059867, nan, 0.0, 0.0, nan, 0.5, 0.5479425538604203, nan, 1.0, 1.0841470984807897, nan, 1.5, 1.5997494986604055, nan, 2.0, 2.0909297426825684, nan, 2.5, 2.5598472144103956, nan, 3.0, 3.0141120008059867, nan, 0.0, 0.0, nan, 0.5, 0.5479425538604203, nan, 1.0, 1.0841470984807897, nan, 1.5, 1.5997494986604055, nan, 2.0, 2.0909297426825684, nan, 2.5, 2.5598472144103956, nan, 3.0, 3.0141120008059867, nan, 0.0, 0.0, nan, 0.5, 0.5479425538604203, nan, 1.0, 1.0841470984807897, nan, 1.5, 1.5997494986604055, nan, 2.0, 2.0909297426825684, nan, 2.5, 2.5598472144103956, nan, 3.0, 3.0141120008059867, nan, 0.0, 0.0, nan, 0.5, 0.5479425538604203, nan, 1.0, 1.0841470984807897, nan, 1.5, 1.5997494986604055, nan, 2.0, 2.0909297426825684, nan, 2.5, 2.5598472144103956, nan, 3.0, 3.0141120008059867, nan, 0.0, 0.0, nan, 0.5, 0.5479425538604203, nan, 1.0, 1.0841470984807897, nan, 1.5, 1.5997494986604055, nan, 2.0, 2.0909297426825684, nan, 2.5, 2.5598472144103956, nan, 3.0, 3.0141120008059867, nan], + x=[0.0, 0.1, nan, 0.0, 0.1, nan, 0.0, 0.1, nan, 0.0, 0.1, nan, 0.0, + 0.1, nan, 0.0, 0.1, nan, 0.0, 0.1, nan, 0.5, 0.5877582561890373, + nan, 0.5, 0.5877582561890373, nan, 0.5, 0.5877582561890373, nan, + 0.5, 0.5877582561890373, nan, 0.5, 0.5877582561890373, nan, 0.5, + 0.5877582561890373, nan, 0.5, 0.5877582561890373, nan, 1.0, + 1.0540302305868139, nan, 1.0, 1.0540302305868139, nan, 1.0, + 1.0540302305868139, nan, 1.0, 1.0540302305868139, nan, 1.0, + 1.0540302305868139, nan, 1.0, 1.0540302305868139, nan, 1.0, + 1.0540302305868139, nan, 1.5, 1.5070737201667703, nan, 1.5, + 1.5070737201667703, nan, 1.5, 1.5070737201667703, nan, 1.5, + 1.5070737201667703, nan, 1.5, 1.5070737201667703, nan, 1.5, + 1.5070737201667703, nan, 1.5, 1.5070737201667703, nan, 2.0, + 1.9583853163452858, nan, 2.0, 1.9583853163452858, nan, 2.0, + 1.9583853163452858, nan, 2.0, 1.9583853163452858, nan, 2.0, + 1.9583853163452858, nan, 2.0, 1.9583853163452858, nan, 2.0, + 1.9583853163452858, nan, 2.5, 2.4198856384453067, nan, 2.5, + 2.4198856384453067, nan, 2.5, 2.4198856384453067, nan, 2.5, + 2.4198856384453067, nan, 2.5, 2.4198856384453067, nan, 2.5, + 2.4198856384453067, nan, 2.5, 2.4198856384453067, nan, 3.0, + 2.9010007503399553, nan, 3.0, 2.9010007503399553, nan, 3.0, + 2.9010007503399553, nan, 3.0, 2.9010007503399553, nan, 3.0, + 2.9010007503399553, nan, 3.0, 2.9010007503399553, nan, 3.0, + 2.9010007503399553, nan], + y=[0.0, 0.0, nan, 0.5, 0.5479425538604203, nan, 1.0, + 1.0841470984807897, nan, 1.5, 1.5997494986604055, nan, 2.0, + 2.0909297426825684, nan, 2.5, 2.5598472144103956, nan, 3.0, + 3.0141120008059867, nan, 0.0, 0.0, nan, 0.5, 0.5479425538604203, + nan, 1.0, 1.0841470984807897, nan, 1.5, 1.5997494986604055, nan, + 2.0, 2.0909297426825684, nan, 2.5, 2.5598472144103956, nan, 3.0, + 3.0141120008059867, nan, 0.0, 0.0, nan, 0.5, 0.5479425538604203, + nan, 1.0, 1.0841470984807897, nan, 1.5, 1.5997494986604055, nan, + 2.0, 2.0909297426825684, nan, 2.5, 2.5598472144103956, nan, 3.0, + 3.0141120008059867, nan, 0.0, 0.0, nan, 0.5, 0.5479425538604203, + nan, 1.0, 1.0841470984807897, nan, 1.5, 1.5997494986604055, nan, + 2.0, 2.0909297426825684, nan, 2.5, 2.5598472144103956, nan, 3.0, + 3.0141120008059867, nan, 0.0, 0.0, nan, 0.5, 0.5479425538604203, + nan, 1.0, 1.0841470984807897, nan, 1.5, 1.5997494986604055, nan, + 2.0, 2.0909297426825684, nan, 2.5, 2.5598472144103956, nan, 3.0, + 3.0141120008059867, nan, 0.0, 0.0, nan, 0.5, 0.5479425538604203, + nan, 1.0, 1.0841470984807897, nan, 1.5, 1.5997494986604055, nan, + 2.0, 2.0909297426825684, nan, 2.5, 2.5598472144103956, nan, 3.0, + 3.0141120008059867, nan, 0.0, 0.0, nan, 0.5, 0.5479425538604203, + nan, 1.0, 1.0841470984807897, nan, 1.5, 1.5997494986604055, nan, + 2.0, 2.0909297426825684, nan, 2.5, 2.5598472144103956, nan, 3.0, + 3.0141120008059867, nan], mode='lines', name='Barb', line=Line(color='rgb(114, 132, 314)', width=2) ) trace2 = Scatter( - x=[0.07690301168721783, 0.1, 0.07690301168721783, nan, 0.08148971695413129, 0.1, 0.07231630642030437, nan, 0.0849534368047658, 0.1, 0.06885258656966987, nan, 0.08644613181823306, 0.1, 0.06735989155620262, nan, 0.08560233819567775, 0.1, 0.06820368517875791, nan, 0.08262864604423276, 0.1, 0.0711773773302029, nan, 0.07825311891371131, 0.1, 0.07555290446072435, nan, 0.5674887420135539, 0.5877582561890373, 0.5674887420135539, nan, 0.5720754472804674, 0.5877582561890373, 0.5629020367466405, nan, 0.5755391671311019, 0.5877582561890373, 0.559438316896006, nan, 0.5770318621445691, 0.5877582561890373, 0.5579456218825387, nan, 0.5761880685220139, 0.5877582561890373, 0.558789415505094, nan, 0.5732143763705688, 0.5877582561890373, 0.561763107656539, nan, 0.5688388492400475, 0.5877582561890373, 0.5661386347870605, nan, 1.0415508745428081, 1.0540302305868139, 1.0415508745428081, nan, 1.0461375798097217, 1.0540302305868139, 1.0369641692758949, nan, 1.049601299660356, 1.0540302305868139, 1.0335004494252602, nan, 1.0510939946738234, 1.0540302305868139, 1.032007754411793, nan, 1.0502502010512682, 1.0540302305868139, 1.0328515480343483, nan, 1.047276508899823, 1.0540302305868139, 1.0358252401857933, nan, 1.0429009817693018, 1.0540302305868139, 1.0402007673163147, nan, 1.5054399038465724, 1.5070737201667703, 1.5054399038465724, nan, 1.510026609113486, 1.5070737201667703, 1.500853198579659, nan, 1.5134903289641204, 1.5070737201667703, 1.4973894787290245, nan, 1.5149830239775877, 1.5070737201667703, 1.4958967837155572, nan, 1.5141392303550323, 1.5070737201667703, 1.4967405773381126, nan, 1.5111655382035873, 1.5070737201667703, 1.4997142694895575, nan, 1.5067900110730659, 1.5070737201667703, 1.504089796620079, nan, 1.9679970549654164, 1.9583853163452858, 1.9679970549654164, nan, 1.97258376023233, 1.9583853163452858, 1.963410349698503, nan, 1.9760474800829644, 1.9583853163452858, 1.9599466298478685, nan, 1.9775401750964317, 1.9583853163452858, 1.9584539348344012, nan, 1.9766963814738763, 1.9583853163452858, 1.9592977284569564, nan, 1.9737226893224313, 1.9583853163452858, 1.9622714206084015, nan, 1.9693471621919099, 1.9583853163452858, 1.966646947738923, nan, 2.438389643170454, 2.4198856384453067, 2.438389643170454, nan, 2.4429763484373677, 2.4198856384453067, 2.4338029379035406, nan, 2.4464400682880023, 2.4198856384453067, 2.4303392180529064, nan, 2.4479327633014694, 2.4198856384453067, 2.428846523039439, nan, 2.447088969678914, 2.4198856384453067, 2.4296903166619943, nan, 2.444115277527469, 2.4198856384453067, 2.4326640088134392, nan, 2.4397397503969476, 2.4198856384453067, 2.4370395359439607, nan, 2.9238665954636778, 2.9010007503399553, 2.9238665954636778, nan, 2.9284533007305913, 2.9010007503399553, 2.9192798901967643, nan, 2.931917020581226, 2.9010007503399553, 2.91581617034613, nan, 2.933409715594693, 2.9010007503399553, 2.9143234753326626, nan, 2.9325659219721376, 2.9010007503399553, 2.915167268955218, nan, 2.9295922298206927, 2.9010007503399553, 2.918140961106663, nan, 2.925216702690171, 2.9010007503399553, 2.9225164882371844, nan], - y=[-0.009567085809127246, 0.0, 0.009567085809127246, nan, 0.5273021819893026, 0.5479425538604203, 0.546436353607557, nan, 1.0551445671700093, 1.0841470984807897, 1.0742787387882637, nan, 1.5671432828036256, 1.5997494986604055, 1.58627745442188, nan, 2.0603606248332054, 2.0909297426825684, 2.07949479645146, nan, 2.5364572244833736, 2.5598472144103956, 2.555591396101628, nan, 3.001285467820001, 3.0141120008059867, 3.0204196394382556, nan, -0.008395907674198922, 0.0, 0.008395907674198922, nan, 0.5284733601242309, 0.5479425538604203, 0.5452651754726286, nan, 1.0563157453049374, 1.0841470984807897, 1.0731075606533353, nan, 1.568314460938554, 1.5997494986604055, 1.5851062762869517, nan, 2.0615318029681338, 2.0909297426825684, 2.0783236183165315, nan, 2.537628402618302, 2.5598472144103956, 2.5544202179666997, nan, 3.002456645954929, 3.0141120008059867, 3.0192484613033272, nan, -0.0051691185231098, 0.0, 0.0051691185231098, nan, 0.5317001492753199, 0.5479425538604203, 0.5420383863215396, nan, 1.0595425344560265, 1.0841470984807897, 1.0698807715022463, nan, 1.571541250089643, 1.5997494986604055, 1.5818794871358626, nan, 2.064758592119223, 2.0909297426825684, 2.0750968291654424, nan, 2.540855191769391, 2.5598472144103956, 2.5511934288156106, nan, 3.0056834351060187, 3.0141120008059867, 3.016021672152238, nan, -0.0006767488782524558, 0.0, 0.0006767488782524558, nan, 0.5361925189201773, 0.5479425538604203, 0.5375460166766822, nan, 1.0640349041008839, 1.0841470984807897, 1.065388401857389, nan, 1.5760336197345004, 1.5997494986604055, 1.5773871174910052, nan, 2.06925096176408, 2.0909297426825684, 2.070604459520585, nan, 2.5453475614142484, 2.5598472144103956, 2.5467010591707533, nan, 3.010175804750876, 3.0141120008059867, 3.011529302507381, nan, 0.0039813124944433585, 0.0, -0.0039813124944433585, nan, 0.5408505802928731, 0.5479425538604203, 0.5328879553039864, nan, 1.0686929654735797, 1.0841470984807897, 1.060730340484693, nan, 1.5806916811071963, 1.5997494986604055, 1.5727290561183094, nan, 2.073909023136776, 2.0909297426825684, 2.065946398147889, nan, 2.5500056227869443, 2.5598472144103956, 2.5420429977980574, nan, 3.0148338661235714, 3.0141120008059867, 3.006871241134685, nan, 0.007664609715371957, 0.0, -0.007664609715371957, nan, 0.5445338775138017, 0.5479425538604203, 0.5292046580830578, nan, 1.0723762626945084, 1.0841470984807897, 1.0570470432637644, nan, 1.5843749783281247, 1.5997494986604055, 1.569045758897381, nan, 2.0775923203577045, 2.0909297426825684, 2.0622631009269607, nan, 2.5536889200078727, 2.5598472144103956, 2.538359700577129, nan, 3.0185171633445003, 3.0141120008059867, 3.003187943913756, nan, 0.009471343165368589, 0.0, -0.009471343165368589, nan, 0.5463406109637984, 0.5479425538604203, 0.5273979246330611, nan, 1.074182996144505, 1.0841470984807897, 1.055240309813768, nan, 1.5861817117781214, 1.5997494986604055, 1.5672390254473842, nan, 2.0793990538077014, 2.0909297426825684, 2.0604563674769643, nan, 2.5554956534578697, 2.5598472144103956, 2.536552967127132, nan, 3.0203238967944968, 3.0141120008059867, 3.0013812104637596, nan], + x=[0.07690301168721783, 0.1, 0.07690301168721783, nan, + 0.08148971695413129, 0.1, 0.07231630642030437, nan, + 0.0849534368047658, 0.1, 0.06885258656966987, nan, + 0.08644613181823306, 0.1, 0.06735989155620262, nan, + 0.08560233819567775, 0.1, 0.06820368517875791, nan, + 0.08262864604423276, 0.1, 0.0711773773302029, nan, + 0.07825311891371131, 0.1, 0.07555290446072435, nan, + 0.5674887420135539, 0.5877582561890373, 0.5674887420135539, nan, + 0.5720754472804674, 0.5877582561890373, 0.5629020367466405, nan, + 0.5755391671311019, 0.5877582561890373, 0.559438316896006, nan, + 0.5770318621445691, 0.5877582561890373, 0.5579456218825387, nan, + 0.5761880685220139, 0.5877582561890373, 0.558789415505094, nan, + 0.5732143763705688, 0.5877582561890373, 0.561763107656539, nan, + 0.5688388492400475, 0.5877582561890373, 0.5661386347870605, nan, + 1.0415508745428081, 1.0540302305868139, 1.0415508745428081, nan, + 1.0461375798097217, 1.0540302305868139, 1.0369641692758949, nan, + 1.049601299660356, 1.0540302305868139, 1.0335004494252602, nan, + 1.0510939946738234, 1.0540302305868139, 1.032007754411793, nan, + 1.0502502010512682, 1.0540302305868139, 1.0328515480343483, nan, + 1.047276508899823, 1.0540302305868139, 1.0358252401857933, nan, + 1.0429009817693018, 1.0540302305868139, 1.0402007673163147, nan, + .5054399038465724, 1.5070737201667703, 1.5054399038465724, nan, + 1.510026609113486, 1.5070737201667703, 1.500853198579659, nan, + 1.5134903289641204, 1.5070737201667703, 1.4973894787290245, nan, + 1.5149830239775877, 1.5070737201667703, 1.4958967837155572, nan, + 1.5141392303550323, 1.5070737201667703, 1.4967405773381126, nan, + 1.5111655382035873, 1.5070737201667703, 1.4997142694895575, nan, + 1.5067900110730659, 1.5070737201667703, 1.504089796620079, nan, + 1.9679970549654164, 1.9583853163452858, 1.9679970549654164, nan, + 1.97258376023233, 1.9583853163452858, 1.963410349698503, nan, + 1.9760474800829644, 1.9583853163452858, 1.9599466298478685, nan, + 1.9775401750964317, 1.9583853163452858, 1.9584539348344012, nan, + 1.9766963814738763, 1.9583853163452858, 1.9592977284569564, nan, + 1.9737226893224313, 1.9583853163452858, 1.9622714206084015, nan, + 1.9693471621919099, 1.9583853163452858, 1.966646947738923, nan, + 2.438389643170454, 2.4198856384453067, 2.438389643170454, nan, + 2.4429763484373677, 2.4198856384453067, 2.4338029379035406, nan, + 2.4464400682880023, 2.4198856384453067, 2.4303392180529064, nan, + 2.4479327633014694, 2.4198856384453067, 2.428846523039439, nan, + 2.447088969678914, 2.4198856384453067, 2.4296903166619943, nan, + 2.444115277527469, 2.4198856384453067, 2.4326640088134392, nan, + 2.4397397503969476, 2.4198856384453067, 2.4370395359439607, nan, + 2.9238665954636778, 2.9010007503399553, 2.9238665954636778, nan, + 2.9284533007305913, 2.9010007503399553, 2.9192798901967643, nan, + 2.931917020581226, 2.9010007503399553, 2.91581617034613, nan, + 2.933409715594693, 2.9010007503399553, 2.9143234753326626, nan, + 2.9325659219721376, 2.9010007503399553, 2.915167268955218, nan, + 2.9295922298206927, 2.9010007503399553, 2.918140961106663, nan, + 2.925216702690171, 2.9010007503399553, 2.9225164882371844, nan], + y=[-0.009567085809127246, 0.0, 0.009567085809127246, nan, + 0.5273021819893026, 0.5479425538604203, 0.546436353607557, nan, + 1.0551445671700093, 1.0841470984807897, 1.0742787387882637, nan, + 1.5671432828036256, 1.5997494986604055, 1.58627745442188, nan, + 2.0603606248332054, 2.0909297426825684, 2.07949479645146, nan, + 2.5364572244833736, 2.5598472144103956, 2.555591396101628, nan, + 3.001285467820001, 3.0141120008059867, 3.0204196394382556, nan, + -0.008395907674198922, 0.0, 0.008395907674198922, nan, + 0.5284733601242309, 0.5479425538604203, 0.5452651754726286, nan, + 1.0563157453049374, 1.0841470984807897, 1.0731075606533353, nan, + 1.568314460938554, 1.5997494986604055, 1.5851062762869517, nan, + 2.0615318029681338, 2.0909297426825684, 2.0783236183165315, nan, + 2.537628402618302, 2.5598472144103956, 2.5544202179666997, nan, + 3.002456645954929, 3.0141120008059867, 3.0192484613033272, nan, + -0.0051691185231098, 0.0, 0.0051691185231098, nan, + 0.5317001492753199, 0.5479425538604203, 0.5420383863215396, nan, + 1.0595425344560265, 1.0841470984807897, 1.0698807715022463, nan, + 1.571541250089643, 1.5997494986604055, 1.5818794871358626, nan, + 2.064758592119223, 2.0909297426825684, 2.0750968291654424, nan, + 2.540855191769391, 2.5598472144103956, 2.5511934288156106, nan, + 3.0056834351060187, 3.0141120008059867, 3.016021672152238, nan, + -0.0006767488782524558, 0.0, 0.0006767488782524558, nan, + 0.5361925189201773, 0.5479425538604203, 0.5375460166766822, nan, + 1.0640349041008839, 1.0841470984807897, 1.065388401857389, nan, + 1.5760336197345004, 1.5997494986604055, 1.5773871174910052, nan, + 2.06925096176408, 2.0909297426825684, 2.070604459520585, nan, + 2.5453475614142484, 2.5598472144103956, 2.5467010591707533, nan, + 3.010175804750876, 3.0141120008059867, 3.011529302507381, nan, + 0.0039813124944433585, 0.0, -0.0039813124944433585, nan, + 0.5408505802928731, 0.5479425538604203, 0.5328879553039864, nan, + 1.0686929654735797, 1.0841470984807897, 1.060730340484693, nan, + 1.5806916811071963, 1.5997494986604055, 1.5727290561183094, nan, + 2.073909023136776, 2.0909297426825684, 2.065946398147889, nan, + 2.5500056227869443, 2.5598472144103956, 2.5420429977980574, nan, + 3.0148338661235714, 3.0141120008059867, 3.006871241134685, nan, + 0.007664609715371957, 0.0, -0.007664609715371957, nan, + 0.5445338775138017, 0.5479425538604203, 0.5292046580830578, nan, + 1.0723762626945084, 1.0841470984807897, 1.0570470432637644, nan, + 1.5843749783281247, 1.5997494986604055, 1.569045758897381, nan, + 2.0775923203577045, 2.0909297426825684, 2.0622631009269607, nan, + 2.5536889200078727, 2.5598472144103956, 2.538359700577129, nan, + 3.0185171633445003, 3.0141120008059867, 3.003187943913756, nan, + 0.009471343165368589, 0.0, -0.009471343165368589, nan, + 0.5463406109637984, 0.5479425538604203, 0.5273979246330611, nan, + 1.074182996144505, 1.0841470984807897, 1.055240309813768, nan, + 1.5861817117781214, 1.5997494986604055, 1.5672390254473842, nan, + 2.0793990538077014, 2.0909297426825684, 2.0604563674769643, nan, + 2.5554956534578697, 2.5598472144103956, 2.536552967127132, nan, + 3.0203238967944968, 3.0141120008059867, 3.0013812104637596, + nan], mode='lines', name='Arrow', line=Line(color='rgb(114, 132, 314)', width=1) From 302f77fca7df8a0d99cb63ae2b1f0c292b4896b7 Mon Sep 17 00:00:00 2001 From: Chelsea Date: 2015年5月28日 23:24:35 -0400 Subject: [PATCH 23/34] quiver test --- .../tests/test_core/test_tools/test_Quiver.py | 155 +----------------- 1 file changed, 8 insertions(+), 147 deletions(-) diff --git a/plotly/tests/test_core/test_tools/test_Quiver.py b/plotly/tests/test_core/test_tools/test_Quiver.py index 0cf4b5a41e..81469d3bb0 100644 --- a/plotly/tests/test_core/test_tools/test_Quiver.py +++ b/plotly/tests/test_core/test_tools/test_Quiver.py @@ -50,166 +50,27 @@ def test_one_arrow(): def test_complicated(): nan = np.nan trace1 = Scatter( - x=[0.0, 0.1, nan, 0.0, 0.1, nan, 0.0, 0.1, nan, 0.0, 0.1, nan, 0.0, - 0.1, nan, 0.0, 0.1, nan, 0.0, 0.1, nan, 0.5, 0.5877582561890373, - nan, 0.5, 0.5877582561890373, nan, 0.5, 0.5877582561890373, nan, - 0.5, 0.5877582561890373, nan, 0.5, 0.5877582561890373, nan, 0.5, - 0.5877582561890373, nan, 0.5, 0.5877582561890373, nan, 1.0, - 1.0540302305868139, nan, 1.0, 1.0540302305868139, nan, 1.0, - 1.0540302305868139, nan, 1.0, 1.0540302305868139, nan, 1.0, - 1.0540302305868139, nan, 1.0, 1.0540302305868139, nan, 1.0, - 1.0540302305868139, nan, 1.5, 1.5070737201667703, nan, 1.5, - 1.5070737201667703, nan, 1.5, 1.5070737201667703, nan, 1.5, - 1.5070737201667703, nan, 1.5, 1.5070737201667703, nan, 1.5, - 1.5070737201667703, nan, 1.5, 1.5070737201667703, nan, 2.0, - 1.9583853163452858, nan, 2.0, 1.9583853163452858, nan, 2.0, - 1.9583853163452858, nan, 2.0, 1.9583853163452858, nan, 2.0, - 1.9583853163452858, nan, 2.0, 1.9583853163452858, nan, 2.0, - 1.9583853163452858, nan, 2.5, 2.4198856384453067, nan, 2.5, - 2.4198856384453067, nan, 2.5, 2.4198856384453067, nan, 2.5, - 2.4198856384453067, nan, 2.5, 2.4198856384453067, nan, 2.5, - 2.4198856384453067, nan, 2.5, 2.4198856384453067, nan, 3.0, - 2.9010007503399553, nan, 3.0, 2.9010007503399553, nan, 3.0, - 2.9010007503399553, nan, 3.0, 2.9010007503399553, nan, 3.0, - 2.9010007503399553, nan, 3.0, 2.9010007503399553, nan, 3.0, - 2.9010007503399553, nan], - y=[0.0, 0.0, nan, 0.5, 0.5479425538604203, nan, 1.0, - 1.0841470984807897, nan, 1.5, 1.5997494986604055, nan, 2.0, - 2.0909297426825684, nan, 2.5, 2.5598472144103956, nan, 3.0, - 3.0141120008059867, nan, 0.0, 0.0, nan, 0.5, 0.5479425538604203, - nan, 1.0, 1.0841470984807897, nan, 1.5, 1.5997494986604055, nan, - 2.0, 2.0909297426825684, nan, 2.5, 2.5598472144103956, nan, 3.0, - 3.0141120008059867, nan, 0.0, 0.0, nan, 0.5, 0.5479425538604203, - nan, 1.0, 1.0841470984807897, nan, 1.5, 1.5997494986604055, nan, - 2.0, 2.0909297426825684, nan, 2.5, 2.5598472144103956, nan, 3.0, - 3.0141120008059867, nan, 0.0, 0.0, nan, 0.5, 0.5479425538604203, - nan, 1.0, 1.0841470984807897, nan, 1.5, 1.5997494986604055, nan, - 2.0, 2.0909297426825684, nan, 2.5, 2.5598472144103956, nan, 3.0, - 3.0141120008059867, nan, 0.0, 0.0, nan, 0.5, 0.5479425538604203, - nan, 1.0, 1.0841470984807897, nan, 1.5, 1.5997494986604055, nan, - 2.0, 2.0909297426825684, nan, 2.5, 2.5598472144103956, nan, 3.0, - 3.0141120008059867, nan, 0.0, 0.0, nan, 0.5, 0.5479425538604203, - nan, 1.0, 1.0841470984807897, nan, 1.5, 1.5997494986604055, nan, - 2.0, 2.0909297426825684, nan, 2.5, 2.5598472144103956, nan, 3.0, - 3.0141120008059867, nan, 0.0, 0.0, nan, 0.5, 0.5479425538604203, - nan, 1.0, 1.0841470984807897, nan, 1.5, 1.5997494986604055, nan, - 2.0, 2.0909297426825684, nan, 2.5, 2.5598472144103956, nan, 3.0, - 3.0141120008059867, nan], + x=[0.0, 0.5, nan, 0.0, 0.5, nan, 0.0, 0.5, nan, 0.0, 0.5, nan, 0.0, 0.5, nan, 0.0, 0.5, nan, 0.0, 0.5, nan, 0.5, 0.9387912809451864, nan, 0.5, 0.9387912809451864, nan, 0.5, 0.9387912809451864, nan, 0.5, 0.9387912809451864, nan, 0.5, 0.9387912809451864, nan, 0.5, 0.9387912809451864, nan, 0.5, 0.9387912809451864, nan, 1.0, 1.2701511529340699, nan, 1.0, 1.2701511529340699, nan, 1.0, 1.2701511529340699, nan, 1.0, 1.2701511529340699, nan, 1.0, 1.2701511529340699, nan, 1.0, 1.2701511529340699, nan, 1.0, 1.2701511529340699, nan, 1.5, 1.5353686008338514, nan, 1.5, 1.5353686008338514, nan, 1.5, 1.5353686008338514, nan, 1.5, 1.5353686008338514, nan, 1.5, 1.5353686008338514, nan, 1.5, 1.5353686008338514, nan, 1.5, 1.5353686008338514, nan, 2.0, 1.7919265817264287, nan, 2.0, 1.7919265817264287, nan, 2.0, 1.7919265817264287, nan, 2.0, 1.7919265817264287, nan, 2.0, 1.7919265817264287, nan, 2.0, 1.7919265817264287, nan, 2.0, 1.7919265817264287, nan, 2.5, 2.099428192226533, nan, 2.5, 2.099428192226533, nan, 2.5, 2.099428192226533, nan, 2.5, 2.099428192226533, nan, 2.5, 2.099428192226533, nan, 2.5, 2.099428192226533, nan, 2.5, 2.099428192226533, nan, 3.0, 2.5050037516997774, nan, 3.0, 2.5050037516997774, nan, 3.0, 2.5050037516997774, nan, 3.0, 2.5050037516997774, nan, 3.0, 2.5050037516997774, nan, 3.0, 2.5050037516997774, nan, 3.0, 2.5050037516997774, nan], + y=[0.0, 0.0, nan, 0.5, 0.7397127693021015, nan, 1.0, 1.4207354924039484, nan, 1.5, 1.9987474933020273, nan, 2.0, 2.454648713412841, nan, 2.5, 2.799236072051978, nan, 3.0, 3.0705600040299337, nan, 0.0, 0.0, nan, 0.5, 0.7397127693021015, nan, 1.0, 1.4207354924039484, nan, 1.5, 1.9987474933020273, nan, 2.0, 2.454648713412841, nan, 2.5, 2.799236072051978, nan, 3.0, 3.0705600040299337, nan, 0.0, 0.0, nan, 0.5, 0.7397127693021015, nan, 1.0, 1.4207354924039484, nan, 1.5, 1.9987474933020273, nan, 2.0, 2.454648713412841, nan, 2.5, 2.799236072051978, nan, 3.0, 3.0705600040299337, nan, 0.0, 0.0, nan, 0.5, 0.7397127693021015, nan, 1.0, 1.4207354924039484, nan, 1.5, 1.9987474933020273, nan, 2.0, 2.454648713412841, nan, 2.5, 2.799236072051978, nan, 3.0, 3.0705600040299337, nan, 0.0, 0.0, nan, 0.5, 0.7397127693021015, nan, 1.0, 1.4207354924039484, nan, 1.5, 1.9987474933020273, nan, 2.0, 2.454648713412841, nan, 2.5, 2.799236072051978, nan, 3.0, 3.0705600040299337, nan, 0.0, 0.0, nan, 0.5, 0.7397127693021015, nan, 1.0, 1.4207354924039484, nan, 1.5, 1.9987474933020273, nan, 2.0, 2.454648713412841, nan, 2.5, 2.799236072051978, nan, 3.0, 3.0705600040299337, nan, 0.0, 0.0, nan, 0.5, 0.7397127693021015, nan, 1.0, 1.4207354924039484, nan, 1.5, 1.9987474933020273, nan, 2.0, 2.454648713412841, nan, 2.5, 2.799236072051978, nan, 3.0, 3.0705600040299337, nan], mode='lines', name='Barb', line=Line(color='rgb(114, 132, 314)', width=2) ) trace2 = Scatter( - x=[0.07690301168721783, 0.1, 0.07690301168721783, nan, - 0.08148971695413129, 0.1, 0.07231630642030437, nan, - 0.0849534368047658, 0.1, 0.06885258656966987, nan, - 0.08644613181823306, 0.1, 0.06735989155620262, nan, - 0.08560233819567775, 0.1, 0.06820368517875791, nan, - 0.08262864604423276, 0.1, 0.0711773773302029, nan, - 0.07825311891371131, 0.1, 0.07555290446072435, nan, - 0.5674887420135539, 0.5877582561890373, 0.5674887420135539, nan, - 0.5720754472804674, 0.5877582561890373, 0.5629020367466405, nan, - 0.5755391671311019, 0.5877582561890373, 0.559438316896006, nan, - 0.5770318621445691, 0.5877582561890373, 0.5579456218825387, nan, - 0.5761880685220139, 0.5877582561890373, 0.558789415505094, nan, - 0.5732143763705688, 0.5877582561890373, 0.561763107656539, nan, - 0.5688388492400475, 0.5877582561890373, 0.5661386347870605, nan, - 1.0415508745428081, 1.0540302305868139, 1.0415508745428081, nan, - 1.0461375798097217, 1.0540302305868139, 1.0369641692758949, nan, - 1.049601299660356, 1.0540302305868139, 1.0335004494252602, nan, - 1.0510939946738234, 1.0540302305868139, 1.032007754411793, nan, - 1.0502502010512682, 1.0540302305868139, 1.0328515480343483, nan, - 1.047276508899823, 1.0540302305868139, 1.0358252401857933, nan, - 1.0429009817693018, 1.0540302305868139, 1.0402007673163147, nan, - .5054399038465724, 1.5070737201667703, 1.5054399038465724, nan, - 1.510026609113486, 1.5070737201667703, 1.500853198579659, nan, - 1.5134903289641204, 1.5070737201667703, 1.4973894787290245, nan, - 1.5149830239775877, 1.5070737201667703, 1.4958967837155572, nan, - 1.5141392303550323, 1.5070737201667703, 1.4967405773381126, nan, - 1.5111655382035873, 1.5070737201667703, 1.4997142694895575, nan, - 1.5067900110730659, 1.5070737201667703, 1.504089796620079, nan, - 1.9679970549654164, 1.9583853163452858, 1.9679970549654164, nan, - 1.97258376023233, 1.9583853163452858, 1.963410349698503, nan, - 1.9760474800829644, 1.9583853163452858, 1.9599466298478685, nan, - 1.9775401750964317, 1.9583853163452858, 1.9584539348344012, nan, - 1.9766963814738763, 1.9583853163452858, 1.9592977284569564, nan, - 1.9737226893224313, 1.9583853163452858, 1.9622714206084015, nan, - 1.9693471621919099, 1.9583853163452858, 1.966646947738923, nan, - 2.438389643170454, 2.4198856384453067, 2.438389643170454, nan, - 2.4429763484373677, 2.4198856384453067, 2.4338029379035406, nan, - 2.4464400682880023, 2.4198856384453067, 2.4303392180529064, nan, - 2.4479327633014694, 2.4198856384453067, 2.428846523039439, nan, - 2.447088969678914, 2.4198856384453067, 2.4296903166619943, nan, - 2.444115277527469, 2.4198856384453067, 2.4326640088134392, nan, - 2.4397397503969476, 2.4198856384453067, 2.4370395359439607, nan, - 2.9238665954636778, 2.9010007503399553, 2.9238665954636778, nan, - 2.9284533007305913, 2.9010007503399553, 2.9192798901967643, nan, - 2.931917020581226, 2.9010007503399553, 2.91581617034613, nan, - 2.933409715594693, 2.9010007503399553, 2.9143234753326626, nan, - 2.9325659219721376, 2.9010007503399553, 2.915167268955218, nan, - 2.9295922298206927, 2.9010007503399553, 2.918140961106663, nan, - 2.925216702690171, 2.9010007503399553, 2.9225164882371844, nan], - y=[-0.009567085809127246, 0.0, 0.009567085809127246, nan, - 0.5273021819893026, 0.5479425538604203, 0.546436353607557, nan, - 1.0551445671700093, 1.0841470984807897, 1.0742787387882637, nan, - 1.5671432828036256, 1.5997494986604055, 1.58627745442188, nan, - 2.0603606248332054, 2.0909297426825684, 2.07949479645146, nan, - 2.5364572244833736, 2.5598472144103956, 2.555591396101628, nan, - 3.001285467820001, 3.0141120008059867, 3.0204196394382556, nan, - -0.008395907674198922, 0.0, 0.008395907674198922, nan, - 0.5284733601242309, 0.5479425538604203, 0.5452651754726286, nan, - 1.0563157453049374, 1.0841470984807897, 1.0731075606533353, nan, - 1.568314460938554, 1.5997494986604055, 1.5851062762869517, nan, - 2.0615318029681338, 2.0909297426825684, 2.0783236183165315, nan, - 2.537628402618302, 2.5598472144103956, 2.5544202179666997, nan, - 3.002456645954929, 3.0141120008059867, 3.0192484613033272, nan, - -0.0051691185231098, 0.0, 0.0051691185231098, nan, - 0.5317001492753199, 0.5479425538604203, 0.5420383863215396, nan, - 1.0595425344560265, 1.0841470984807897, 1.0698807715022463, nan, - 1.571541250089643, 1.5997494986604055, 1.5818794871358626, nan, - 2.064758592119223, 2.0909297426825684, 2.0750968291654424, nan, - 2.540855191769391, 2.5598472144103956, 2.5511934288156106, nan, - 3.0056834351060187, 3.0141120008059867, 3.016021672152238, nan, - -0.0006767488782524558, 0.0, 0.0006767488782524558, nan, - 0.5361925189201773, 0.5479425538604203, 0.5375460166766822, nan, - 1.0640349041008839, 1.0841470984807897, 1.065388401857389, nan, - 1.5760336197345004, 1.5997494986604055, 1.5773871174910052, nan, - 2.06925096176408, 2.0909297426825684, 2.070604459520585, nan, - 2.5453475614142484, 2.5598472144103956, 2.5467010591707533, nan, - 3.010175804750876, 3.0141120008059867, 3.011529302507381, nan, - 0.0039813124944433585, 0.0, -0.0039813124944433585, nan, - 0.5408505802928731, 0.5479425538604203, 0.5328879553039864, nan, - 1.0686929654735797, 1.0841470984807897, 1.060730340484693, nan, - 1.5806916811071963, 1.5997494986604055, 1.5727290561183094, nan, - 2.073909023136776, 2.0909297426825684, 2.065946398147889, nan, - 2.5500056227869443, 2.5598472144103956, 2.5420429977980574, nan, - 3.0148338661235714, 3.0141120008059867, 3.006871241134685, nan, - 0.007664609715371957, 0.0, -0.007664609715371957, nan, - 0.5445338775138017, 0.5479425538604203, 0.5292046580830578, nan, - 1.0723762626945084, 1.0841470984807897, 1.0570470432637644, nan, - 1.5843749783281247, 1.5997494986604055, 1.569045758897381, nan, - 2.0775923203577045, 2.0909297426825684, 2.0622631009269607, nan, - 2.5536889200078727, 2.5598472144103956, 2.538359700577129, nan, - 3.0185171633445003, 3.0141120008059867, 3.003187943913756, nan, - 0.009471343165368589, 0.0, -0.009471343165368589, nan, - 0.5463406109637984, 0.5479425538604203, 0.5273979246330611, nan, - 1.074182996144505, 1.0841470984807897, 1.055240309813768, nan, - 1.5861817117781214, 1.5997494986604055, 1.5672390254473842, nan, - 2.0793990538077014, 2.0909297426825684, 2.0604563674769643, nan, - 2.5554956534578697, 2.5598472144103956, 2.536552967127132, nan, - 3.0203238967944968, 3.0141120008059867, 3.0013812104637596, - nan], + x=[0.38451505843608913, 0.5, 0.38451505843608913, nan, 0.40744858477065643, 0.5, 0.3615815321015219, nan, 0.4247671840238289, 0.5, 0.3442629328483494, nan, 0.43223065909116526, 0.5, 0.33679945778101306, nan, 0.42801169097838865, 0.5, 0.3410184258937896, nan, 0.4131432302211637, 0.5, 0.3558868866510146, nan, 0.39126559456855653, 0.5, 0.37776452230362173, nan, 0.8374437100677695, 0.9387912809451864, 0.8374437100677695, nan, 0.8603772364023368, 0.9387912809451864, 0.8145101837332023, nan, 0.8776958356555092, 0.9387912809451864, 0.7971915844800297, nan, 0.8851593107228456, 0.9387912809451864, 0.7897281094126934, nan, 0.880940342610069, 0.9387912809451864, 0.79394707752547, nan, 0.8660718818528441, 0.9387912809451864, 0.8088155382826949, nan, 0.8441942462002369, 0.9387912809451864, 0.8306931739353021, nan, 1.2077543727140414, 1.2701511529340699, 1.2077543727140414, nan, 1.2306878990486088, 1.2701511529340699, 1.1848208463794743, nan, 1.2480064983017813, 1.2701511529340699, 1.1675022471263017, nan, 1.2554699733691175, 1.2701511529340699, 1.1600387720589653, nan, 1.251251005256341, 1.2701511529340699, 1.164257740171742, nan, 1.236382544499116, 1.2701511529340699, 1.179126200928967, nan, 1.214504908846509, 1.2701511529340699, 1.201003836581574, nan, 1.5271995192328622, 1.5353686008338514, 1.5271995192328622, nan, 1.5501330455674294, 1.5353686008338514, 1.5042659928982949, nan, 1.567451644820602, 1.5353686008338514, 1.4869473936451223, nan, 1.5749151198879383, 1.5353686008338514, 1.4794839185777862, nan, 1.5706961517751616, 1.5353686008338514, 1.4837028866905626, nan, 1.5558276910179367, 1.5353686008338514, 1.4985713474477875, nan, 1.5339500553653296, 1.5353686008338514, 1.5204489831003947, nan, 1.8399852748270817, 1.7919265817264287, 1.8399852748270817, nan, 1.862918801161649, 1.7919265817264287, 1.8170517484925146, nan, 1.8802374004148217, 1.7919265817264287, 1.799733149239342, nan, 1.887700875482158, 1.7919265817264287, 1.7922696741720057, nan, 1.8834819073693814, 1.7919265817264287, 1.7964886422847823, nan, 1.8686134466121564, 1.7919265817264287, 1.8113571030420073, nan, 1.8467358109595493, 1.7919265817264287, 1.8332347386946144, nan, 2.1919482158522707, 2.099428192226533, 2.1919482158522707, nan, 2.2148817421868383, 2.099428192226533, 2.1690146895177036, nan, 2.2322003414400107, 2.099428192226533, 2.1516960902645312, nan, 2.2396638165073472, 2.099428192226533, 2.1442326151971947, nan, 2.2354448483945704, 2.099428192226533, 2.1484515833099715, nan, 2.2205763876373457, 2.099428192226533, 2.1633200440671962, nan, 2.1986987519847383, 2.099428192226533, 2.1851976797198036, nan, 2.61933297731839, 2.5050037516997774, 2.61933297731839, nan, 2.6422665036529573, 2.5050037516997774, 2.5963994509838226, nan, 2.6595851029061297, 2.5050037516997774, 2.5790808517306503, nan, 2.6670485779734663, 2.5050037516997774, 2.571617376663314, nan, 2.6628296098606894, 2.5050037516997774, 2.5758363447760906, nan, 2.6479611491034647, 2.5050037516997774, 2.5907048055333153, nan, 2.6260835134508573, 2.5050037516997774, 2.6125824411859226, nan], + y=[-0.04783542904563622, 0.0, 0.04783542904563622, nan, 0.6365109099465124, 0.7397127693021015, 0.7321817680377849, nan, 1.2757228358500456, 1.4207354924039484, 1.371393693941318, nan, 1.835716414018128, 1.9987474933020273, 1.9313872721094003, nan, 2.3018031241660264, 2.454648713412841, 2.397473982257299, nan, 2.682286122416868, 2.799236072051978, 2.7779569805081405, nan, 3.006427339100005, 3.0705600040299337, 3.1020981971912773, nan, -0.041979538370994586, 0.0, 0.041979538370994586, nan, 0.642366800621154, 0.7397127693021015, 0.7263258773631432, nan, 1.2815787265246874, 1.4207354924039484, 1.3655378032666765, nan, 1.8415723046927694, 1.9987474933020273, 1.9255313814347585, nan, 2.307659014840668, 2.454648713412841, 2.3916180915826573, nan, 2.6881420130915097, 2.799236072051978, 2.772101089833499, nan, 3.0122832297746465, 3.0705600040299337, 3.096242306516636, nan, -0.02584559261554904, 0.0, 0.02584559261554904, nan, 0.6585007463765996, 0.7397127693021015, 0.7101919316076977, nan, 1.2977126722801329, 1.4207354924039484, 1.349403857511231, nan, 1.857706250448215, 1.9987474933020273, 1.909397435679313, nan, 2.3237929605961134, 2.454648713412841, 2.375484145827212, nan, 2.704275958846955, 2.799236072051978, 2.7559671440780535, nan, 3.0284171755300924, 3.0705600040299337, 3.0801083607611903, nan, -0.003383744391262258, 0.0, 0.003383744391262258, nan, 0.6809625946008864, 0.7397127693021015, 0.6877300833834109, nan, 1.3201745205044197, 1.4207354924039484, 1.3269420092869442, nan, 1.8801680986725018, 1.9987474933020273, 1.8869355874550262, nan, 2.3462548088204005, 2.454648713412841, 2.3530222976029247, nan, 2.7267378070712422, 2.799236072051978, 2.7335052958537664, nan, 3.050879023754379, 3.0705600040299337, 3.0576465125369037, nan, 0.019906562472216813, 0.0, -0.019906562472216813, nan, 0.7042529014643655, 0.7397127693021015, 0.6644397765199318, nan, 1.3434648273678986, 1.4207354924039484, 1.303651702423465, nan, 1.903458405535981, 1.9987474933020273, 1.863645280591547, nan, 2.3695451156838794, 2.454648713412841, 2.329731990739446, nan, 2.750028113934721, 2.799236072051978, 2.7102149889902876, nan, 3.074169330617858, 3.0705600040299337, 3.0343562056734243, nan, 0.03832304857685983, 0.0, -0.03832304857685983, nan, 0.7226693875690084, 0.7397127693021015, 0.6460232904152888, nan, 1.3618813134725418, 1.4207354924039484, 1.285235216318822, nan, 1.9218748916406239, 1.9987474933020273, 1.8452287944869041, nan, 2.3879616017885223, 2.454648713412841, 2.311315504634803, nan, 2.768444600039364, 2.799236072051978, 2.6917985028856446, nan, 3.092585816722501, 3.0705600040299337, 3.0159397195687814, nan, 0.04735671582684286, 0.0, -0.04735671582684286, nan, 0.7317030548189914, 0.7397127693021015, 0.6369896231653058, nan, 1.3709149807225247, 1.4207354924039484, 1.276201549068839, nan, 1.9309085588906068, 1.9987474933020273, 1.8361951272369212, nan, 2.3969952690385052, 2.454648713412841, 2.30228183738482, nan, 2.7774782672893474, 2.799236072051978, 2.6827648356356617, nan, 3.101619483972484, 3.0705600040299337, 3.0069060523187985, nan], mode='lines', name='Arrow', - line=Line(color='rgb(114, 132, 314)', width=1) + line=Line(color='rgb(114, 132, 314)', width=3) ) expected = Data([trace1, trace2]) x, y = np.meshgrid(np.arange(0, np.pi, .5), np.arange(0, np.pi, .5)) u = np.cos(x) v = np.sin(y) - data = tls.Quiver(x, y, u, v, scale=.1, angle=np.pi/8, - arrow_scale=.25, barb_width=2) + data = tls.Quiver(x, y, u, v, scale=.5, angle=np.pi/8, + arrow_scale=.25, barb_width=2 + arrow_width=3) # np.testing.assert_almost_equal(data[0]['y'], expected[0]['y']) # np.testing.assert_almost_equal(data[0]['x'], expected[0]['x']) From 624537518f54a760de5cbabb13980d31e105c808 Mon Sep 17 00:00:00 2001 From: Chelsea Date: 2015年5月28日 23:25:36 -0400 Subject: [PATCH 24/34] test --- plotly/tests/test_core/test_tools/test_Quiver.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plotly/tests/test_core/test_tools/test_Quiver.py b/plotly/tests/test_core/test_tools/test_Quiver.py index 81469d3bb0..a9cad84000 100644 --- a/plotly/tests/test_core/test_tools/test_Quiver.py +++ b/plotly/tests/test_core/test_tools/test_Quiver.py @@ -69,7 +69,7 @@ def test_complicated(): u = np.cos(x) v = np.sin(y) data = tls.Quiver(x, y, u, v, scale=.5, angle=np.pi/8, - arrow_scale=.25, barb_width=2 + arrow_scale=.25, barb_width=2, arrow_width=3) # np.testing.assert_almost_equal(data[0]['y'], expected[0]['y']) From 70d78f6815aa7f7d8e1d63f0cb897eae46fa8919 Mon Sep 17 00:00:00 2001 From: Chelsea Date: 2015年5月28日 23:38:29 -0400 Subject: [PATCH 25/34] test spacing --- .../tests/test_core/test_tools/test_Quiver.py | 156 +++++++++++++++++- 1 file changed, 150 insertions(+), 6 deletions(-) diff --git a/plotly/tests/test_core/test_tools/test_Quiver.py b/plotly/tests/test_core/test_tools/test_Quiver.py index a9cad84000..c66d115596 100644 --- a/plotly/tests/test_core/test_tools/test_Quiver.py +++ b/plotly/tests/test_core/test_tools/test_Quiver.py @@ -50,15 +50,159 @@ def test_one_arrow(): def test_complicated(): nan = np.nan trace1 = Scatter( - x=[0.0, 0.5, nan, 0.0, 0.5, nan, 0.0, 0.5, nan, 0.0, 0.5, nan, 0.0, 0.5, nan, 0.0, 0.5, nan, 0.0, 0.5, nan, 0.5, 0.9387912809451864, nan, 0.5, 0.9387912809451864, nan, 0.5, 0.9387912809451864, nan, 0.5, 0.9387912809451864, nan, 0.5, 0.9387912809451864, nan, 0.5, 0.9387912809451864, nan, 0.5, 0.9387912809451864, nan, 1.0, 1.2701511529340699, nan, 1.0, 1.2701511529340699, nan, 1.0, 1.2701511529340699, nan, 1.0, 1.2701511529340699, nan, 1.0, 1.2701511529340699, nan, 1.0, 1.2701511529340699, nan, 1.0, 1.2701511529340699, nan, 1.5, 1.5353686008338514, nan, 1.5, 1.5353686008338514, nan, 1.5, 1.5353686008338514, nan, 1.5, 1.5353686008338514, nan, 1.5, 1.5353686008338514, nan, 1.5, 1.5353686008338514, nan, 1.5, 1.5353686008338514, nan, 2.0, 1.7919265817264287, nan, 2.0, 1.7919265817264287, nan, 2.0, 1.7919265817264287, nan, 2.0, 1.7919265817264287, nan, 2.0, 1.7919265817264287, nan, 2.0, 1.7919265817264287, nan, 2.0, 1.7919265817264287, nan, 2.5, 2.099428192226533, nan, 2.5, 2.099428192226533, nan, 2.5, 2.099428192226533, nan, 2.5, 2.099428192226533, nan, 2.5, 2.099428192226533, nan, 2.5, 2.099428192226533, nan, 2.5, 2.099428192226533, nan, 3.0, 2.5050037516997774, nan, 3.0, 2.5050037516997774, nan, 3.0, 2.5050037516997774, nan, 3.0, 2.5050037516997774, nan, 3.0, 2.5050037516997774, nan, 3.0, 2.5050037516997774, nan, 3.0, 2.5050037516997774, nan], - y=[0.0, 0.0, nan, 0.5, 0.7397127693021015, nan, 1.0, 1.4207354924039484, nan, 1.5, 1.9987474933020273, nan, 2.0, 2.454648713412841, nan, 2.5, 2.799236072051978, nan, 3.0, 3.0705600040299337, nan, 0.0, 0.0, nan, 0.5, 0.7397127693021015, nan, 1.0, 1.4207354924039484, nan, 1.5, 1.9987474933020273, nan, 2.0, 2.454648713412841, nan, 2.5, 2.799236072051978, nan, 3.0, 3.0705600040299337, nan, 0.0, 0.0, nan, 0.5, 0.7397127693021015, nan, 1.0, 1.4207354924039484, nan, 1.5, 1.9987474933020273, nan, 2.0, 2.454648713412841, nan, 2.5, 2.799236072051978, nan, 3.0, 3.0705600040299337, nan, 0.0, 0.0, nan, 0.5, 0.7397127693021015, nan, 1.0, 1.4207354924039484, nan, 1.5, 1.9987474933020273, nan, 2.0, 2.454648713412841, nan, 2.5, 2.799236072051978, nan, 3.0, 3.0705600040299337, nan, 0.0, 0.0, nan, 0.5, 0.7397127693021015, nan, 1.0, 1.4207354924039484, nan, 1.5, 1.9987474933020273, nan, 2.0, 2.454648713412841, nan, 2.5, 2.799236072051978, nan, 3.0, 3.0705600040299337, nan, 0.0, 0.0, nan, 0.5, 0.7397127693021015, nan, 1.0, 1.4207354924039484, nan, 1.5, 1.9987474933020273, nan, 2.0, 2.454648713412841, nan, 2.5, 2.799236072051978, nan, 3.0, 3.0705600040299337, nan, 0.0, 0.0, nan, 0.5, 0.7397127693021015, nan, 1.0, 1.4207354924039484, nan, 1.5, 1.9987474933020273, nan, 2.0, 2.454648713412841, nan, 2.5, 2.799236072051978, nan, 3.0, 3.0705600040299337, nan], + x=[0.0, 0.5, nan, 0.0, 0.5, nan, 0.0, 0.5, nan, 0.0, 0.5, nan, 0.0, + .5, nan, 0.0, 0.5, nan, 0.0, 0.5, nan, 0.5, 0.9387912809451864, + nan, 0.5, 0.9387912809451864, nan, 0.5, 0.9387912809451864, nan, + 0.5, 0.9387912809451864, nan, 0.5, 0.9387912809451864, nan, 0.5, + 0.9387912809451864, nan, 0.5, 0.9387912809451864, nan, 1.0, + 1.2701511529340699, nan, 1.0, 1.2701511529340699, nan, 1.0, + 1.2701511529340699, nan, 1.0, 1.2701511529340699, nan, 1.0, + 1.2701511529340699, nan, 1.0, 1.2701511529340699, nan, 1.0, + 1.2701511529340699, nan, 1.5, 1.5353686008338514, nan, 1.5, + 1.5353686008338514, nan, 1.5, 1.5353686008338514, nan, 1.5, + 1.5353686008338514, nan, 1.5, 1.5353686008338514, nan, 1.5, + 1.5353686008338514, nan, 1.5, 1.5353686008338514, nan, 2.0, + 1.7919265817264287, nan, 2.0, 1.7919265817264287, nan, 2.0, + 1.7919265817264287, nan, 2.0, 1.7919265817264287, nan, 2.0, + 1.7919265817264287, nan, 2.0, 1.7919265817264287, nan, 2.0, + 1.7919265817264287, nan, 2.5, 2.099428192226533, nan, 2.5, + 2.099428192226533, nan, 2.5, 2.099428192226533, nan, 2.5, + 2.099428192226533, nan, 2.5, 2.099428192226533, nan, 2.5, + 2.099428192226533, nan, 2.5, 2.099428192226533, nan, 3.0, + 2.5050037516997774, nan, 3.0, 2.5050037516997774, nan, 3.0, + 2.5050037516997774, nan, 3.0, 2.5050037516997774, nan, 3.0, + 2.5050037516997774, nan, 3.0, 2.5050037516997774, nan, 3.0, + 2.5050037516997774, nan], + y=[0.0, 0.0, nan, 0.5, 0.7397127693021015, nan, 1.0, + 1.4207354924039484, nan, 1.5, 1.9987474933020273, nan, 2.0, + 2.454648713412841, nan, 2.5, 2.799236072051978, nan, 3.0, + 3.0705600040299337, nan, 0.0, 0.0, nan, 0.5, 0.7397127693021015, + nan, 1.0, 1.4207354924039484, nan, 1.5, 1.9987474933020273, nan, + 2.0, 2.454648713412841, nan, 2.5, 2.799236072051978, nan, 3.0, + 3.0705600040299337, nan, 0.0, 0.0, nan, 0.5, 0.7397127693021015, + nan, 1.0, 1.4207354924039484, nan, 1.5, 1.9987474933020273, nan, + 2.0, 2.454648713412841, nan, 2.5, 2.799236072051978, nan, 3.0, + 3.0705600040299337, nan, 0.0, 0.0, nan, 0.5, 0.7397127693021015, + nan, 1.0, 1.4207354924039484, nan, 1.5, 1.9987474933020273, nan, + 2.0, 2.454648713412841, nan, 2.5, 2.799236072051978, nan, 3.0, + 3.0705600040299337, nan, 0.0, 0.0, nan, 0.5, 0.7397127693021015, + nan, 1.0, 1.4207354924039484, nan, 1.5, 1.9987474933020273, nan, + 2.0, 2.454648713412841, nan, 2.5, 2.799236072051978, nan, 3.0, + 3.0705600040299337, nan, 0.0, 0.0, nan, 0.5, 0.7397127693021015, + nan, 1.0, 1.4207354924039484, nan, 1.5, 1.9987474933020273, nan, + 2.0, 2.454648713412841, nan, 2.5, 2.799236072051978, nan, 3.0, + 3.0705600040299337, nan, 0.0, 0.0, nan, 0.5, 0.7397127693021015, + nan, 1.0, 1.4207354924039484, nan, 1.5, 1.9987474933020273, nan, + 2.0, 2.454648713412841, nan, 2.5, 2.799236072051978, nan, 3.0, + 3.0705600040299337, nan], mode='lines', name='Barb', line=Line(color='rgb(114, 132, 314)', width=2) ) trace2 = Scatter( - x=[0.38451505843608913, 0.5, 0.38451505843608913, nan, 0.40744858477065643, 0.5, 0.3615815321015219, nan, 0.4247671840238289, 0.5, 0.3442629328483494, nan, 0.43223065909116526, 0.5, 0.33679945778101306, nan, 0.42801169097838865, 0.5, 0.3410184258937896, nan, 0.4131432302211637, 0.5, 0.3558868866510146, nan, 0.39126559456855653, 0.5, 0.37776452230362173, nan, 0.8374437100677695, 0.9387912809451864, 0.8374437100677695, nan, 0.8603772364023368, 0.9387912809451864, 0.8145101837332023, nan, 0.8776958356555092, 0.9387912809451864, 0.7971915844800297, nan, 0.8851593107228456, 0.9387912809451864, 0.7897281094126934, nan, 0.880940342610069, 0.9387912809451864, 0.79394707752547, nan, 0.8660718818528441, 0.9387912809451864, 0.8088155382826949, nan, 0.8441942462002369, 0.9387912809451864, 0.8306931739353021, nan, 1.2077543727140414, 1.2701511529340699, 1.2077543727140414, nan, 1.2306878990486088, 1.2701511529340699, 1.1848208463794743, nan, 1.2480064983017813, 1.2701511529340699, 1.1675022471263017, nan, 1.2554699733691175, 1.2701511529340699, 1.1600387720589653, nan, 1.251251005256341, 1.2701511529340699, 1.164257740171742, nan, 1.236382544499116, 1.2701511529340699, 1.179126200928967, nan, 1.214504908846509, 1.2701511529340699, 1.201003836581574, nan, 1.5271995192328622, 1.5353686008338514, 1.5271995192328622, nan, 1.5501330455674294, 1.5353686008338514, 1.5042659928982949, nan, 1.567451644820602, 1.5353686008338514, 1.4869473936451223, nan, 1.5749151198879383, 1.5353686008338514, 1.4794839185777862, nan, 1.5706961517751616, 1.5353686008338514, 1.4837028866905626, nan, 1.5558276910179367, 1.5353686008338514, 1.4985713474477875, nan, 1.5339500553653296, 1.5353686008338514, 1.5204489831003947, nan, 1.8399852748270817, 1.7919265817264287, 1.8399852748270817, nan, 1.862918801161649, 1.7919265817264287, 1.8170517484925146, nan, 1.8802374004148217, 1.7919265817264287, 1.799733149239342, nan, 1.887700875482158, 1.7919265817264287, 1.7922696741720057, nan, 1.8834819073693814, 1.7919265817264287, 1.7964886422847823, nan, 1.8686134466121564, 1.7919265817264287, 1.8113571030420073, nan, 1.8467358109595493, 1.7919265817264287, 1.8332347386946144, nan, 2.1919482158522707, 2.099428192226533, 2.1919482158522707, nan, 2.2148817421868383, 2.099428192226533, 2.1690146895177036, nan, 2.2322003414400107, 2.099428192226533, 2.1516960902645312, nan, 2.2396638165073472, 2.099428192226533, 2.1442326151971947, nan, 2.2354448483945704, 2.099428192226533, 2.1484515833099715, nan, 2.2205763876373457, 2.099428192226533, 2.1633200440671962, nan, 2.1986987519847383, 2.099428192226533, 2.1851976797198036, nan, 2.61933297731839, 2.5050037516997774, 2.61933297731839, nan, 2.6422665036529573, 2.5050037516997774, 2.5963994509838226, nan, 2.6595851029061297, 2.5050037516997774, 2.5790808517306503, nan, 2.6670485779734663, 2.5050037516997774, 2.571617376663314, nan, 2.6628296098606894, 2.5050037516997774, 2.5758363447760906, nan, 2.6479611491034647, 2.5050037516997774, 2.5907048055333153, nan, 2.6260835134508573, 2.5050037516997774, 2.6125824411859226, nan], - y=[-0.04783542904563622, 0.0, 0.04783542904563622, nan, 0.6365109099465124, 0.7397127693021015, 0.7321817680377849, nan, 1.2757228358500456, 1.4207354924039484, 1.371393693941318, nan, 1.835716414018128, 1.9987474933020273, 1.9313872721094003, nan, 2.3018031241660264, 2.454648713412841, 2.397473982257299, nan, 2.682286122416868, 2.799236072051978, 2.7779569805081405, nan, 3.006427339100005, 3.0705600040299337, 3.1020981971912773, nan, -0.041979538370994586, 0.0, 0.041979538370994586, nan, 0.642366800621154, 0.7397127693021015, 0.7263258773631432, nan, 1.2815787265246874, 1.4207354924039484, 1.3655378032666765, nan, 1.8415723046927694, 1.9987474933020273, 1.9255313814347585, nan, 2.307659014840668, 2.454648713412841, 2.3916180915826573, nan, 2.6881420130915097, 2.799236072051978, 2.772101089833499, nan, 3.0122832297746465, 3.0705600040299337, 3.096242306516636, nan, -0.02584559261554904, 0.0, 0.02584559261554904, nan, 0.6585007463765996, 0.7397127693021015, 0.7101919316076977, nan, 1.2977126722801329, 1.4207354924039484, 1.349403857511231, nan, 1.857706250448215, 1.9987474933020273, 1.909397435679313, nan, 2.3237929605961134, 2.454648713412841, 2.375484145827212, nan, 2.704275958846955, 2.799236072051978, 2.7559671440780535, nan, 3.0284171755300924, 3.0705600040299337, 3.0801083607611903, nan, -0.003383744391262258, 0.0, 0.003383744391262258, nan, 0.6809625946008864, 0.7397127693021015, 0.6877300833834109, nan, 1.3201745205044197, 1.4207354924039484, 1.3269420092869442, nan, 1.8801680986725018, 1.9987474933020273, 1.8869355874550262, nan, 2.3462548088204005, 2.454648713412841, 2.3530222976029247, nan, 2.7267378070712422, 2.799236072051978, 2.7335052958537664, nan, 3.050879023754379, 3.0705600040299337, 3.0576465125369037, nan, 0.019906562472216813, 0.0, -0.019906562472216813, nan, 0.7042529014643655, 0.7397127693021015, 0.6644397765199318, nan, 1.3434648273678986, 1.4207354924039484, 1.303651702423465, nan, 1.903458405535981, 1.9987474933020273, 1.863645280591547, nan, 2.3695451156838794, 2.454648713412841, 2.329731990739446, nan, 2.750028113934721, 2.799236072051978, 2.7102149889902876, nan, 3.074169330617858, 3.0705600040299337, 3.0343562056734243, nan, 0.03832304857685983, 0.0, -0.03832304857685983, nan, 0.7226693875690084, 0.7397127693021015, 0.6460232904152888, nan, 1.3618813134725418, 1.4207354924039484, 1.285235216318822, nan, 1.9218748916406239, 1.9987474933020273, 1.8452287944869041, nan, 2.3879616017885223, 2.454648713412841, 2.311315504634803, nan, 2.768444600039364, 2.799236072051978, 2.6917985028856446, nan, 3.092585816722501, 3.0705600040299337, 3.0159397195687814, nan, 0.04735671582684286, 0.0, -0.04735671582684286, nan, 0.7317030548189914, 0.7397127693021015, 0.6369896231653058, nan, 1.3709149807225247, 1.4207354924039484, 1.276201549068839, nan, 1.9309085588906068, 1.9987474933020273, 1.8361951272369212, nan, 2.3969952690385052, 2.454648713412841, 2.30228183738482, nan, 2.7774782672893474, 2.799236072051978, 2.6827648356356617, nan, 3.101619483972484, 3.0705600040299337, 3.0069060523187985, nan], + x=[0.38451505843608913, 0.5, 0.38451505843608913, nan, + 0.40744858477065643, 0.5, 0.3615815321015219, nan, + 0.4247671840238289, 0.5, 0.3442629328483494, nan, + 0.43223065909116526, 0.5, 0.33679945778101306, nan, + 0.42801169097838865, 0.5, 0.3410184258937896, nan, + 0.4131432302211637, 0.5, 0.3558868866510146, nan, + 0.39126559456855653, 0.5, 0.37776452230362173, nan, + 0.8374437100677695, 0.9387912809451864, 0.8374437100677695, nan, + 0.8603772364023368, 0.9387912809451864, 0.8145101837332023, nan, + 0.8776958356555092, 0.9387912809451864, 0.7971915844800297, nan, + 0.8851593107228456, 0.9387912809451864, 0.7897281094126934, nan, + 0.880940342610069, 0.9387912809451864, 0.79394707752547, nan, + 0.8660718818528441, 0.9387912809451864, 0.8088155382826949, nan, + 0.8441942462002369, 0.9387912809451864, 0.8306931739353021, nan, + 1.2077543727140414, 1.2701511529340699, 1.2077543727140414, nan, + 1.2306878990486088, 1.2701511529340699, 1.1848208463794743, nan, + 1.2480064983017813, 1.2701511529340699, 1.1675022471263017, nan, + 1.2554699733691175, 1.2701511529340699, 1.1600387720589653, nan, + 1.251251005256341, 1.2701511529340699, 1.164257740171742, nan, + 1.236382544499116, 1.2701511529340699, 1.179126200928967, nan, + 1.214504908846509, 1.2701511529340699, 1.201003836581574, nan, + 1.5271995192328622, 1.5353686008338514, 1.5271995192328622, nan, + 1.5501330455674294, 1.5353686008338514, 1.5042659928982949, nan, + 1.567451644820602, 1.5353686008338514, 1.4869473936451223, nan, + 1.5749151198879383, 1.5353686008338514, 1.4794839185777862, nan, + 1.5706961517751616, 1.5353686008338514, 1.4837028866905626, nan, + 1.5558276910179367, 1.5353686008338514, 1.4985713474477875, nan, + 1.5339500553653296, 1.5353686008338514, 1.5204489831003947, nan, + 1.8399852748270817, 1.7919265817264287, 1.8399852748270817, nan, + 1.862918801161649, 1.7919265817264287, 1.8170517484925146, nan, + 1.8802374004148217, 1.7919265817264287, 1.799733149239342, nan, + 1.887700875482158, 1.7919265817264287, 1.7922696741720057, nan, + 1.8834819073693814, 1.7919265817264287, 1.7964886422847823, nan, + 1.8686134466121564, 1.7919265817264287, 1.8113571030420073, nan, + 1.8467358109595493, 1.7919265817264287, 1.8332347386946144, nan, + 2.1919482158522707, 2.099428192226533, 2.1919482158522707, nan, + 2.2148817421868383, 2.099428192226533, 2.1690146895177036, nan, + 2.2322003414400107, 2.099428192226533, 2.1516960902645312, nan, + 2.2396638165073472, 2.099428192226533, 2.1442326151971947, nan, + 2.2354448483945704, 2.099428192226533, 2.1484515833099715, nan, + 2.2205763876373457, 2.099428192226533, 2.1633200440671962, nan, + 2.1986987519847383, 2.099428192226533, 2.1851976797198036, nan, + 2.61933297731839, 2.5050037516997774, 2.61933297731839, nan, + 2.6422665036529573, 2.5050037516997774, 2.5963994509838226, nan, + 2.6595851029061297, 2.5050037516997774, 2.5790808517306503, nan, + 2.6670485779734663, 2.5050037516997774, 2.571617376663314, nan, + 2.6628296098606894, 2.5050037516997774, 2.5758363447760906, nan, + 2.6479611491034647, 2.5050037516997774, 2.5907048055333153, nan, + 2.6260835134508573, 2.5050037516997774, 2.6125824411859226, nan + ], + y=[-0.04783542904563622, 0.0, 0.04783542904563622, nan, + 0.6365109099465124, 0.7397127693021015, 0.7321817680377849, + nan, 1.2757228358500456, 1.4207354924039484, 1.371393693941318, + nan, 1.835716414018128, 1.9987474933020273, 1.9313872721094003, + nan, 2.3018031241660264, 2.454648713412841, 2.397473982257299, + nan, 2.682286122416868, 2.799236072051978, 2.7779569805081405, + nan, 3.006427339100005, 3.0705600040299337, 3.1020981971912773, + nan, -0.041979538370994586, 0.0, 0.041979538370994586, nan, + 0.642366800621154, 0.7397127693021015, 0.7263258773631432, nan, + 1.2815787265246874, 1.4207354924039484, 1.3655378032666765, + nan, 1.8415723046927694, 1.9987474933020273, + 1.9255313814347585, nan, 2.307659014840668, 2.454648713412841, + 2.3916180915826573, nan, 2.6881420130915097, 2.799236072051978, + 2.772101089833499, nan, 3.0122832297746465, 3.0705600040299337, + 3.096242306516636, nan, -0.02584559261554904, 0.0, + 0.02584559261554904, nan, 0.6585007463765996, + 0.7397127693021015, 0.7101919316076977, nan, + 1.2977126722801329, 1.4207354924039484, 1.349403857511231, nan, + 1.857706250448215, 1.9987474933020273, 1.909397435679313, nan, + 2.3237929605961134, 2.454648713412841, 2.375484145827212, nan, + 2.704275958846955, 2.799236072051978, 2.7559671440780535, nan, + 3.0284171755300924, 3.0705600040299337, 3.0801083607611903, + nan, -0.003383744391262258, 0.0, 0.003383744391262258, nan, + 0.6809625946008864, 0.7397127693021015, 0.6877300833834109, + nan, 1.3201745205044197, 1.4207354924039484, + 1.3269420092869442, nan, 1.8801680986725018, + 1.9987474933020273, 1.8869355874550262, nan, + 2.3462548088204005, 2.454648713412841, 2.3530222976029247, nan, + 2.7267378070712422, 2.799236072051978, 2.7335052958537664, nan, + 3.050879023754379, 3.0705600040299337, 3.0576465125369037, nan, + 0.019906562472216813, 0.0, -0.019906562472216813, nan, + 0.7042529014643655, 0.7397127693021015, 0.6644397765199318, + nan, 1.3434648273678986, 1.4207354924039484, 1.303651702423465, + nan, 1.903458405535981, 1.9987474933020273, 1.863645280591547, + nan, 2.3695451156838794, 2.454648713412841, 2.329731990739446, + nan, 2.750028113934721, 2.799236072051978, 2.7102149889902876, + nan, 3.074169330617858, 3.0705600040299337, 3.0343562056734243, + nan, 0.03832304857685983, 0.0, -0.03832304857685983, nan, + 0.7226693875690084, 0.7397127693021015, 0.6460232904152888, + nan, 1.3618813134725418, 1.4207354924039484, 1.285235216318822, + nan, 1.9218748916406239, 1.9987474933020273, + 1.8452287944869041, nan, 2.3879616017885223, 2.454648713412841, + 2.311315504634803, nan, 2.768444600039364, 2.799236072051978, + 2.6917985028856446, nan, 3.092585816722501, 3.0705600040299337, + 3.0159397195687814, nan, 0.04735671582684286, 0.0, + -0.04735671582684286, nan, 0.7317030548189914, + 0.7397127693021015, 0.6369896231653058, nan, + 1.3709149807225247, 1.4207354924039484, 1.276201549068839, nan, + 1.9309085588906068, 1.9987474933020273, 1.8361951272369212, + nan, 2.3969952690385052, 2.454648713412841, 2.30228183738482, + nan, 2.7774782672893474, 2.799236072051978, 2.6827648356356617, + nan, 3.101619483972484, 3.0705600040299337, 3.0069060523187985, + nan], mode='lines', name='Arrow', line=Line(color='rgb(114, 132, 314)', width=3) @@ -72,8 +216,8 @@ def test_complicated(): arrow_scale=.25, barb_width=2, arrow_width=3) - # np.testing.assert_almost_equal(data[0]['y'], expected[0]['y']) - # np.testing.assert_almost_equal(data[0]['x'], expected[0]['x']) + np.testing.assert_almost_equal(data[0]['y'], expected[0]['y']) + np.testing.assert_almost_equal(data[0]['x'], expected[0]['x']) np.testing.assert_almost_equal(data[1]['y'], expected[1]['y']) np.testing.assert_almost_equal(data[1]['x'], expected[1]['x']) assert data[0].keys() == expected[0].keys() From f6d043d292154e6389521e6dae98f492fcce1cb6 Mon Sep 17 00:00:00 2001 From: Chelsea Date: Thu, 4 Jun 2015 13:05:55 -0400 Subject: [PATCH 26/34] Quiver edits -remove numpy dependency -change to class -additional pep8 edits --- plotly/tools.py | 705 +++++++++++++----------------------------------- 1 file changed, 186 insertions(+), 519 deletions(-) diff --git a/plotly/tools.py b/plotly/tools.py index a1c57b8008..9bf58cd38b 100644 --- a/plotly/tools.py +++ b/plotly/tools.py @@ -1278,16 +1278,34 @@ def return_figure_from_figure_or_data(figure_or_data, validate_figure): return figure -def Quiver(x, y, u, v, - scale=.1, angle=np.pi/9, - arrow_scale=.3, barb_color='rgb(114, 132, 314)', - arrow_color='rgb(114, 132, 314)', arrow_width=1, - barb_width=1, **kwargs): +class Quiver(object): """Return a data object for a quiver plot. - x, y, u, and v can be np.ndarrays of equal dimmensions, - or lists where x & y are the same length and u & v are the same length. - x and y are the coordinates of the arrow locations. - u and v give the x and y components of the arrow vectors. + + :param (list or array) x: x coordinates of the arrow locations + :param (list or array) y: y coordinates of the arrow locations + :param (list or array) u: x components of the arrow vectors + :param (list or array) v: y components of the arrow vectors + :param (float in [0,1]) scale: scales size of the arrows(ideally to + avoid overlap). + :param (angle in radians) angle: angle of arrowhead. + :param (float in [0,1]) arrow_scale: value multiplied to length of barb to + get length of arrowhead. + :param barb_color (kwarg, color string, default = 'rgb(114, 132, 314)'): + Set color of barbs. + :param arrow_color (kwarg, color string, default = 'rgb(114, 132, 314)'): + Set color of arrow. + :param barb_width (kwarg, int or float greater than 0, default = 1): + Change width of lines for barbs. + :param arrow_width (kwarg, int or float greater than 0, default = 1): + Change width of lines for arrows. + + :rtype: (obj) data object + + :raises: (AttributeError) Will happen if x, y, u, and v are not all + the same length (or size if ndarray) + + :raises: (AttributeError) Will happen if scale, arrow_scale, barb_width, or + arrow_width are less than or equal to 0 Example 1: ``` @@ -1297,7 +1315,7 @@ def Quiver(x, y, u, v, Example 2: ``` - x,y = np.meshgrid(np.arange(0,np.pi,.2),np.arange(0,np.pi,.2) ) + x,y = np.meshgrid(np.arange(0,math.pi,.2),np.arange(0,math.pi,.2) ) u = np.cos(x) v = np.sin(y) data = Quiver(x, y, u, v) @@ -1310,513 +1328,162 @@ def Quiver(x, y, u, v, v = np.sin(x)*y; data = Quiver(x, y, u, v) ``` - - Keywords arguments with constant defaults: - - scale (kwarg, float in [0,1], default = .1): - Scales size of the arrows (ideally to avoid overlap). - - angle (kwarg, angle in radians, default = np.pi/9): - Angle of arrowhead. - - arrow_scale (kwarg, float in [0,1], default = 0.3): - Value multiplied to length of barb to get length of arrowhead. - - barb_color (kwarg, color string, default = color): - Set color of barbs. - - arrow_color (kwarg, color string, default = color): - Set color of arrow. - - barb_width (kwarg, int greater than or equal to 1, default = width): - Change width of lines for barbs. - - arrow_width (kwarg, int greater than or equal to 1, default = width): - Change width of lines for arrows. - """ - - if _numpy_imported is False: - raise Exception("To use Quiver() please import numpy as np") - - if len(x) != len(y): - raise Exception("x, y, u, and v should be ndarrays of equal dimmension" - "or lists where x and y are the same length" - "and u and v are the same length") - - if len(u) != len(v): - raise Exception("x, y, u, and v should be ndarrays of equal dimmension" - "or lists where x and y are the same length" - "and u and v are the same length") - - VALID_KWARGS = ['angle', 'scale', - 'arrow_scale', 'barb_color', - 'arrow_color', 'barb_width', - 'arrow_width'] - for key in kwargs.keys(): - if key not in VALID_KWARGS: - raise Exception("Invalid keyword argument: '{0}'".format(key)) - - # Make x y u v into np.array in case user entered list - x = np.array(x) - y = np.array(y) - u = np.array(u) - v = np.array(v) - - # Make arrays of x & y start values - Xstart = x.flatten('F') - Ystart = y.flatten('F') - - # Make arrays of x & y end values - Xend = x + (u * scale) - Xend = Xend.flatten('F') - Yend = y + (v * scale) - Yend = Yend.flatten('F') - - # Make array of nans - # These are used as spaces so the separate - # arrows can be included in 1 trace - nnn = np.empty((len(Xstart))) - nnn[:] = np.NAN - - # Combine arrays into matrix - Xvals = np.matrix([Xstart, Xend, nnn]) - # Make matrix into array readable by plotly - Xvals = np.array(Xvals) - Xvals = Xvals.flatten('F') - Xvals = Xvals.tolist() - - # Combine arrays into matrix - Yvals = np.matrix([Ystart, Yend, nnn]) - # Make matrix into array readable by plotly - Yvals = np.array(Yvals) - Yvals = Yvals.flatten('F') - Yvals = Yvals.tolist() - - # Make Trace1: the lines - trace1 = Scatter(x=Xvals, y=Yvals, - mode='lines', name='Barb', - line=Line(width=barb_width, color=barb_color)) - - # Arrows - # Get line lengths - # Default arrow length = 30% of line length - Xdif = Xend - Xstart - Ydif = Yend - Ystart - LineLen = np.sqrt(np.square(Xdif) + np.square(Ydif)) - ArrowLen = LineLen * arrow_scale - - # Get angle of line - LineAng = np.arctan(Ydif / Xdif) - - # Set angles to create arrow - # Default angle = +/- 20 degrees - Ang1 = LineAng + (angle) - Ang2 = LineAng - (angle) - - XSeg1 = np.cos(Ang1) * ArrowLen - YSeg1 = np.sin(Ang1) * ArrowLen - - XSeg2 = np.cos(Ang2) * ArrowLen - YSeg2 = np.sin(Ang2) * ArrowLen - - XPoint1 = np.empty((len(Xdif))) - YPoint1 = np.empty((len(Ydif))) - - XPoint2 = np.empty((len(Xdif))) - YPoint2 = np.empty((len(Ydif))) - - for index in range(len(Xdif)): - if Xdif[index]>= 0: - XPoint1[index] = Xend[index] - XSeg1[index] - YPoint1[index] = Yend[index] - YSeg1[index] - XPoint2[index] = Xend[index] - XSeg2[index] - YPoint2[index] = Yend[index] - YSeg2[index] - else: - XPoint1[index] = Xend[index] + XSeg1[index] - YPoint1[index] = Yend[index] + YSeg1[index] - XPoint2[index] = Xend[index] + XSeg2[index] - YPoint2[index] = Yend[index] + YSeg2[index] - - # Combine arrays into matrix - XArrows = np.matrix([XPoint1, Xend, XPoint2, nnn]) - # Make matrix into array readable by plotly - XArrows = np.array(XArrows) - XArrows = XArrows.flatten('F') - XArrows = XArrows.tolist() - - # Combine arrays into matrix - YArrows = np.matrix([YPoint1, Yend, YPoint2, nnn]) - # Make matrix into array readable by plotly - YArrows = np.array(YArrows) - YArrows = YArrows.flatten('F') - YArrows = YArrows.tolist() - - # Make trace2: the arrows - trace2 = Scatter(x=XArrows, y=YArrows, - mode='lines', name='Arrow', - line=Line(width=arrow_width, color=arrow_color)) - - # Data - data = Data([trace1, trace2]) - - return data - - -def Streamline(x, y, u, v, - density=1, angle=np.pi/9, - arrow_scale=.08, stream_color='rgb(114, 132, 304)', - arrow_color='rgb(114, 132, 304)', arrow_width=1, - stream_width=1, **kwargs): - """Return a data object to plot streamlines of a vector flow. - x and y are 1d arrays that define an EVENLY spaced grid. - u and v are 2d arrays (shape [y,x]) giving velocities. - - Example 1: - ``` - # 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 - - # Streamline function - data = Streamline(x, y, u, v, arrow_length = .1) - ``` - - Example 2: - # from http://nbviewer.ipython.org/github/barbagroup/AeroPython - ``` - import plotly.plotly as py - - # 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) - strength_source = 5.0 - x_source, y_source = -1.0, 0.0 - - # Compute the velocity field on the mesh grid - u_source = strength_source/(2*np.pi) * - (X-x_source)/((X-x_source)**2 + - (Y-y_source)**2) - v_source = strength_source/(2*np.pi) * - (Y-y_source)/((X-x_source)**2 + - (Y-y_source)**2) - - # Streamline function - data = Streamline(x, y, u_source, v_source) - - # Add Source Point to data - trace2 = Scatter(x=[x_source], y=[y_source], mode='markers', - marker=Marker(size=14), name='Source Point') - data = data + Data([trace2]) - - # Plot - url = py.plot(data, filename='Streamline_Source') - ``` - - Keywords arguments: - - density (kwarg, integer, default=1): - density controls the closeness (density) of the streamlines. - - angle (kwarg, angle in radians, default = np.pi/9): - Angle of arrowhead. - - arrow_scale (kwarg, float in [0,1], default = 0.08): - Value multiplied to length of barb to get length of arrowhead. - - barb_color (kwarg, color string, default = color): - Set color of barbs. - - arrow_color (kwarg, color string, default = color): - Set color of arrow. - - barb_width (kwarg, int greater than or equal to 1, default = width): - Change width of lines for barbs. - - arrow_width (kwarg, int greater than or equal to 1, default = width): - Change width of lines for arrows. """ - - # Throw exception if numpy is not imported - if _numpy_imported is False: - raise Exception("To use Streamline() please import numpy as np") - - # Throw exception if x is not an evenly spaced array - for index in range(len(x)-1): - if (x[index + 1]-x[index])-(x[1]-x[0])> .0001: - raise Exception("x must be a 1 dimmensional evenly spaced array") - - # Throw exception if y is not an evenly spaced array - for index in range(len(y)-1): - if (y[index + 1]-y[index])-(y[1]-y[0])> .0001: - raise Exception("y must be a 1 dimmensional evenly spaced array") - - # Throw exception if u and v are not the same dimmensions - if u.shape != v.shape: - raise Exception("u and v should have the same dimmensions" - "try using np.ndarrays with the same dimmensions") - - # Throw exception for invalid kwarg - VALID_KWARGS = ['density', 'angle', - 'arrow_scale', 'barb_color', - 'arrow_color', 'width', - 'barb_width', 'arrow_width'] - for key in kwargs.keys(): - if key not in VALID_KWARGS: - raise Exception("Invalid keyword argument: '{0}'".format(key)) - - # Set up some constants - size of the grid used. - NGX = len(x) - NGY = len(y) - - # Constants used to convert between grid index coords and user coords. - DX = x[1] - x[0] - DY = y[1] - y[0] - XOFF = x[0] - YOFF = y[0] - - # Now rescale velocity onto axes-coordinates - u = u/(x[-1]-x[0]) - v = v/(y[-1]-y[0]) - speed = np.sqrt(u*u + v*v) - # s (path length) will now be in axes-coordinates - # rescale u for integrations. - u *= NGX - v *= NGY - # Now u and v in grid-coordinates. - - NBX = int(30*density) - NBY = int(30*density) - blank = np.zeros((NBY, NBX)) - - bx_spacing = NGX/float(NBX-1) - by_spacing = NGY/float(NBY-1) - - def blank_pos(xi, yi): - return int((xi/bx_spacing)+0.5), int((yi/by_spacing)+0.5) - - def value_at(a, xi, yi): - if type(xi) == np.ndarray: - x = xi.astype(np.int) - y = yi.astype(np.int) - else: - x = np.int(xi) - y = np.int(yi) - a00 = a[y, x] - a01 = a[y, x+1] - a10 = a[y+1, x] - a11 = a[y+1, x+1] - xt = xi-x - yt = yi-y - a0 = a00*(1-xt) + a01*xt - a1 = a10*(1-xt) + a11*xt - return a0*(1-yt) + a1*yt - - # RK4 forward and back trajectories from the initial conditions, - # with the odd 'blank array' termination conditions. - def rk4_integrate(x0, y0): - - def f(xi, yi): - dt_ds = 1./value_at(speed, xi, yi) - ui = value_at(u, xi, yi) - vi = value_at(v, xi, yi) - return ui*dt_ds, vi*dt_ds - - def g(xi, yi): - dt_ds = 1./value_at(speed, xi, yi) - ui = value_at(u, xi, yi) - vi = value_at(v, xi, yi) - return -ui*dt_ds, -vi*dt_ds - - check = lambda xi, yi: xi>= 0 and xi < NGX-1 and yi>= 0 and yi < NGY-1 - - bx_changes = [] - by_changes = [] - - # Integrator function - def rk4(x0, y0, f): - ds = 0.01 # Min(1./NGX, 1./NGY, 0.01) - stotal = 0 - xi = x0 - yi = y0 - xb, yb = blank_pos(xi, yi) - xf_traj = [] - yf_traj = [] - while check(xi, yi): - # Time step. First save the point. - xf_traj.append(xi) - yf_traj.append(yi) - # Next, advance one using RK4 - try: - k1x, k1y = f(xi, yi) - k2x, k2y = f(xi + .5*ds*k1x, yi + .5*ds*k1y) - k3x, k3y = f(xi + .5*ds*k2x, yi + .5*ds*k2y) - k4x, k4y = f(xi + ds*k3x, yi + ds*k3y) - except IndexError: - # Out of the domain on one of the intermediate steps - break - xi += ds*(k1x+2*k2x+2*k3x+k4x) / 6. - yi += ds*(k1y+2*k2y+2*k3y+k4y) / 6. - # Final position might be out of the domain - if not check(xi, yi): - break - stotal += ds - # Next, if s gets to thres, check blank. - new_xb, new_yb = blank_pos(xi, yi) - if new_xb != xb or new_yb != yb: - # New square, so check and colour. Quit if required. - if blank[new_yb, new_xb] == 0: - blank[new_yb, new_xb] = 1 - bx_changes.append(new_xb) - by_changes.append(new_yb) - xb = new_xb - yb = new_yb - else: - break - if stotal> 2: - break - return stotal, xf_traj, yf_traj - - integrator = rk4 - - sf, xf_traj, yf_traj = integrator(x0, y0, f) - sb, xb_traj, yb_traj = integrator(x0, y0, g) - stotal = sf + sb - x_traj = xb_traj[::-1] + xf_traj[1:] - y_traj = yb_traj[::-1] + yf_traj[1:] - - # Tests to check length of traj. Remember, s in units of axes. - if len(x_traj) < 1: - return None - if stotal> .2: - initxb, inityb = blank_pos(x0, y0) - blank[inityb, initxb] = 1 - return x_traj, y_traj - else: - for xb, yb in zip(bx_changes, by_changes): - blank[yb, xb] = 0 - return None - - # A quick function for integrating trajectories if blank==0. - trajectories = [] - - def traj(xb, yb): - if xb < 0 or xb>= NBX or yb < 0 or yb>= NBY: - return - if blank[yb, xb] == 0: - t = rk4_integrate(xb*bx_spacing, yb*by_spacing) - if t is not None: - trajectories.append(t) - - # Build up the trajectory set. - for indent in range((max(NBX, NBY))//2): - for xi in range(max(NBX, NBY)-2*indent): - traj(xi+indent, indent) - traj(xi+indent, NBY-1-indent) - traj(indent, xi+indent) - traj(NBX-1-indent, xi+indent) - - xs = [np.array(t[0])*DX+XOFF for t in trajectories] - ys = [np.array(t[1])*DY+YOFF for t in trajectories] - - # Below edited to make a list readable by plotly - # and add one nan to the end of each list to plot - # multiple "streamlines" in one trace - - xspl = xs - yspl = ys - - for index in range(len(xspl)): - xspl[index] = xspl[index].tolist() - xspl[index].append(np.nan) - - for index in range(len(yspl)): - yspl[index] = yspl[index].tolist() - yspl[index].append(np.nan) - - # xs & ys are lists of lists-> combine each into 1 list - # This makes all the steamlines into 1 trace - xspl = sum(xspl, []) - yspl = sum(yspl, []) - - # ARROWS - XMid = np.empty((len(xs))) - YMid = np.empty((len(ys))) - XStart = np.empty((len(xs))) - YStart = np.empty((len(ys))) - # Find slopes between point a (mid point + 2) and - # point b (midpoint -2) - for index in range(len(xs)): - XMid[index] = xs[index][(len(xs[index])/2)+1] - XStart[index] = xs[index][(len(xs[index])/2)-1] - YMid[index] = ys[index][(len(ys[index])/2)+1] - YStart[index] = ys[index][(len(ys[index])/2)-1] - - Xdif = XMid - XStart - Ydif = YMid - YStart - - # Get angle of line - LineAng = np.arctan(Ydif/Xdif) - - # Set angles to create arrow - # Currently set to +/- 20 degrees - Ang1 = LineAng + (angle) - Ang2 = LineAng - (angle) - - XSeg1 = np.cos(Ang1)*arrow_scale - YSeg1 = np.sin(Ang1)*arrow_scale - - XSeg2 = np.cos(Ang2)*arrow_scale - YSeg2 = np.sin(Ang2)*arrow_scale - - XPoint1 = np.empty((len(Xdif))) - YPoint1 = np.empty((len(Ydif))) - - XPoint2 = np.empty((len(Xdif))) - YPoint2 = np.empty((len(Ydif))) - - for index in range(len(Xdif)): - if Xdif[index]>= 0: - XPoint1[index] = XMid[index] - XSeg1[index] - YPoint1[index] = YMid[index] - YSeg1[index] - XPoint2[index] = XMid[index] - XSeg2[index] - YPoint2[index] = YMid[index] - YSeg2[index] - else: - XPoint1[index] = XMid[index] + XSeg1[index] - YPoint1[index] = YMid[index] + YSeg1[index] - XPoint2[index] = XMid[index] + XSeg2[index] - YPoint2[index] = YMid[index] + YSeg2[index] - - nnn = np.empty((len(YMid))) - nnn[:] = np.NAN - - # Combine arrays into matrix - XArrows = np.matrix([XPoint1, XMid, XPoint2, nnn]) - # Make matrix into array readable by plotly - XArrows = np.array(XArrows) - XArrows = XArrows.flatten('F') - - # Combine arrays into matrix - YArrows = np.matrix([YPoint1, YMid, YPoint2, nnn]) - # Make matrix into array readable by plotly - YArrows = np.array(YArrows) - YArrows = YArrows.flatten('F') - - # Data - trace1 = Scatter(x=xspl, y=yspl, - mode='lines', name='Streamline', - line=Line(width=stream_width, color=stream_color)) - trace2 = Scatter(x=XArrows, y=YArrows, - mode='lines', name='Arrow', - line=Line(width=arrow_width, color=arrow_color)) - data = Data([trace1, trace2]) - - return data + def __init__(self, x, y, u, v, + scale=.1, angle=math.pi/9, + arrow_scale=.3, barb_color='rgb(114, 132, 314)', + arrow_color='rgb(114, 132, 314)', arrow_width=1, + barb_width=1, **kwargs): + self.x = x + self.y = y + self.u = u + self.v = v + self.scale = scale + self.angle = angle + self.arrow_scale = arrow_scale + self.barb_color = barb_color + self.arrow_color = arrow_color + self.arrow_width = arrow_width + self.barb_width = barb_width + self.kwargs = kwargs + + def quiver_cleanup(self): + if type(self.x) != list: + self.x = self.x.tolist() + self.x = [item for sublist in self.x for item in sublist] + + if type(self.y) != list: + self.y = self.y.tolist() + self.y = [item for sublist in self.y for item in sublist] + + if type(self.u) != list: + self.u = self.u.tolist() + self.u = [item for sublist in self.u for item in sublist] + + if type(self.v) != list: + self.v = self.v.tolist() + self.v = [item for sublist in self.v for item in sublist] + + if self.scale <= 0: + raise Exception("scale must be> 0") + + if self.arrow_scale <= 0: + raise Exception("arrow_scale must be> 0") + + if self.barb_width <= 0: + raise Exception("barb_width must be> 0") + + if self.arrow_width <= 0: + raise Exception("arrow_width must be> 0") + + if len(self.x) != len(self.y) != len(self.u) != len(self.v): + raise Exception("x, y, u, and v must be the same length") + + VALID_QUIVER_KWARGS = ('angle', 'scale', + 'arrow_scale', 'barb_color', + 'arrow_color', 'barb_width', + 'arrow_width') + for key in self.kwargs.keys(): + if key not in VALID_QUIVER_KWARGS: + raise Exception("Invalid keyword argument: '{0}'".format(key)) + + def data(self): + # Make arrays of x & y start values + start_x = self.x + start_y = self.y + + # Make arrays of x & y end values + self.u = [i * self.scale for i in self.u] + self.v = [i * self.scale for i in self.v] + + end_x = [i + j for i, j in zip(self.x, self.u)] + end_y = [i + j for i, j in zip(self.y, self.v)] + + # These are used as spaces so the separate + # arrows can be included in 1 trace + blank_space = [None] * len(start_x) + + # Zip lists: start, end, blank + barb_x = [i for sub in zip(start_x, end_x, blank_space) for i in sub] + barb_y = [i for sub in zip(start_y, end_y, blank_space) for i in sub] + + # Make Trace1: the lines + trace1 = Scatter(x=barb_x, y=barb_y, + mode='lines', name='Barb', + line=Line(width=self.barb_width, + color=self.barb_color)) + + # Arrows + + # Get barb lengths + # Default arrow length (arrow_scale) = 30% of barb length + dif_x = [i - j for i, j in zip(end_x, start_x)] + dif_y = [i - j for i, j in zip(end_y, start_y)] + + barb_len = [None] * len(start_x) + for index in range(len(barb_len)): + barb_len[index] = math.hypot(dif_x[index], dif_y[index]) + + arrow_len = [None] * len(barb_len) + arrow_len = [i * self.arrow_scale for i in barb_len] + + # Get barb angles + barb_ang = [None] * len(start_x) + for index in range(len(barb_ang)): + barb_ang[index] = math.atan2(dif_y[index], dif_x[index]) + + # Set angles to create arrow + # Default angle = +/- 20 degrees + ang1 = [i + self.angle for i in barb_ang] + ang2 = [i - self.angle for i in barb_ang] + + cos_ang1 = [None] * len(ang1) + for index in range(len(ang1)): + cos_ang1[index] = math.cos(ang1[index]) + seg1_x = [i * j for i, j in zip(arrow_len, cos_ang1)] + + sin_ang1 = [None] * len(ang1) + for index in range(len(ang1)): + sin_ang1[index] = math.sin(ang1[index]) + seg1_y = [i * j for i, j in zip(arrow_len, sin_ang1)] + + cos_ang2 = [None] * len(ang2) + for index in range(len(ang2)): + cos_ang2[index] = math.cos(ang2[index]) + seg2_x = [i * j for i, j in zip(arrow_len, cos_ang2)] + + sin_ang2 = [None] * len(ang2) + for index in range(len(ang2)): + sin_ang2[index] = math.sin(ang2[index]) + seg2_y = [i * j for i, j in zip(arrow_len, sin_ang2)] + + point1_x = [None] * len(dif_x) + point1_y = [None] * len(dif_y) + + point2_x = [None] * len(dif_x) + point2_y = [None] * len(dif_y) + + for index in range(len(dif_x)): + point1_x = [i - j for i, j in zip(end_x, seg1_x)] + point1_y = [i - j for i, j in zip(end_y, seg1_y)] + point2_x = [i - j for i, j in zip(end_x, seg2_x)] + point2_y = [i - j for i, j in zip(end_y, seg2_y)] + + # Combine arrays into matrix + arrows_x = [i for sub in zip(point1_x, end_x, point2_x, blank_space) + for i in sub] + arrows_y = [i for sub in zip(point1_y, end_y, point2_y, blank_space) + for i in sub] + + # Make trace2: the arrows + trace2 = Scatter(x=arrows_x, y=arrows_y, + mode='lines', name='Arrow', + line=Line(width=self.arrow_width, + color=self.arrow_color)) + + data = Data([trace1, trace2]) + return data + + def main(self): + return self.quiver_cleanup() + return self.data() From 427d49988afe597d78ea41fa9392bc89de170b6c Mon Sep 17 00:00:00 2001 From: Chelsea Date: Tue, 9 Jun 2015 12:54:40 -0400 Subject: [PATCH 27/34] Quiver edits @chriddyp @theengineear @etpinard @aneda Could use some feedback: I changed this quite a bit to remove the numpy dependency and make Quiver a class. --- plotly/tools.py | 244 +++++++++++++++++++++--------------------------- 1 file changed, 104 insertions(+), 140 deletions(-) diff --git a/plotly/tools.py b/plotly/tools.py index 9bf58cd38b..8b63f1de1a 100644 --- a/plotly/tools.py +++ b/plotly/tools.py @@ -1278,7 +1278,7 @@ def return_figure_from_figure_or_data(figure_or_data, validate_figure): return figure -class Quiver(object): +class Quiver(dict): """Return a data object for a quiver plot. :param (list or array) x: x coordinates of the arrow locations @@ -1286,156 +1286,130 @@ class Quiver(object): :param (list or array) u: x components of the arrow vectors :param (list or array) v: y components of the arrow vectors :param (float in [0,1]) scale: scales size of the arrows(ideally to - avoid overlap). - :param (angle in radians) angle: angle of arrowhead. + avoid overlap). Default = .1 :param (float in [0,1]) arrow_scale: value multiplied to length of barb to - get length of arrowhead. - :param barb_color (kwarg, color string, default = 'rgb(114, 132, 314)'): - Set color of barbs. - :param arrow_color (kwarg, color string, default = 'rgb(114, 132, 314)'): - Set color of arrow. - :param barb_width (kwarg, int or float greater than 0, default = 1): - Change width of lines for barbs. - :param arrow_width (kwarg, int or float greater than 0, default = 1): - Change width of lines for arrows. - - :rtype: (obj) data object - - :raises: (AttributeError) Will happen if x, y, u, and v are not all + get length of arrowhead. Default = .3 + :param (angle in radians) angle: angle of arrowhead. Default = pi/9 + + :rtype: (obj) returns quiver trace + + :raises: (PlotlyError) will happen if x, y, u, and v are not all the same length (or size if ndarray) - :raises: (AttributeError) Will happen if scale, arrow_scale, barb_width, or - arrow_width are less than or equal to 0 + :raises: (PlotlyError) will happen if scale or arrow_scale are less than or + equal to 0 Example 1: ``` - # Just 1 Arrow from (0,0) to (1,1) + # 1 Arrow from (0,0) to (1,1) data = Quiver(x=[0], y=[0], u=[1], v=[1], scale=1) ``` Example 2: ``` - x,y = np.meshgrid(np.arange(0,math.pi,.2),np.arange(0,math.pi,.2) ) + x,y = np.meshgrid(np.arange(0,2,.5),np.arange(0,2,.5)) u = np.cos(x) v = np.sin(y) - data = Quiver(x, y, u, v) + data = Quiver(x, y, u, v, scale=.2) ``` Example 3: ``` - x,y = np.meshgrid(np.arange(0,2,.2),np.arange(0,2,.2)) - u = np.cos(x)*y; - v = np.sin(x)*y; - data = Quiver(x, y, u, v) + x,y = np.meshgrid(np.arange(-np.pi,math.pi,.2), + np.arange(-math.pi,math.pi,.2)) + u = np.cos(x) + v = np.sin(x)*x + data = Quiver(x,y,u,v, + arrow_scale=.2, + angle = math.pi/6, + line=Line(color='purple', + width=1)) ``` """ def __init__(self, x, y, u, v, - scale=.1, angle=math.pi/9, - arrow_scale=.3, barb_color='rgb(114, 132, 314)', - arrow_color='rgb(114, 132, 314)', arrow_width=1, - barb_width=1, **kwargs): - self.x = x - self.y = y - self.u = u - self.v = v - self.scale = scale - self.angle = angle - self.arrow_scale = arrow_scale - self.barb_color = barb_color - self.arrow_color = arrow_color - self.arrow_width = arrow_width - self.barb_width = barb_width - self.kwargs = kwargs - - def quiver_cleanup(self): - if type(self.x) != list: - self.x = self.x.tolist() - self.x = [item for sublist in self.x for item in sublist] - - if type(self.y) != list: - self.y = self.y.tolist() - self.y = [item for sublist in self.y for item in sublist] - - if type(self.u) != list: - self.u = self.u.tolist() - self.u = [item for sublist in self.u for item in sublist] - - if type(self.v) != list: - self.v = self.v.tolist() - self.v = [item for sublist in self.v for item in sublist] - - if self.scale <= 0: - raise Exception("scale must be> 0") - - if self.arrow_scale <= 0: - raise Exception("arrow_scale must be> 0") - - if self.barb_width <= 0: - raise Exception("barb_width must be> 0") - - if self.arrow_width <= 0: - raise Exception("arrow_width must be> 0") - - if len(self.x) != len(self.y) != len(self.u) != len(self.v): - raise Exception("x, y, u, and v must be the same length") - - VALID_QUIVER_KWARGS = ('angle', 'scale', - 'arrow_scale', 'barb_color', - 'arrow_color', 'barb_width', - 'arrow_width') - for key in self.kwargs.keys(): - if key not in VALID_QUIVER_KWARGS: - raise Exception("Invalid keyword argument: '{0}'".format(key)) - - def data(self): - # Make arrays of x & y start values - start_x = self.x - start_y = self.y - - # Make arrays of x & y end values - self.u = [i * self.scale for i in self.u] - self.v = [i * self.scale for i in self.v] - - end_x = [i + j for i, j in zip(self.x, self.u)] - end_y = [i + j for i, j in zip(self.y, self.v)] - - # These are used as spaces so the separate - # arrows can be included in 1 trace - blank_space = [None] * len(start_x) - - # Zip lists: start, end, blank - barb_x = [i for sub in zip(start_x, end_x, blank_space) for i in sub] - barb_y = [i for sub in zip(start_y, end_y, blank_space) for i in sub] - - # Make Trace1: the lines - trace1 = Scatter(x=barb_x, y=barb_y, - mode='lines', name='Barb', - line=Line(width=self.barb_width, - color=self.barb_color)) - - # Arrows - - # Get barb lengths - # Default arrow length (arrow_scale) = 30% of barb length - dif_x = [i - j for i, j in zip(end_x, start_x)] - dif_y = [i - j for i, j in zip(end_y, start_y)] - - barb_len = [None] * len(start_x) + scale=.1, arrow_scale=.3, angle=math.pi/9, **kwargs): + + if type(x) != list: + x = x.tolist() + x = [item for sublist in x for item in sublist] + else: + x = x + if type(y) != list: + y = y.tolist() + y = [item for sublist in y for item in sublist] + else: + y = y + if type(u) != list: + u = u.tolist() + u = [item for sublist in u for item in sublist] + else: + u = u + if type(v) != list: + v = v.tolist() + v = [item for sublist in v for item in sublist] + else: + v = v + + scale = scale + arrow_scale = arrow_scale + angle = angle + + errors = Quiver.quiver_checks(scale, arrow_scale, x, y, u, v) + u, v = Quiver.scale_uv(u, v, scale) + end_x, end_y, barb_x, barb_y = Quiver.barb(x, y, u, v) + arrow_x, arrow_y = Quiver.arrow(x, end_x, y, end_y, arrow_scale, angle) + + super(Quiver, self).__init__(x=barb_x + arrow_x, y=barb_y + arrow_y, + mode='lines', name='quiver', **kwargs) + + @staticmethod + def quiver_checks(scale, arrow_scale, x, y, u, v): + if scale <= 0: + raise exceptions.PlotlyError("scale must be> 0") + if arrow_scale <= 0: + raise exceptions.PlotlyError("arrow_scale must be> 0") + if len(x) != len(y) or len(u) != len(v) or len(x) != len(u): + raise exceptions.PlotlyError("x, y, u, and v should all be the" + "length (or size if ndarray)") + + @staticmethod + def scale_uv(u, v, scale): + u = [i * scale for i in u] + v = [i * scale for i in v] + return u, v + + @staticmethod + def barb(x, y, u, v): + end_x = [i + j for i, j in zip(x, u)] + end_y = [i + j for i, j in zip(y, v)] + empty = [None] * len(x) + barb_x = [i for sub in zip(x, end_x, empty) for i in sub] + barb_y = [i for sub in zip(y, end_y, empty) for i in sub] + return end_x, end_y, barb_x, barb_y + + @staticmethod + def arrow(x, end_x, y, end_y, arrow_scale, angle): + + # Get barb lengths(default arrow length = 30% barb length) + dif_x = [i - j for i, j in zip(end_x, x)] + dif_y = [i - j for i, j in zip(end_y, y)] + + barb_len = [None] * len(x) for index in range(len(barb_len)): barb_len[index] = math.hypot(dif_x[index], dif_y[index]) - arrow_len = [None] * len(barb_len) - arrow_len = [i * self.arrow_scale for i in barb_len] + # Make arrow lengths + arrow_len = [None] * len(x) + arrow_len = [i * arrow_scale for i in barb_len] # Get barb angles - barb_ang = [None] * len(start_x) + barb_ang = [None] * len(x) for index in range(len(barb_ang)): barb_ang[index] = math.atan2(dif_y[index], dif_x[index]) # Set angles to create arrow - # Default angle = +/- 20 degrees - ang1 = [i + self.angle for i in barb_ang] - ang2 = [i - self.angle for i in barb_ang] + ang1 = [i + angle for i in barb_ang] + ang2 = [i - angle for i in barb_ang] cos_ang1 = [None] * len(ang1) for index in range(len(ang1)): @@ -1457,6 +1431,7 @@ def data(self): sin_ang2[index] = math.sin(ang2[index]) seg2_y = [i * j for i, j in zip(arrow_len, sin_ang2)] + # Set coordinates to create arrow point1_x = [None] * len(dif_x) point1_y = [None] * len(dif_y) @@ -1469,21 +1444,10 @@ def data(self): point2_x = [i - j for i, j in zip(end_x, seg2_x)] point2_y = [i - j for i, j in zip(end_y, seg2_y)] - # Combine arrays into matrix - arrows_x = [i for sub in zip(point1_x, end_x, point2_x, blank_space) - for i in sub] - arrows_y = [i for sub in zip(point1_y, end_y, point2_y, blank_space) - for i in sub] - - # Make trace2: the arrows - trace2 = Scatter(x=arrows_x, y=arrows_y, - mode='lines', name='Arrow', - line=Line(width=self.arrow_width, - color=self.arrow_color)) - - data = Data([trace1, trace2]) - return data - - def main(self): - return self.quiver_cleanup() - return self.data() + # Combine lists to create arrow + empty = [None] * len(x) + arrow_x = [i for sub in zip(point1_x, end_x, point2_x, empty) + for i in sub] + arrow_y = [i for sub in zip(point1_y, end_y, point2_y, empty) + for i in sub] + return arrow_x, arrow_y From 573dd6e5248174b47c2115c4f2d9925f70bcc6df Mon Sep 17 00:00:00 2001 From: Chelsea Date: Tue, 9 Jun 2015 23:10:07 -0400 Subject: [PATCH 28/34] update quiver test not sure if this is the correct way of testing PlotlyErrors --- .../tests/test_core/test_tools/test_Quiver.py | 260 +++--------------- 1 file changed, 44 insertions(+), 216 deletions(-) diff --git a/plotly/tests/test_core/test_tools/test_Quiver.py b/plotly/tests/test_core/test_tools/test_Quiver.py index c66d115596..ba0ec320e2 100644 --- a/plotly/tests/test_core/test_tools/test_Quiver.py +++ b/plotly/tests/test_core/test_tools/test_Quiver.py @@ -1,223 +1,51 @@ -import plotly -from plotly.graph_objs import * +from unittest import TestCase +from plotly.graph_objs import graph_objs, Scatter, Data, Marker, Line, Trace +from plotly.exceptions import PlotlyError + +import plotly.plotly as py import plotly.tools as tls from nose.tools import raises -import numpy as np -@raises(Exception) -def unequal_xy_length(): +@raises(PlotlyError) +def test_unequal_xy_length(): data = tls.Quiver(x=[1, 2], y=[1], u=[1, 2], v=[1, 2]) -@raises(Exception) -def unequal_uv_length(): - data = tls.Quiver(x=[1, 2], y=[1, 3], u=[1], v=[1, 2]) - - -@raises(Exception) -def test_wrong_kwarg(): - data = tls.Quiver(stuff='not gonna work') - - -def test_one_arrow(): - nan = np.nan - trace1 = Scatter( - x=[0., 1., nan], - y=[0., 1., nan], - mode='lines', - name='Barb', - line=Line(color='rgb(114, 132, 314)', width=1) - ) - trace2 = Scatter( - x=[0.82069826, 1., 0.61548617, nan], - y=[0.61548617, 1., 0.82069826, nan], - mode='lines', - name='Arrow', - line=Line(color='rgb(114, 132, 314)', width=1) - ) - expected = Data([trace1, trace2]) - - data = tls.Quiver(x=[0], y=[0], u=[1], v=[1], scale=1) - - np.testing.assert_almost_equal(data[0]['y'], expected[0]['y']) - np.testing.assert_almost_equal(data[0]['x'], expected[0]['x']) - np.testing.assert_almost_equal(data[1]['y'], expected[1]['y']) - np.testing.assert_almost_equal(data[1]['x'], expected[1]['x']) - assert data[0].keys() == expected[0].keys() - - -def test_complicated(): - nan = np.nan - trace1 = Scatter( - x=[0.0, 0.5, nan, 0.0, 0.5, nan, 0.0, 0.5, nan, 0.0, 0.5, nan, 0.0, - .5, nan, 0.0, 0.5, nan, 0.0, 0.5, nan, 0.5, 0.9387912809451864, - nan, 0.5, 0.9387912809451864, nan, 0.5, 0.9387912809451864, nan, - 0.5, 0.9387912809451864, nan, 0.5, 0.9387912809451864, nan, 0.5, - 0.9387912809451864, nan, 0.5, 0.9387912809451864, nan, 1.0, - 1.2701511529340699, nan, 1.0, 1.2701511529340699, nan, 1.0, - 1.2701511529340699, nan, 1.0, 1.2701511529340699, nan, 1.0, - 1.2701511529340699, nan, 1.0, 1.2701511529340699, nan, 1.0, - 1.2701511529340699, nan, 1.5, 1.5353686008338514, nan, 1.5, - 1.5353686008338514, nan, 1.5, 1.5353686008338514, nan, 1.5, - 1.5353686008338514, nan, 1.5, 1.5353686008338514, nan, 1.5, - 1.5353686008338514, nan, 1.5, 1.5353686008338514, nan, 2.0, - 1.7919265817264287, nan, 2.0, 1.7919265817264287, nan, 2.0, - 1.7919265817264287, nan, 2.0, 1.7919265817264287, nan, 2.0, - 1.7919265817264287, nan, 2.0, 1.7919265817264287, nan, 2.0, - 1.7919265817264287, nan, 2.5, 2.099428192226533, nan, 2.5, - 2.099428192226533, nan, 2.5, 2.099428192226533, nan, 2.5, - 2.099428192226533, nan, 2.5, 2.099428192226533, nan, 2.5, - 2.099428192226533, nan, 2.5, 2.099428192226533, nan, 3.0, - 2.5050037516997774, nan, 3.0, 2.5050037516997774, nan, 3.0, - 2.5050037516997774, nan, 3.0, 2.5050037516997774, nan, 3.0, - 2.5050037516997774, nan, 3.0, 2.5050037516997774, nan, 3.0, - 2.5050037516997774, nan], - y=[0.0, 0.0, nan, 0.5, 0.7397127693021015, nan, 1.0, - 1.4207354924039484, nan, 1.5, 1.9987474933020273, nan, 2.0, - 2.454648713412841, nan, 2.5, 2.799236072051978, nan, 3.0, - 3.0705600040299337, nan, 0.0, 0.0, nan, 0.5, 0.7397127693021015, - nan, 1.0, 1.4207354924039484, nan, 1.5, 1.9987474933020273, nan, - 2.0, 2.454648713412841, nan, 2.5, 2.799236072051978, nan, 3.0, - 3.0705600040299337, nan, 0.0, 0.0, nan, 0.5, 0.7397127693021015, - nan, 1.0, 1.4207354924039484, nan, 1.5, 1.9987474933020273, nan, - 2.0, 2.454648713412841, nan, 2.5, 2.799236072051978, nan, 3.0, - 3.0705600040299337, nan, 0.0, 0.0, nan, 0.5, 0.7397127693021015, - nan, 1.0, 1.4207354924039484, nan, 1.5, 1.9987474933020273, nan, - 2.0, 2.454648713412841, nan, 2.5, 2.799236072051978, nan, 3.0, - 3.0705600040299337, nan, 0.0, 0.0, nan, 0.5, 0.7397127693021015, - nan, 1.0, 1.4207354924039484, nan, 1.5, 1.9987474933020273, nan, - 2.0, 2.454648713412841, nan, 2.5, 2.799236072051978, nan, 3.0, - 3.0705600040299337, nan, 0.0, 0.0, nan, 0.5, 0.7397127693021015, - nan, 1.0, 1.4207354924039484, nan, 1.5, 1.9987474933020273, nan, - 2.0, 2.454648713412841, nan, 2.5, 2.799236072051978, nan, 3.0, - 3.0705600040299337, nan, 0.0, 0.0, nan, 0.5, 0.7397127693021015, - nan, 1.0, 1.4207354924039484, nan, 1.5, 1.9987474933020273, nan, - 2.0, 2.454648713412841, nan, 2.5, 2.799236072051978, nan, 3.0, - 3.0705600040299337, nan], - mode='lines', - name='Barb', - line=Line(color='rgb(114, 132, 314)', width=2) - ) - trace2 = Scatter( - x=[0.38451505843608913, 0.5, 0.38451505843608913, nan, - 0.40744858477065643, 0.5, 0.3615815321015219, nan, - 0.4247671840238289, 0.5, 0.3442629328483494, nan, - 0.43223065909116526, 0.5, 0.33679945778101306, nan, - 0.42801169097838865, 0.5, 0.3410184258937896, nan, - 0.4131432302211637, 0.5, 0.3558868866510146, nan, - 0.39126559456855653, 0.5, 0.37776452230362173, nan, - 0.8374437100677695, 0.9387912809451864, 0.8374437100677695, nan, - 0.8603772364023368, 0.9387912809451864, 0.8145101837332023, nan, - 0.8776958356555092, 0.9387912809451864, 0.7971915844800297, nan, - 0.8851593107228456, 0.9387912809451864, 0.7897281094126934, nan, - 0.880940342610069, 0.9387912809451864, 0.79394707752547, nan, - 0.8660718818528441, 0.9387912809451864, 0.8088155382826949, nan, - 0.8441942462002369, 0.9387912809451864, 0.8306931739353021, nan, - 1.2077543727140414, 1.2701511529340699, 1.2077543727140414, nan, - 1.2306878990486088, 1.2701511529340699, 1.1848208463794743, nan, - 1.2480064983017813, 1.2701511529340699, 1.1675022471263017, nan, - 1.2554699733691175, 1.2701511529340699, 1.1600387720589653, nan, - 1.251251005256341, 1.2701511529340699, 1.164257740171742, nan, - 1.236382544499116, 1.2701511529340699, 1.179126200928967, nan, - 1.214504908846509, 1.2701511529340699, 1.201003836581574, nan, - 1.5271995192328622, 1.5353686008338514, 1.5271995192328622, nan, - 1.5501330455674294, 1.5353686008338514, 1.5042659928982949, nan, - 1.567451644820602, 1.5353686008338514, 1.4869473936451223, nan, - 1.5749151198879383, 1.5353686008338514, 1.4794839185777862, nan, - 1.5706961517751616, 1.5353686008338514, 1.4837028866905626, nan, - 1.5558276910179367, 1.5353686008338514, 1.4985713474477875, nan, - 1.5339500553653296, 1.5353686008338514, 1.5204489831003947, nan, - 1.8399852748270817, 1.7919265817264287, 1.8399852748270817, nan, - 1.862918801161649, 1.7919265817264287, 1.8170517484925146, nan, - 1.8802374004148217, 1.7919265817264287, 1.799733149239342, nan, - 1.887700875482158, 1.7919265817264287, 1.7922696741720057, nan, - 1.8834819073693814, 1.7919265817264287, 1.7964886422847823, nan, - 1.8686134466121564, 1.7919265817264287, 1.8113571030420073, nan, - 1.8467358109595493, 1.7919265817264287, 1.8332347386946144, nan, - 2.1919482158522707, 2.099428192226533, 2.1919482158522707, nan, - 2.2148817421868383, 2.099428192226533, 2.1690146895177036, nan, - 2.2322003414400107, 2.099428192226533, 2.1516960902645312, nan, - 2.2396638165073472, 2.099428192226533, 2.1442326151971947, nan, - 2.2354448483945704, 2.099428192226533, 2.1484515833099715, nan, - 2.2205763876373457, 2.099428192226533, 2.1633200440671962, nan, - 2.1986987519847383, 2.099428192226533, 2.1851976797198036, nan, - 2.61933297731839, 2.5050037516997774, 2.61933297731839, nan, - 2.6422665036529573, 2.5050037516997774, 2.5963994509838226, nan, - 2.6595851029061297, 2.5050037516997774, 2.5790808517306503, nan, - 2.6670485779734663, 2.5050037516997774, 2.571617376663314, nan, - 2.6628296098606894, 2.5050037516997774, 2.5758363447760906, nan, - 2.6479611491034647, 2.5050037516997774, 2.5907048055333153, nan, - 2.6260835134508573, 2.5050037516997774, 2.6125824411859226, nan - ], - y=[-0.04783542904563622, 0.0, 0.04783542904563622, nan, - 0.6365109099465124, 0.7397127693021015, 0.7321817680377849, - nan, 1.2757228358500456, 1.4207354924039484, 1.371393693941318, - nan, 1.835716414018128, 1.9987474933020273, 1.9313872721094003, - nan, 2.3018031241660264, 2.454648713412841, 2.397473982257299, - nan, 2.682286122416868, 2.799236072051978, 2.7779569805081405, - nan, 3.006427339100005, 3.0705600040299337, 3.1020981971912773, - nan, -0.041979538370994586, 0.0, 0.041979538370994586, nan, - 0.642366800621154, 0.7397127693021015, 0.7263258773631432, nan, - 1.2815787265246874, 1.4207354924039484, 1.3655378032666765, - nan, 1.8415723046927694, 1.9987474933020273, - 1.9255313814347585, nan, 2.307659014840668, 2.454648713412841, - 2.3916180915826573, nan, 2.6881420130915097, 2.799236072051978, - 2.772101089833499, nan, 3.0122832297746465, 3.0705600040299337, - 3.096242306516636, nan, -0.02584559261554904, 0.0, - 0.02584559261554904, nan, 0.6585007463765996, - 0.7397127693021015, 0.7101919316076977, nan, - 1.2977126722801329, 1.4207354924039484, 1.349403857511231, nan, - 1.857706250448215, 1.9987474933020273, 1.909397435679313, nan, - 2.3237929605961134, 2.454648713412841, 2.375484145827212, nan, - 2.704275958846955, 2.799236072051978, 2.7559671440780535, nan, - 3.0284171755300924, 3.0705600040299337, 3.0801083607611903, - nan, -0.003383744391262258, 0.0, 0.003383744391262258, nan, - 0.6809625946008864, 0.7397127693021015, 0.6877300833834109, - nan, 1.3201745205044197, 1.4207354924039484, - 1.3269420092869442, nan, 1.8801680986725018, - 1.9987474933020273, 1.8869355874550262, nan, - 2.3462548088204005, 2.454648713412841, 2.3530222976029247, nan, - 2.7267378070712422, 2.799236072051978, 2.7335052958537664, nan, - 3.050879023754379, 3.0705600040299337, 3.0576465125369037, nan, - 0.019906562472216813, 0.0, -0.019906562472216813, nan, - 0.7042529014643655, 0.7397127693021015, 0.6644397765199318, - nan, 1.3434648273678986, 1.4207354924039484, 1.303651702423465, - nan, 1.903458405535981, 1.9987474933020273, 1.863645280591547, - nan, 2.3695451156838794, 2.454648713412841, 2.329731990739446, - nan, 2.750028113934721, 2.799236072051978, 2.7102149889902876, - nan, 3.074169330617858, 3.0705600040299337, 3.0343562056734243, - nan, 0.03832304857685983, 0.0, -0.03832304857685983, nan, - 0.7226693875690084, 0.7397127693021015, 0.6460232904152888, - nan, 1.3618813134725418, 1.4207354924039484, 1.285235216318822, - nan, 1.9218748916406239, 1.9987474933020273, - 1.8452287944869041, nan, 2.3879616017885223, 2.454648713412841, - 2.311315504634803, nan, 2.768444600039364, 2.799236072051978, - 2.6917985028856446, nan, 3.092585816722501, 3.0705600040299337, - 3.0159397195687814, nan, 0.04735671582684286, 0.0, - -0.04735671582684286, nan, 0.7317030548189914, - 0.7397127693021015, 0.6369896231653058, nan, - 1.3709149807225247, 1.4207354924039484, 1.276201549068839, nan, - 1.9309085588906068, 1.9987474933020273, 1.8361951272369212, - nan, 2.3969952690385052, 2.454648713412841, 2.30228183738482, - nan, 2.7774782672893474, 2.799236072051978, 2.6827648356356617, - nan, 3.101619483972484, 3.0705600040299337, 3.0069060523187985, - nan], - mode='lines', - name='Arrow', - line=Line(color='rgb(114, 132, 314)', width=3) - ) - expected = Data([trace1, trace2]) - - x, y = np.meshgrid(np.arange(0, np.pi, .5), np.arange(0, np.pi, .5)) - u = np.cos(x) - v = np.sin(y) - data = tls.Quiver(x, y, u, v, scale=.5, angle=np.pi/8, - arrow_scale=.25, barb_width=2, - arrow_width=3) - - np.testing.assert_almost_equal(data[0]['y'], expected[0]['y']) - np.testing.assert_almost_equal(data[0]['x'], expected[0]['x']) - np.testing.assert_almost_equal(data[1]['y'], expected[1]['y']) - np.testing.assert_almost_equal(data[1]['x'], expected[1]['x']) - assert data[0].keys() == expected[0].keys() +@raises(PlotlyError) +def test_wrong_scale(): + data = tls.Quiver(x=[1], y=[1], u=[1], v=[1], scale=0) + + +@raises(PlotlyError) +def test_wrong_arrow_scale(): + data = tls.Quiver(x=[1], y=[1], u=[1], v=[1], arrow_scale=-1) + + +class TestQuiver(TestCase): + + def test_one_arrow(self): + self.assertAlmostEqual(tls.Quiver(x=[1], y=[1], u=[1], v=[1], scale=1), + {'y': [1, 2, None, 1.615486170766527, 2, + 1.820698256761928, None], 'x': [1, 2, None, + 1.820698256761928, 2, 1.615486170766527, None], + 'name': 'quiver', 'mode': 'lines'}) + + def test_more_kwargs(self): + self.assertAlmostEqual(tls.Quiver(x=[1, 2], y=[1, 2], + u=[math.cos(1), math.cos(2)], + v=[math.sin(1), math.sin(2)], + arrow_scale=.4, angle=math.pi/6, + line=Line(color='purple', width=3)), + {'name': 'quiver', 'mode': 'lines', 'y': [1, + 1.0841470984807897, None, 2, + 2.0909297426825684, None, 1.044191642387781, + 1.0841470984807897, 1.0658037346225067, None, + 2.0677536925644366, 2.0909297426825684, + 2.051107819102551, None], 'x': [1, + 1.0540302305868139, None, 2, + 1.9583853163452858, None, 1.052143029378767, + 1.0540302305868139, 1.0184841899864512, None, + 1.9909870141679737, 1.9583853163452858, + 1.9546151170949464, None], 'line': {'color': + 'purple', 'width': 3}, 'type': 'scatter'}) From b7a6a8a50fcb91ae7450e5bb7cbd947ef293a932 Mon Sep 17 00:00:00 2001 From: Chelsea Date: 2015年6月12日 00:11:28 -0400 Subject: [PATCH 29/34] Quiver edits based on feedback added flatten function added doc for each function -still unsure about decorators? removed decorators but still using super() --- plotly/tools.py | 289 +++++++++++++++++++++++++++--------------------- 1 file changed, 165 insertions(+), 124 deletions(-) diff --git a/plotly/tools.py b/plotly/tools.py index 8b63f1de1a..1d947dd182 100644 --- a/plotly/tools.py +++ b/plotly/tools.py @@ -14,13 +14,14 @@ import warnings import six import requests +import math from plotly import utils from plotly import exceptions from plotly import session from plotly.graph_objs import graph_objs -from plotly.graph_objs import Scatter, Data, Marker, Line +from plotly.graph_objs import Scatter, Marker, Line # Warning format def warning_on_one_line(message, category, filename, lineno, file=None, line=None): @@ -1279,137 +1280,185 @@ def return_figure_from_figure_or_data(figure_or_data, validate_figure): class Quiver(dict): - """Return a data object for a quiver plot. - :param (list or array) x: x coordinates of the arrow locations - :param (list or array) y: y coordinates of the arrow locations - :param (list or array) u: x components of the arrow vectors - :param (list or array) 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 - - :rtype: (obj) returns quiver trace + def __init__(self, x, y, u, v, + scale=.1, arrow_scale=.3, angle=math.pi/9, **kwargs): + """ + Return data for a quiver plot. + + :param (list or array) x: x coordinates of the arrow locations + :param (list or array) y: y coordinates of the arrow locations + :param (list or array) u: x components of the arrow vectors + :param (list or array) 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 + + :rtype: (Quiver) returns quiver trace + + :raises: (PlotlyError) will happen if x, y, u, and v are not all + the same length (or size if ndarray) + + :raises: (PlotlyError) will happen if scale or arrow_scale are less + than or equal to 0 + + Example 1: + ``` + # 1 Arrow from (0,0) to (1,1) + q = Quiver(x=[0], y=[0], u=[1], v=[1], scale=1) + ``` + + Example 2: + ``` + x,y = np.meshgrid(np.arange(0, 2, .2), np.arange(0, 2, .2)) + u = np.cos(x)*y + v = np.sin(x)*y + q = Quiver(x, y, u, v) + ``` + + Example 3: + ``` + 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 + data = Quiver(x, y, u, v, scale=.2, arrow_scale=.3, angle=math.pi/6, + line=Line(color='purple', width=1))``` + """ - :raises: (PlotlyError) will happen if x, y, u, and v are not all - the same length (or size if ndarray) + try: + x = self.flatten(x) + except exceptions.PlotlyError: + pass - :raises: (PlotlyError) will happen if scale or arrow_scale are less than or - equal to 0 + try: + y = self.flatten(y) + except exceptions.PlotlyError: + pass - Example 1: - ``` - # 1 Arrow from (0,0) to (1,1) - data = Quiver(x=[0], y=[0], u=[1], v=[1], scale=1) - ``` + try: + u = self.flatten(u) + except exceptions.PlotlyError: + pass - Example 2: - ``` - x,y = np.meshgrid(np.arange(0,2,.5),np.arange(0,2,.5)) - u = np.cos(x) - v = np.sin(y) - data = Quiver(x, y, u, v, scale=.2) - ``` + try: + v = self.flatten(v) + except exceptions.PlotlyError: + pass - Example 3: - ``` - x,y = np.meshgrid(np.arange(-np.pi,math.pi,.2), - np.arange(-math.pi,math.pi,.2)) - u = np.cos(x) - v = np.sin(x)*x - data = Quiver(x,y,u,v, - arrow_scale=.2, - angle = math.pi/6, - line=Line(color='purple', - width=1)) - ``` - """ - def __init__(self, x, y, u, v, - scale=.1, arrow_scale=.3, angle=math.pi/9, **kwargs): + self.x = x + self.y = y + self.u = u + self.v = v + self.scale = scale + self.arrow_scale = arrow_scale + self.angle = angle + self.end_x = [] + self.end_y = [] + self.validate() + self.scale_uv() + barb_x, barb_y = self.get_barb() + arrow_x, arrow_y = self.get_quiver_arrow() - if type(x) != list: - x = x.tolist() - x = [item for sublist in x for item in sublist] - else: - x = x - if type(y) != list: - y = y.tolist() - y = [item for sublist in y for item in sublist] - else: - y = y - if type(u) != list: - u = u.tolist() - u = [item for sublist in u for item in sublist] - else: - u = u - if type(v) != list: - v = v.tolist() - v = [item for sublist in v for item in sublist] - else: - v = v + super(Quiver, self).__init__(x=barb_x + arrow_x, y=barb_y + arrow_y, + mode='lines', name='quiver', **kwargs) - scale = scale - arrow_scale = arrow_scale - angle = angle + def flatten(self, array): + """ + Uses list comprehension to flatten array - errors = Quiver.quiver_checks(scale, arrow_scale, x, y, u, v) - u, v = Quiver.scale_uv(u, v, scale) - end_x, end_y, barb_x, barb_y = Quiver.barb(x, y, u, v) - arrow_x, arrow_y = Quiver.arrow(x, end_x, y, end_y, arrow_scale, angle) + :param array: An iterable to flatten + :raises: (PlotlyError) If iterable is not nested. + :rtype: (list) The flattened list. + """ + try: + return [item for sublist in array for item in sublist] + except TypeError: + raise exceptions.PlotlyError("Your data array could not be" + "flattened! Make sure x, y, u, and v" + "are lists or numpy ndarrays!") - super(Quiver, self).__init__(x=barb_x + arrow_x, y=barb_y + arrow_y, - mode='lines', name='quiver', **kwargs) + def validate(self): + """ + Validates that args and kwargs meet criteria, + specifically that scale and arrow_scale are positive + and that x, y, u, and v are the same length - @staticmethod - def quiver_checks(scale, arrow_scale, x, y, u, v): - if scale <= 0: - raise exceptions.PlotlyError("scale must be> 0") - if arrow_scale <= 0: - raise exceptions.PlotlyError("arrow_scale must be> 0") - if len(x) != len(y) or len(u) != len(v) or len(x) != len(u): + :raises: (ValueError) If scale or arrow_scale is < 1. + :raises: (PlotlyError) If x, y, u, and v are not the same length. + """ + if self.scale <= 0: + raise ValueError("scale must be> 0") + if self.arrow_scale <= 0: + raise ValueError("arrow_scale must be> 0") + if (len(self.x) != len(self.y) or len(self.u) != len(self.v) or + len(self.x) != len(self.u)): raise exceptions.PlotlyError("x, y, u, and v should all be the" - "length (or size if ndarray)") - - @staticmethod - def scale_uv(u, v, scale): - u = [i * scale for i in u] - v = [i * scale for i in v] - return u, v - - @staticmethod - def barb(x, y, u, v): - end_x = [i + j for i, j in zip(x, u)] - end_y = [i + j for i, j in zip(y, v)] - empty = [None] * len(x) - barb_x = [i for sub in zip(x, end_x, empty) for i in sub] - barb_y = [i for sub in zip(y, end_y, empty) for i in sub] - return end_x, end_y, barb_x, barb_y - - @staticmethod - def arrow(x, end_x, y, end_y, arrow_scale, angle): + " same length (or size if ndarray)") - # Get barb lengths(default arrow length = 30% barb length) - dif_x = [i - j for i, j in zip(end_x, x)] - dif_y = [i - j for i, j in zip(end_y, y)] + def scale_uv(self): + """ + Scales u and v. u and v are added to x and y to get the + endpoints of the arros so a smaller scale value will + result in less overlap of arrows. + """ + self.u = [i * self.scale for i in self.u] + self.v = [i * self.scale for i in self.v] + + def get_barb(self): + """ + Gets endpoint of the barb and then zips startpoint and endpoint + pairs to create 2 lists: x_values for barbs and y values for barbs - barb_len = [None] * len(x) + :rtype: (list) barb_x: list of startpoint and endpoint x_value + pairs separated by a None to create the barb of the arrow. + :rtype: (list) barb_y: list of startpoint and endpoint y_value + pairs separated by a None to create the barb of the arrow. + """ + self.end_x = [i + j for i, j in zip(self.x, self.u)] + self.end_y = [i + j for i, j in zip(self.y, self.v)] + empty = [None] * len(self.x) + barb_x = self.flatten(zip(self.x, self.end_x, empty)) + barb_y = self.flatten(zip(self.y, self.end_y, empty)) + return barb_x, barb_y + + def get_quiver_arrow(self): + """ + Gets length of each barb then calculates the length of each side of + the arrow. Gets angle of barb and applies angle (kwarg) to each + side of the arrowhead. Next uses arrow_scale to scale the length of + arrowhead and creates x and y values for arrowhead point1 and point2. + Finally x and y values for point1, endpoint and point2s for each + arrowhead are separated by a None and zipped to create lists of x and + y values for the arrows. + + :rtype: (list) arrow_x: list of point1, endpoint, point2 x_values + separated by a None to create the arrowhead. + :rtype: (list) arrow_y: list of point1, endpoint, point2 y_values + separated by a None to create the barb of the arrow. + """ + dif_x = [i - j for i, j in zip(self.end_x, self.x)] + dif_y = [i - j for i, j in zip(self.end_y, self.y)] + + # Get barb lengths(default arrow length = 30% barb length) + barb_len = [None] * len(self.x) for index in range(len(barb_len)): barb_len[index] = math.hypot(dif_x[index], dif_y[index]) # Make arrow lengths - arrow_len = [None] * len(x) - arrow_len = [i * arrow_scale for i in barb_len] + arrow_len = [None] * len(self.x) + arrow_len = [i * self.arrow_scale for i in barb_len] # Get barb angles - barb_ang = [None] * len(x) + barb_ang = [None] * len(self.x) for index in range(len(barb_ang)): barb_ang[index] = math.atan2(dif_y[index], dif_x[index]) # Set angles to create arrow - ang1 = [i + angle for i in barb_ang] - ang2 = [i - angle for i in barb_ang] + ang1 = [i + self.angle for i in barb_ang] + ang2 = [i - self.angle for i in barb_ang] cos_ang1 = [None] * len(ang1) for index in range(len(ang1)): @@ -1432,22 +1481,14 @@ def arrow(x, end_x, y, end_y, arrow_scale, angle): seg2_y = [i * j for i, j in zip(arrow_len, sin_ang2)] # Set coordinates to create arrow - point1_x = [None] * len(dif_x) - point1_y = [None] * len(dif_y) - - point2_x = [None] * len(dif_x) - point2_y = [None] * len(dif_y) - - for index in range(len(dif_x)): - point1_x = [i - j for i, j in zip(end_x, seg1_x)] - point1_y = [i - j for i, j in zip(end_y, seg1_y)] - point2_x = [i - j for i, j in zip(end_x, seg2_x)] - point2_y = [i - j for i, j in zip(end_y, seg2_y)] + for index in range(len(self.end_x)): + point1_x = [i - j for i, j in zip(self.end_x, seg1_x)] + point1_y = [i - j for i, j in zip(self.end_y, seg1_y)] + point2_x = [i - j for i, j in zip(self.end_x, seg2_x)] + point2_y = [i - j for i, j in zip(self.end_y, seg2_y)] # Combine lists to create arrow - empty = [None] * len(x) - arrow_x = [i for sub in zip(point1_x, end_x, point2_x, empty) - for i in sub] - arrow_y = [i for sub in zip(point1_y, end_y, point2_y, empty) - for i in sub] + empty = [None] * len(self.end_x) + arrow_x = self.flatten(zip(point1_x, self.end_x, point2_x, empty)) + arrow_y = self.flatten(zip(point1_y, self.end_y, point2_y, empty)) return arrow_x, arrow_y From 943b7623a099b05277990d9dd61036ea9cc93f36 Mon Sep 17 00:00:00 2001 From: Chelsea Date: 2015年6月16日 03:27:10 -0400 Subject: [PATCH 30/34] Adding Streamline class and core test Adding Streamline class - Streamline class still uses bumpy dependency - this calculation can be slow with numpy, depending on the range and density. Added core tests for Quiver and Streamline --- .../tests/test_core/test_tools/test_Quiver.py | 31 +- .../test_core/test_tools/test_Streamline.py | 53 ++- plotly/tools.py | 361 +++++++++++++++++- 3 files changed, 419 insertions(+), 26 deletions(-) diff --git a/plotly/tests/test_core/test_tools/test_Quiver.py b/plotly/tests/test_core/test_tools/test_Quiver.py index ba0ec320e2..a0ae3a8748 100644 --- a/plotly/tests/test_core/test_tools/test_Quiver.py +++ b/plotly/tests/test_core/test_tools/test_Quiver.py @@ -4,6 +4,7 @@ import plotly.plotly as py import plotly.tools as tls +import math from nose.tools import raises @@ -12,12 +13,12 @@ def test_unequal_xy_length(): data = tls.Quiver(x=[1, 2], y=[1], u=[1, 2], v=[1, 2]) -@raises(PlotlyError) +@raises(ValueError) def test_wrong_scale(): data = tls.Quiver(x=[1], y=[1], u=[1], v=[1], scale=0) -@raises(PlotlyError) +@raises(ValueError) def test_wrong_arrow_scale(): data = tls.Quiver(x=[1], y=[1], u=[1], v=[1], arrow_scale=-1) @@ -37,15 +38,17 @@ def test_more_kwargs(self): v=[math.sin(1), math.sin(2)], arrow_scale=.4, angle=math.pi/6, line=Line(color='purple', width=3)), - {'name': 'quiver', 'mode': 'lines', 'y': [1, - 1.0841470984807897, None, 2, - 2.0909297426825684, None, 1.044191642387781, - 1.0841470984807897, 1.0658037346225067, None, - 2.0677536925644366, 2.0909297426825684, - 2.051107819102551, None], 'x': [1, - 1.0540302305868139, None, 2, - 1.9583853163452858, None, 1.052143029378767, - 1.0540302305868139, 1.0184841899864512, None, - 1.9909870141679737, 1.9583853163452858, - 1.9546151170949464, None], 'line': {'color': - 'purple', 'width': 3}, 'type': 'scatter'}) + {'y': [1, 1.0841470984807897, None, 2, + 2.0909297426825684, None, + 1.044191642387781, 1.0841470984807897, + 1.0658037346225067, None, + 2.0677536925644366, 2.0909297426825684, + 2.051107819102551, None], + 'x': [1, 1.0540302305868139, None, 2, + 1.9583853163452858, None, + 1.052143029378767, 1.0540302305868139, + 1.0184841899864512, None, + 1.9909870141679737, 1.9583853163452858, + 1.9546151170949464, None], + 'line': {'color': 'purple', 'width': 3}, + 'name': 'quiver', 'mode': 'lines', }) diff --git a/plotly/tests/test_core/test_tools/test_Streamline.py b/plotly/tests/test_core/test_tools/test_Streamline.py index 451db6e9b6..10fd721dc7 100644 --- a/plotly/tests/test_core/test_tools/test_Streamline.py +++ b/plotly/tests/test_core/test_tools/test_Streamline.py @@ -1,10 +1,51 @@ -import plotly -from plotly.graph_objs import * +from unittest import TestCase +from plotly.graph_objs import graph_objs, Scatter, Data, Marker, Line, Trace +from plotly.exceptions import PlotlyError + +import plotly.plotly as py import plotly.tools as tls +import math from nose.tools import raises -import numpy as np -@raises(Exception) -def test_wrong_kwarg(): - fig = tls.make_subplots(stuff='not gonna work') +@raises(ValueError) +def test_wrong_arrow_scale(): + data = tls.Streamline(x=[0, 2], y=[0, 2], + u=[[-1, -5], [-1, -5]], + v=[[1, 1], [-3, -3]], + arrow_scale=0) + + +@raises(ValueError) +def test_wrong_density(): + data = tls.Streamline(x=[0, 2], y=[0, 2], + u=[[-1, -5], [-1, -5]], + v=[[1, 1], [-3, -3]], + density=-1) + + +@raises(PlotlyError) +def test_uneven_x(): + data = tls.Streamline(x=[0, 2, 7, 9], y=[0, 2, 4, 6], + u=[[-1, -5], [-1, -5]], + v=[[1, 1], [-3, -3]]) + + +@raises(PlotlyError) +def test_uneven_y(): + data = tls.Streamline(x=[0, 2, 4], y=[0, 2, 6], + u=[[-1, -5], [-1, -5]], + v=[[1, 1], [-3, -3]]) + + +class TestStreamline(TestCase): + + def test_simple(self): + self.assertEqual((tls.Streamline(x=[0, 2], y=[0, 2], + u=[[-1, -5], [-1, -5]], + v=[[1, 1], [-3, -3]], + density=2, arrow_scale=.4, + angle=math.pi/6, + line=Line(color='purple', + width=3))).keys(), + (['y', 'x', 'line', 'name', 'mode'])) diff --git a/plotly/tools.py b/plotly/tools.py index 1d947dd182..ccc17fe3a2 100644 --- a/plotly/tools.py +++ b/plotly/tools.py @@ -1296,12 +1296,12 @@ def __init__(self, x, y, u, v, to get length of arrowhead. Default = .3 :param (angle in radians) angle: angle of arrowhead. Default = pi/9 - :rtype: (Quiver) returns quiver trace + :rtype (Quiver): returns quiver trace - :raises: (PlotlyError) will happen if x, y, u, and v are not all + :raises (PlotlyError): will happen if x, y, u, and v are not all the same length (or size if ndarray) - :raises: (PlotlyError) will happen if scale or arrow_scale are less + :raises (PlotlyError): will happen if scale or arrow_scale are less than or equal to 0 Example 1: @@ -1369,9 +1369,9 @@ def flatten(self, array): """ Uses list comprehension to flatten array - :param array: An iterable to flatten - :raises: (PlotlyError) If iterable is not nested. - :rtype: (list) The flattened list. + :param (array): An iterable to flatten + :raises (PlotlyError): If iterable is not nested. + :rtype (list): The flattened list. """ try: return [item for sublist in array for item in sublist] @@ -1492,3 +1492,352 @@ def get_quiver_arrow(self): arrow_x = self.flatten(zip(point1_x, self.end_x, point2_x, empty)) arrow_y = self.flatten(zip(point1_y, self.end_y, point2_y, empty)) return arrow_x, arrow_y + + +class Streamline(dict): + def __init__(self, x, y, u, v, + density=1, angle=math.pi/9, + arrow_scale=.09, **kwargs): + """ + Return data for a streamline plot. + + :param (list or array) x: 1 dimmensional, evenly spaced list or array + :param (list or array) y: 1 dimmensional, evenly spaced list or array + :param (array) u: 2 dimmensional array + :param (array) v: 2 dimmensional array + + :param (int) density: controls the density of streamlines in plot. + 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 + + :rtype: (dict) returns streamline data + + :raises: (ImportError) will happen if numpy is not installed + :raises: (PlotlyError) will happen if x or y are not evenly spaced + 1 dimmensional lists or arrays + :raises: (PlotlyError) will happen if u and v are not the same shape. + + Example 1: + ``` + # 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 + + # Streamline function + data = Streamline(x, y, u, v, arrow_scale = .1) + ``` + + Example 2: + # from http://nbviewer.ipython.org/github/barbagroup/AeroPython + ``` + import plotly.plotly as py + + 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) + # Streamline function + data = Streamline(x, y, u_s, v_s, density=2) + + # Add Source Point to data + trace2 = Scatter(x=[x_s], y=[y_s], mode='markers', + marker=Marker(size=14), name='Source Point') + data = [data] + [trace2] + """ + self.x = np.array(x) + self.y = np.array(y) + self.u = np.array(u) + self.v = np.array(v) + self.angle = angle + self.arrow_scale = arrow_scale + self.density = int(30*density) + self.delta_x = self.x[1] - self.x[0] + self.delta_y = self.y[1] - self.y[0] + self.val_x = self.x + self.val_y = self.y + # Set up spacing + self.blank = np.zeros((self.density, self.density)) + self.spacing_x = len(self.x)/float(self.density-1) + self.spacing_y = len(self.y)/float(self.density-1) + self.trajectories = [] + # Rescale speed onto axes-coordinates + self.u = self.u/(self.x[-1]-self.x[0]) + self.v = self.v/(self.y[-1]-self.y[0]) + self.speed = np.sqrt(self.u*self.u + self.v*self.v) + # Rescale u and v for integrations. + self.u *= len(self.x) + self.v *= len(self.y) + + self.validate() + stream_x, stream_y, total_x, total_y = self.get_streams() + arrows_x, arrows_y = self.get_streamline_arrows(stream_x, stream_y) + + super(Streamline, self).__init__(x=total_x + arrows_x, + y=total_y + arrows_y, + mode='lines', name='quiver', **kwargs) + + def validate(self): + """ + Validates that args and kwargs meet criteria, + specifically that scale and arrow_scale are positive + and that x, y, u, and v are the same length + + :raises: (ImportError) If numpy is not imported. + :raises: (ValueError) If scale or arrow_scale is < 1. + :raises: (ValueError) If density or arrow_scale is < 1. + :raises: (PlotlyError) If x and y are not evenly spaced lists or + 1D arrays. + :raises: (PlotlyError) If u and v are not the same shape. + """ + if _numpy_imported is False: + raise ImportError("To use Streamline() please import numpy as np") + if self.arrow_scale <= 0: + raise ValueError("arrow_scale must be> 0") + if self.density <= 0: + raise ValueError("density must be> 0") + for index in range(len(self.x)-1): + if (self.x[index + 1]-self.x[index])-(self.x[1]-self.x[0])> .0001: + raise exceptions.PlotlyError("x must be a 1 dimmensional" + "evenly spaced array") + for index in range(len(self.y)-1): + if (self.y[index + 1]-self.y[index])-(self.y[1]-self.y[0])> .0001: + raise exceptions.PlotlyError("y must be a 1 dimmensional" + "evenly spaced array") + if self.u.shape != self.v.shape: + raise exceptions.PlotlyError("u and v should both be 2d arrays" + "with the same dimmensions") + + def blank_pos(self, xi, yi): + """ + Set up postitions for trajectories to be used with rk4 function. + """ + return int((xi/self.spacing_x)+0.5), int((yi/self.spacing_y)+0.5) + + def value_at(self, a, xi, yi): + """ + Set up for RK4 function, taken from Bokeh's streamline code + """ + if type(xi) == np.ndarray: + self.x = xi.astype(np.int) + self.y = yi.astype(np.int) + else: + self.val_x = np.int(xi) + self.val_y = np.int(yi) + a00 = a[self.val_y, self.val_x] + a01 = a[self.val_y, self.val_x+1] + a10 = a[self.val_y+1, self.val_x] + a11 = a[self.val_y+1, self.val_x+1] + xt = xi-self.val_x + yt = yi-self.val_y + a0 = a00*(1-xt) + a01*xt + a1 = a10*(1-xt) + a11*xt + return a0*(1-yt) + a1*yt + + def rk4_integrate(self, x0, y0): + """ + RK4 forward and back trajectories from the initial conditions. + Adapted from Bokeh's streamline code + Use Runge-Kutta method to fill x and y trajectories + then checks length of traj (s in units of axes) + """ + def f(xi, yi): + dt_ds = 1./self.value_at(self.speed, xi, yi) + ui = self.value_at(self.u, xi, yi) + vi = self.value_at(self.v, xi, yi) + return ui*dt_ds, vi*dt_ds + + def g(xi, yi): + dt_ds = 1./self.value_at(self.speed, xi, yi) + ui = self.value_at(self.u, xi, yi) + vi = self.value_at(self.v, xi, yi) + return -ui*dt_ds, -vi*dt_ds + + check = lambda xi, yi: 0 <= xi < len(self.x)-1 and 0 <= yi < len(self.y)-1 + xb_changes = [] + yb_changes = [] + + def rk4(x0, y0, f): + ds = 0.01 + stotal = 0 + xi = x0 + yi = y0 + xb, yb = self.blank_pos(xi, yi) + xf_traj = [] + yf_traj = [] + while check(xi, yi): + xf_traj.append(xi) + yf_traj.append(yi) + try: + k1x, k1y = f(xi, yi) + k2x, k2y = f(xi + .5*ds*k1x, yi + .5*ds*k1y) + k3x, k3y = f(xi + .5*ds*k2x, yi + .5*ds*k2y) + k4x, k4y = f(xi + ds*k3x, yi + ds*k3y) + except IndexError: + break + xi += ds*(k1x+2*k2x+2*k3x+k4x) / 6. + yi += ds*(k1y+2*k2y+2*k3y+k4y) / 6. + if not check(xi, yi): + break + stotal += ds + new_xb, new_yb = self.blank_pos(xi, yi) + if new_xb != xb or new_yb != yb: + if self.blank[new_yb, new_xb] == 0: + self.blank[new_yb, new_xb] = 1 + xb_changes.append(new_xb) + yb_changes.append(new_yb) + xb = new_xb + yb = new_yb + else: + break + if stotal> 2: + break + return stotal, xf_traj, yf_traj + + sf, xf_traj, yf_traj = rk4(x0, y0, f) + sb, xb_traj, yb_traj = rk4(x0, y0, g) + stotal = sf + sb + x_traj = xb_traj[::-1] + xf_traj[1:] + y_traj = yb_traj[::-1] + yf_traj[1:] + + if len(x_traj) < 1: + return None + if stotal> .2: + initxb, inityb = self.blank_pos(x0, y0) + self.blank[inityb, initxb] = 1 + return x_traj, y_traj + else: + for xb, yb in zip(xb_changes, yb_changes): + self.blank[yb, xb] = 0 + return None + + def traj(self, xb, yb): + """ + Integrate trajectories, used in get_streams() + """ + if xb < 0 or xb>= self.density or yb < 0 or yb>= self.density: + return + if self.blank[yb, xb] == 0: + t = self.rk4_integrate(xb*self.spacing_x, yb*self.spacing_y) + if t is not None: + self.trajectories.append(t) + + def get_streams(self): + """ + Get streamlines by building trajectory set. + + :rtype (list of lists) stream_x: lists of x-values for each stream + :rtype (list of lists) stream_y: lists of y-values for each stream + :rtype (list) total_x: all x values for each stream combined into + single list + :rtype (list) total_y: all y values for each stream combined into + single list + """ + for indent in range((max(self.density, self.density))//2): + for xi in range(max(self.density, self.density)-2*indent): + self.traj(xi+indent, indent) + self.traj(xi+indent, self.density-1-indent) + self.traj(indent, xi+indent) + self.traj(self.density-1-indent, xi+indent) + + stream_x = [np.array(t[0])*self.delta_x+self.x[0] for t in + self.trajectories] + stream_y = [np.array(t[1])*self.delta_y+self.y[0] for t in + self.trajectories] + + for index in range(len(stream_x)): + stream_x[index] = stream_x[index].tolist() + stream_x[index].append(np.nan) + + for index in range(len(stream_y)): + stream_y[index] = stream_y[index].tolist() + stream_y[index].append(np.nan) + + total_x = sum(stream_x, []) + total_y = sum(stream_y, []) + return stream_x, stream_y, total_x, total_y + + def get_streamline_arrows(self, stream_x, stream_y): + """ + Makes an arrow for each stream. Gets angle of streamline at 1/3 mark + and creates arrow coordinates based off of user defined angle and + arrow_scale + + :param (array) stream_x: x-values for all streams + :param (array) stream_y: y-values for all streams + :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 + :rtype (list) arrows_x: x-values to create arrowhead + :rtype (list) arrows_y: y-values to create arrowhead + """ + ArrowEnd_x = np.empty((len(stream_x))) + ArrowEnd_y = np.empty((len(stream_y))) + ArrowStart_x = np.empty((len(stream_x))) + ArrowStart_y = np.empty((len(stream_y))) + for index in range(len(stream_x)): + ArrowEnd_x[index] = stream_x[index][(len(stream_x[index])/3)] + ArrowStart_x[index] = stream_x[index][(len(stream_x[index])/3)-1] + ArrowEnd_y[index] = stream_y[index][(len(stream_y[index])/3)] + ArrowStart_y[index] = stream_y[index][(len(stream_y[index])/3)-1] + + dif_x = ArrowEnd_x - ArrowStart_x + dif_y = ArrowEnd_y - ArrowStart_y + + stream_ang = np.arctan(dif_y/dif_x) + + ang1 = stream_ang + (self.angle) + ang2 = stream_ang - (self.angle) + + seg1_x = np.cos(ang1)*self.arrow_scale + seg1_y = np.sin(ang1)*self.arrow_scale + seg2_x = np.cos(ang2)*self.arrow_scale + seg2_y = np.sin(ang2)*self.arrow_scale + + point1_x = np.empty((len(dif_x))) + point1_y = np.empty((len(dif_y))) + point2_x = np.empty((len(dif_x))) + point2_y = np.empty((len(dif_y))) + + for index in range(len(dif_x)): + if dif_x[index]>= 0: + point1_x[index] = ArrowEnd_x[index] - seg1_x[index] + point1_y[index] = ArrowEnd_y[index] - seg1_y[index] + point2_x[index] = ArrowEnd_x[index] - seg2_x[index] + point2_y[index] = ArrowEnd_y[index] - seg2_y[index] + else: + point1_x[index] = ArrowEnd_x[index] + seg1_x[index] + point1_y[index] = ArrowEnd_y[index] + seg1_y[index] + point2_x[index] = ArrowEnd_x[index] + seg2_x[index] + point2_y[index] = ArrowEnd_y[index] + seg2_y[index] + + space = np.empty((len(point1_x))) + space[:] = np.NAN + + # Combine arrays into matrix + arrows_x = np.matrix([point1_x, ArrowEnd_x, point2_x, space]) + arrows_x = np.array(arrows_x) + arrows_x = arrows_x.flatten('F') + arrows_x = arrows_x.tolist() + + # Combine arrays into matrix + arrows_y = np.matrix([point1_y, ArrowEnd_y, point2_y, space]) + arrows_y = np.array(arrows_y) + arrows_y = arrows_y.flatten('F') + arrows_y = arrows_y.tolist() + + return arrows_x, arrows_y From c3c52af0491f98621160e543fc9009b5b2189b2a Mon Sep 17 00:00:00 2001 From: Chelsea Date: 2015年6月23日 12:21:27 -0400 Subject: [PATCH 31/34] TraceFactory update Create TraceFactory class with methods: create_quiver and create_streamline --- plotly/tests/test_optional/test_Streamline.py | 74 +++++ plotly/tools.py | 276 ++++++++++-------- 2 files changed, 229 insertions(+), 121 deletions(-) create mode 100644 plotly/tests/test_optional/test_Streamline.py diff --git a/plotly/tests/test_optional/test_Streamline.py b/plotly/tests/test_optional/test_Streamline.py new file mode 100644 index 0000000000..3334156240 --- /dev/null +++ b/plotly/tests/test_optional/test_Streamline.py @@ -0,0 +1,74 @@ +import plotly +from plotly.graph_objs import graph_objs, Scatter, Data, Marker, Line +import plotly.tools as tls +from nose.tools import raises +import numpy as np + + +@raises(Exception) +def test_even_x_spacing(): + data = tls.Streamline(x=[1, 2, 2.2], y=[1, 2, 3], + u=np.matrix('1 2 3; 1 2 3; 1 2 3'), + v=np.matrix('1 2 3; 1 2 3; 1 2 3')) + + +@raises(Exception) +def test_even_y_spacing(): + data = tls.Streamline(x=[1, 2, 3], y=[1, 2.5, 3], + u=np.matrix('1 2 3; 1 2 3; 1 2 3'), + v=np.matrix('1 2 3; 1 2 3; 1 2 3')) + + +@raises(Exception) +def test_uv_dimmensions(): + data = tls.Streamline(x=[1, 2, 3], y=[1, 2, 3], + u=np.matrix('1 2 3; 1 2 3; 1 2 3'), + v=np.matrix('1 2 3; 1 2 3')) + + +@raises(Exception) +def test_wrong_kwarg(): + data = tls.Streamline(x=[1, 2, 3], y=[1, 2, 3], + u=np.matrix('1 2 3; 1 2 3; 1 2 3'), + v=np.matrix('1 2 3; 1 2 3; 1 2 3'), + nope="not gonna work") + + +@raises(Exception) +def test_wrong_density(): + data = tls.Streamline(x=[1, 2, 3], y=[1, 2, 3], + u=np.matrix('1 2 3; 1 2 3; 1 2 3'), + v=np.matrix('1 2 3; 1 2 3; 1 2 3'), + density=0) + + +def test_simple_streamline(): + trace1 = Scatter( + x=[0., 1., nan], + y=[0., 1., nan], + mode='lines', + name='Barb', + line=Line(color='rgb(114, 132, 314)', width=1) + ) + trace2 = Scatter( + x=[0.82069826, 1., 0.61548617, nan], + y=[0.61548617, 1., 0.82069826, nan], + mode='lines', + name='Arrow', + line=Line(color='rgb(114, 132, 314)', width=1) + ) + expected = Data([trace1, trace2]) + + data = tls.Streamline(x=[1, 2, 3], y=[1, 2, 3], + u=np.matrix('1 2 3; 1 2 3; 1 2 3'), + v=np.matrix('1 2 3; 1 2 3; 1 2 3'), + density=0) + + np.testing.assert_almost_equal(data[0]['y'], expected[0]['y']) + np.testing.assert_almost_equal(data[0]['x'], expected[0]['x']) + np.testing.assert_almost_equal(data[1]['y'], expected[1]['y']) + np.testing.assert_almost_equal(data[1]['x'], expected[1]['x']) + assert data[0].keys() == expected[0].keys() + + +def test_complicated_streamline(): diff --git a/plotly/tools.py b/plotly/tools.py index ccc17fe3a2..0ec1505128 100644 --- a/plotly/tools.py +++ b/plotly/tools.py @@ -1279,10 +1279,10 @@ def return_figure_from_figure_or_data(figure_or_data, validate_figure): return figure -class Quiver(dict): - - def __init__(self, x, y, u, v, - scale=.1, arrow_scale=.3, angle=math.pi/9, **kwargs): +class TraceFactory(dict): + @staticmethod + def create_quiver(x, y, u, v, scale=.1, arrow_scale=.3, + angle=math.pi/9, **kwargs): """ Return data for a quiver plot. @@ -1296,7 +1296,7 @@ def __init__(self, x, y, u, v, to get length of arrowhead. Default = .3 :param (angle in radians) angle: angle of arrowhead. Default = pi/9 - :rtype (Quiver): returns quiver trace + :rtype (trace): returns quiver trace :raises (PlotlyError): will happen if x, y, u, and v are not all the same length (or size if ndarray) @@ -1307,7 +1307,9 @@ def __init__(self, x, y, u, v, Example 1: ``` # 1 Arrow from (0,0) to (1,1) - q = Quiver(x=[0], y=[0], u=[1], v=[1], scale=1) + quiver = TraceFactory.create_quiver(x=[0], y=[0], + u=[1], v=[1], + scale=1) ``` Example 2: @@ -1315,7 +1317,7 @@ def __init__(self, x, y, u, v, x,y = np.meshgrid(np.arange(0, 2, .2), np.arange(0, 2, .2)) u = np.cos(x)*y v = np.sin(x)*y - q = Quiver(x, y, u, v) + quiver = TraceFactory.create_quiver(x, y, u, v) ``` Example 3: @@ -1324,10 +1326,105 @@ def __init__(self, x, y, u, v, np.arange(-math.pi, math.pi, .5)) u = np.cos(x)*y v = np.sin(x)*y - data = Quiver(x, y, u, v, scale=.2, arrow_scale=.3, angle=math.pi/6, - line=Line(color='purple', width=1))``` + quiver = TraceFactory.create_quiver(x, y, u, v, scale=.2, + arrow_scale=.3, angle=math.pi/6, + line=Line(color='purple', width=1)) + ``` + """ + 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 = Scatter(x=barb_x + arrow_x, + y=barb_y + arrow_y, + mode='lines', **kwargs) + return quiver + + @staticmethod + def create_streamline(x, y, u, v, + density=1, angle=math.pi/9, + arrow_scale=.09, **kwargs): + """ + Return data for a streamline plot. + + :param (list or array) x: 1 dimmensional, evenly spaced list or array + :param (list or array) y: 1 dimmensional, evenly spaced list or array + :param (array) u: 2 dimmensional array + :param (array) v: 2 dimmensional array + + :param (int) density: controls the density of streamlines in plot. + 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 + + :rtype: (trace) returns streamline data + + :raises: (ImportError) will happen if numpy is not installed + :raises: (PlotlyError) will happen if x or y are not evenly spaced + 1 dimmensional lists or arrays + :raises: (PlotlyError) will happen if u and v are not the same shape. + + Example 1: + ``` + # 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 + stream = TraceFactory.create_streamline(x, y, u, v, arrow_scale = .1) + ``` + + Example 2: + # from http://nbviewer.ipython.org/github/barbagroup/AeroPython + ``` + 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 + stream = TraceFactory.create_streamline(x, y, u_s, v_s, density=2, + name='stream') + + # Add source point + point = Scatter(x=[x_s], y=[y_s], mode='markers', + marker=Marker(size=14), name='source point') + fig=Figure() + fig['data'].append(stream) + fig['data'].append(point) + py.iplot(fig, filename='stream', overwrite=True) + ``` """ + streams_x, streams_y = Streamline(x, y, u, v, + density, angle, + arrow_scale).get_total_streams() + arrow_x, arrow_y = Streamline(x, y, u, v, + density, angle, + arrow_scale).get_streamline_arrows() + + stream = Scatter(x=streams_x + arrow_x, + y=streams_y + arrow_y, + mode='lines', **kwargs) + return stream + +class Quiver(TraceFactory): + def __init__(self, x, y, u, v, + scale, arrow_scale, angle, **kwargs): try: x = self.flatten(x) except exceptions.PlotlyError: @@ -1359,11 +1456,8 @@ def __init__(self, x, y, u, v, self.end_y = [] self.validate() self.scale_uv() - barb_x, barb_y = self.get_barb() - arrow_x, arrow_y = self.get_quiver_arrow() - - super(Quiver, self).__init__(x=barb_x + arrow_x, y=barb_y + arrow_y, - mode='lines', name='quiver', **kwargs) + barb_x, barb_y = self.get_barbs() + arrow_x, arrow_y = self.get_quiver_arrows() def flatten(self, array): """ @@ -1407,7 +1501,7 @@ def scale_uv(self): self.u = [i * self.scale for i in self.u] self.v = [i * self.scale for i in self.v] - def get_barb(self): + def get_barbs(self): """ Gets endpoint of the barb and then zips startpoint and endpoint pairs to create 2 lists: x_values for barbs and y values for barbs @@ -1424,7 +1518,7 @@ def get_barb(self): barb_y = self.flatten(zip(self.y, self.end_y, empty)) return barb_x, barb_y - def get_quiver_arrow(self): + def get_quiver_arrows(self): """ Gets length of each barb then calculates the length of each side of the arrow. Gets angle of barb and applies angle (kwarg) to each @@ -1494,71 +1588,10 @@ def get_quiver_arrow(self): return arrow_x, arrow_y -class Streamline(dict): +class Streamline(TraceFactory): def __init__(self, x, y, u, v, - density=1, angle=math.pi/9, - arrow_scale=.09, **kwargs): - """ - Return data for a streamline plot. - - :param (list or array) x: 1 dimmensional, evenly spaced list or array - :param (list or array) y: 1 dimmensional, evenly spaced list or array - :param (array) u: 2 dimmensional array - :param (array) v: 2 dimmensional array - - :param (int) density: controls the density of streamlines in plot. - 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 - - :rtype: (dict) returns streamline data - - :raises: (ImportError) will happen if numpy is not installed - :raises: (PlotlyError) will happen if x or y are not evenly spaced - 1 dimmensional lists or arrays - :raises: (PlotlyError) will happen if u and v are not the same shape. - - Example 1: - ``` - # 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 - - # Streamline function - data = Streamline(x, y, u, v, arrow_scale = .1) - ``` - - Example 2: - # from http://nbviewer.ipython.org/github/barbagroup/AeroPython - ``` - import plotly.plotly as py - - 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) - # Streamline function - data = Streamline(x, y, u_s, v_s, density=2) - - # Add Source Point to data - trace2 = Scatter(x=[x_s], y=[y_s], mode='markers', - marker=Marker(size=14), name='Source Point') - data = [data] + [trace2] - """ + density, angle, + arrow_scale, **kwargs): self.x = np.array(x) self.y = np.array(y) self.u = np.array(u) @@ -1582,14 +1615,12 @@ def __init__(self, x, y, u, v, # Rescale u and v for integrations. self.u *= len(self.x) self.v *= len(self.y) - self.validate() - stream_x, stream_y, total_x, total_y = self.get_streams() - arrows_x, arrows_y = self.get_streamline_arrows(stream_x, stream_y) - - super(Streamline, self).__init__(x=total_x + arrows_x, - y=total_y + arrows_y, - mode='lines', name='quiver', **kwargs) + self.st_x = [] + self.st_y = [] + self.get_stream() + streams_x, streams_y = self.get_total_streams() + arrows_x, arrows_y = self.get_streamline_arrows() def validate(self): """ @@ -1727,7 +1758,7 @@ def rk4(x0, y0, f): def traj(self, xb, yb): """ - Integrate trajectories, used in get_streams() + Integrate trajectories, used in get_stream() """ if xb < 0 or xb>= self.density or yb < 0 or yb>= self.density: return @@ -1736,16 +1767,12 @@ def traj(self, xb, yb): if t is not None: self.trajectories.append(t) - def get_streams(self): + def get_stream(self): """ Get streamlines by building trajectory set. - :rtype (list of lists) stream_x: lists of x-values for each stream - :rtype (list of lists) stream_y: lists of y-values for each stream - :rtype (list) total_x: all x values for each stream combined into - single list - :rtype (list) total_y: all y values for each stream combined into - single list + :rtype (list of lists) st_x: lists of x-values for each stream + :rtype (list of lists) st_y: lists of y-values for each stream """ for indent in range((max(self.density, self.density))//2): for xi in range(max(self.density, self.density)-2*indent): @@ -1754,46 +1781,42 @@ def get_streams(self): self.traj(indent, xi+indent) self.traj(self.density-1-indent, xi+indent) - stream_x = [np.array(t[0])*self.delta_x+self.x[0] for t in - self.trajectories] - stream_y = [np.array(t[1])*self.delta_y+self.y[0] for t in - self.trajectories] - - for index in range(len(stream_x)): - stream_x[index] = stream_x[index].tolist() - stream_x[index].append(np.nan) + self.st_x = [np.array(t[0])*self.delta_x+self.x[0] for t in + self.trajectories] + self.st_y = [np.array(t[1])*self.delta_y+self.y[0] for t in + self.trajectories] - for index in range(len(stream_y)): - stream_y[index] = stream_y[index].tolist() - stream_y[index].append(np.nan) + for index in range(len(self.st_x)): + self.st_x[index] = self.st_x[index].tolist() + self.st_x[index].append(np.nan) - total_x = sum(stream_x, []) - total_y = sum(stream_y, []) - return stream_x, stream_y, total_x, total_y + for index in range(len(self.st_y)): + self.st_y[index] = self.st_y[index].tolist() + self.st_y[index].append(np.nan) - def get_streamline_arrows(self, stream_x, stream_y): + def get_streamline_arrows(self): """ Makes an arrow for each stream. Gets angle of streamline at 1/3 mark and creates arrow coordinates based off of user defined angle and arrow_scale - :param (array) stream_x: x-values for all streams - :param (array) stream_y: y-values for all streams + :param (array) st_x: x-values for all streams + :param (array) st_y: y-values for all streams :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 :rtype (list) arrows_x: x-values to create arrowhead :rtype (list) arrows_y: y-values to create arrowhead """ - ArrowEnd_x = np.empty((len(stream_x))) - ArrowEnd_y = np.empty((len(stream_y))) - ArrowStart_x = np.empty((len(stream_x))) - ArrowStart_y = np.empty((len(stream_y))) - for index in range(len(stream_x)): - ArrowEnd_x[index] = stream_x[index][(len(stream_x[index])/3)] - ArrowStart_x[index] = stream_x[index][(len(stream_x[index])/3)-1] - ArrowEnd_y[index] = stream_y[index][(len(stream_y[index])/3)] - ArrowStart_y[index] = stream_y[index][(len(stream_y[index])/3)-1] + ArrowEnd_x = np.empty((len(self.st_x))) + ArrowEnd_y = np.empty((len(self.st_y))) + ArrowStart_x = np.empty((len(self.st_x))) + ArrowStart_y = np.empty((len(self.st_y))) + for index in range(len(self.st_x)): + ArrowEnd_x[index] = self.st_x[index][(len(self.st_x[index])/3)] + ArrowStart_x[index] = self.st_x[index][(len(self.st_x[index])/3)-1] + ArrowEnd_y[index] = self.st_y[index][(len(self.st_y[index])/3)] + ArrowStart_y[index] = self.st_y[index][(len(self.st_y[index])/3)-1] dif_x = ArrowEnd_x - ArrowStart_x dif_y = ArrowEnd_y - ArrowStart_y @@ -1841,3 +1864,14 @@ def get_streamline_arrows(self, stream_x, stream_y): arrows_y = arrows_y.tolist() return arrows_x, arrows_y + + def get_total_streams(self): + """ + :rtype (list) streams_x: all x values for each stream combined into + single list + :rtype (list) streams_y: all y values for each stream combined into + single list + """ + streams_x = sum(self.st_x, []) + streams_y = sum(self.st_y, []) + return streams_x, streams_y From 136a2497dfcbec2e1704b758a03ac4be6919a8da Mon Sep 17 00:00:00 2001 From: Chelsea Date: 2015年6月23日 13:06:28 -0400 Subject: [PATCH 32/34] TraceFactory tests update tests for quiver/streamline with TraceFactory --- .../tests/test_core/test_tools/test_Quiver.py | 54 --------- .../test_core/test_tools/test_Streamline.py | 51 -------- .../test_core/test_tools/test_TraceFactory.py | 112 ++++++++++++++++++ plotly/tests/test_optional/test_Streamline.py | 74 ------------ 4 files changed, 112 insertions(+), 179 deletions(-) delete mode 100644 plotly/tests/test_core/test_tools/test_Quiver.py delete mode 100644 plotly/tests/test_core/test_tools/test_Streamline.py create mode 100644 plotly/tests/test_core/test_tools/test_TraceFactory.py delete mode 100644 plotly/tests/test_optional/test_Streamline.py diff --git a/plotly/tests/test_core/test_tools/test_Quiver.py b/plotly/tests/test_core/test_tools/test_Quiver.py deleted file mode 100644 index a0ae3a8748..0000000000 --- a/plotly/tests/test_core/test_tools/test_Quiver.py +++ /dev/null @@ -1,54 +0,0 @@ -from unittest import TestCase -from plotly.graph_objs import graph_objs, Scatter, Data, Marker, Line, Trace -from plotly.exceptions import PlotlyError - -import plotly.plotly as py -import plotly.tools as tls -import math -from nose.tools import raises - - -@raises(PlotlyError) -def test_unequal_xy_length(): - data = tls.Quiver(x=[1, 2], y=[1], u=[1, 2], v=[1, 2]) - - -@raises(ValueError) -def test_wrong_scale(): - data = tls.Quiver(x=[1], y=[1], u=[1], v=[1], scale=0) - - -@raises(ValueError) -def test_wrong_arrow_scale(): - data = tls.Quiver(x=[1], y=[1], u=[1], v=[1], arrow_scale=-1) - - -class TestQuiver(TestCase): - - def test_one_arrow(self): - self.assertAlmostEqual(tls.Quiver(x=[1], y=[1], u=[1], v=[1], scale=1), - {'y': [1, 2, None, 1.615486170766527, 2, - 1.820698256761928, None], 'x': [1, 2, None, - 1.820698256761928, 2, 1.615486170766527, None], - 'name': 'quiver', 'mode': 'lines'}) - - def test_more_kwargs(self): - self.assertAlmostEqual(tls.Quiver(x=[1, 2], y=[1, 2], - u=[math.cos(1), math.cos(2)], - v=[math.sin(1), math.sin(2)], - arrow_scale=.4, angle=math.pi/6, - line=Line(color='purple', width=3)), - {'y': [1, 1.0841470984807897, None, 2, - 2.0909297426825684, None, - 1.044191642387781, 1.0841470984807897, - 1.0658037346225067, None, - 2.0677536925644366, 2.0909297426825684, - 2.051107819102551, None], - 'x': [1, 1.0540302305868139, None, 2, - 1.9583853163452858, None, - 1.052143029378767, 1.0540302305868139, - 1.0184841899864512, None, - 1.9909870141679737, 1.9583853163452858, - 1.9546151170949464, None], - 'line': {'color': 'purple', 'width': 3}, - 'name': 'quiver', 'mode': 'lines', }) diff --git a/plotly/tests/test_core/test_tools/test_Streamline.py b/plotly/tests/test_core/test_tools/test_Streamline.py deleted file mode 100644 index 10fd721dc7..0000000000 --- a/plotly/tests/test_core/test_tools/test_Streamline.py +++ /dev/null @@ -1,51 +0,0 @@ -from unittest import TestCase -from plotly.graph_objs import graph_objs, Scatter, Data, Marker, Line, Trace -from plotly.exceptions import PlotlyError - -import plotly.plotly as py -import plotly.tools as tls -import math -from nose.tools import raises - - -@raises(ValueError) -def test_wrong_arrow_scale(): - data = tls.Streamline(x=[0, 2], y=[0, 2], - u=[[-1, -5], [-1, -5]], - v=[[1, 1], [-3, -3]], - arrow_scale=0) - - -@raises(ValueError) -def test_wrong_density(): - data = tls.Streamline(x=[0, 2], y=[0, 2], - u=[[-1, -5], [-1, -5]], - v=[[1, 1], [-3, -3]], - density=-1) - - -@raises(PlotlyError) -def test_uneven_x(): - data = tls.Streamline(x=[0, 2, 7, 9], y=[0, 2, 4, 6], - u=[[-1, -5], [-1, -5]], - v=[[1, 1], [-3, -3]]) - - -@raises(PlotlyError) -def test_uneven_y(): - data = tls.Streamline(x=[0, 2, 4], y=[0, 2, 6], - u=[[-1, -5], [-1, -5]], - v=[[1, 1], [-3, -3]]) - - -class TestStreamline(TestCase): - - def test_simple(self): - self.assertEqual((tls.Streamline(x=[0, 2], y=[0, 2], - u=[[-1, -5], [-1, -5]], - v=[[1, 1], [-3, -3]], - density=2, arrow_scale=.4, - angle=math.pi/6, - line=Line(color='purple', - width=3))).keys(), - (['y', 'x', 'line', 'name', 'mode'])) diff --git a/plotly/tests/test_core/test_tools/test_TraceFactory.py b/plotly/tests/test_core/test_tools/test_TraceFactory.py new file mode 100644 index 0000000000..d6a2afa838 --- /dev/null +++ b/plotly/tests/test_core/test_tools/test_TraceFactory.py @@ -0,0 +1,112 @@ +from unittest import TestCase +from plotly.graph_objs import graph_objs, Scatter, Data, Marker, Line, Trace +from plotly.exceptions import PlotlyError + +import plotly.plotly as py +import plotly.tools as tls +import math +from nose.tools import raises + + +@raises(PlotlyError) +def test_unequal_xy_length(): + data = tls.TraceFactory.create_quiver(x=[1, 2], y=[1], u=[1, 2], v=[1, 2]) + + +@raises(ValueError) +def test_wrong_scale(): + data = tls.TraceFactory.create_quiver(x=[1], y=[1], u=[1], v=[1], + scale=0) + + +@raises(ValueError) +def test_wrong_arrow_scale(): + data = tls.TraceFactory.create_quiver(x=[1], y=[1], u=[1], v=[1], + arrow_scale=-1) + + +class TestQuiver(TestCase): + + def test_one_arrow(self): + self.assertAlmostEqual(tls.TraceFactory.create_quiver(x=[1], y=[1], + u=[1], v=[1], + scale=1), + {'y': [1, 2, None, 1.615486170766527, 2, + 1.820698256761928, None], 'x': [1, 2, None, + 1.820698256761928, 2, 1.615486170766527, None], + 'type': 'scatter', 'mode': 'lines'}) + + def test_more_kwargs(self): + self.assertAlmostEqual(tls.TraceFactory.create_quiver(x=[1, 2], + y=[1, 2], + u=[math.cos(1), + math.cos(2)], + v=[math.sin(1), + math.sin(2)], + arrow_scale=.4, + angle=math.pi/6, + line=Line( + color='purple', + width=3)), + {'y': [1, 1.0841470984807897, None, 2, + 2.0909297426825684, None, + 1.044191642387781, 1.0841470984807897, + 1.0658037346225067, None, + 2.0677536925644366, 2.0909297426825684, + 2.051107819102551, None], + 'x': [1, 1.0540302305868139, None, 2, + 1.9583853163452858, None, + 1.052143029378767, 1.0540302305868139, + 1.0184841899864512, None, + 1.9909870141679737, 1.9583853163452858, + 1.9546151170949464, None], + 'line': {'color': 'purple', 'width': 3}, + 'type': 'scatter', 'mode': 'lines', }) + + +@raises(ValueError) +def test_wrong_arrow_scale(): + stream = tls.TraceFactory.create_streamline(x=[0, 2], y=[0, 2], + u=[[-1, -5], [-1, -5]], + v=[[1, 1], [-3, -3]], + arrow_scale=0) + + +@raises(ValueError) +def test_wrong_density(): + stream = tls.TraceFactory.create_streamline(x=[0, 2], y=[0, 2], + u=[[-1, -5], [-1, -5]], + v=[[1, 1], [-3, -3]], + density=-1) + + +@raises(PlotlyError) +def test_uneven_x(): + stream = tls.TraceFactory.create_streamline(x=[0, 2, 7, 9], y=[0, 2, 4, 6], + u=[[-1, -5], [-1, -5]], + v=[[1, 1], [-3, -3]]) + + +@raises(PlotlyError) +def test_uneven_y(): + stream = tls.TraceFactory.create_streamline(x=[0, 2, 4], y=[0, 2, 6], + u=[[-1, -5], [-1, -5]], + v=[[1, 1], [-3, -3]]) + + +class TestStreamline(TestCase): + + def test_simple(self): + self.assertEqual((tls.TraceFactory.create_streamline(x=[0, 2], + y=[0, 2], + u=[[-1, -5], + [-1, -5]], + v=[[1, 1], + [-3, -3]], + density=2, + arrow_scale=.4, + angle=math.pi/6, + line=Line( + color='purple', + width=3))).keys(), + (['y', 'x', 'line', 'type', 'mode'])) diff --git a/plotly/tests/test_optional/test_Streamline.py b/plotly/tests/test_optional/test_Streamline.py deleted file mode 100644 index 3334156240..0000000000 --- a/plotly/tests/test_optional/test_Streamline.py +++ /dev/null @@ -1,74 +0,0 @@ -import plotly -from plotly.graph_objs import graph_objs, Scatter, Data, Marker, Line -import plotly.tools as tls -from nose.tools import raises -import numpy as np - - -@raises(Exception) -def test_even_x_spacing(): - data = tls.Streamline(x=[1, 2, 2.2], y=[1, 2, 3], - u=np.matrix('1 2 3; 1 2 3; 1 2 3'), - v=np.matrix('1 2 3; 1 2 3; 1 2 3')) - - -@raises(Exception) -def test_even_y_spacing(): - data = tls.Streamline(x=[1, 2, 3], y=[1, 2.5, 3], - u=np.matrix('1 2 3; 1 2 3; 1 2 3'), - v=np.matrix('1 2 3; 1 2 3; 1 2 3')) - - -@raises(Exception) -def test_uv_dimmensions(): - data = tls.Streamline(x=[1, 2, 3], y=[1, 2, 3], - u=np.matrix('1 2 3; 1 2 3; 1 2 3'), - v=np.matrix('1 2 3; 1 2 3')) - - -@raises(Exception) -def test_wrong_kwarg(): - data = tls.Streamline(x=[1, 2, 3], y=[1, 2, 3], - u=np.matrix('1 2 3; 1 2 3; 1 2 3'), - v=np.matrix('1 2 3; 1 2 3; 1 2 3'), - nope="not gonna work") - - -@raises(Exception) -def test_wrong_density(): - data = tls.Streamline(x=[1, 2, 3], y=[1, 2, 3], - u=np.matrix('1 2 3; 1 2 3; 1 2 3'), - v=np.matrix('1 2 3; 1 2 3; 1 2 3'), - density=0) - - -def test_simple_streamline(): - trace1 = Scatter( - x=[0., 1., nan], - y=[0., 1., nan], - mode='lines', - name='Barb', - line=Line(color='rgb(114, 132, 314)', width=1) - ) - trace2 = Scatter( - x=[0.82069826, 1., 0.61548617, nan], - y=[0.61548617, 1., 0.82069826, nan], - mode='lines', - name='Arrow', - line=Line(color='rgb(114, 132, 314)', width=1) - ) - expected = Data([trace1, trace2]) - - data = tls.Streamline(x=[1, 2, 3], y=[1, 2, 3], - u=np.matrix('1 2 3; 1 2 3; 1 2 3'), - v=np.matrix('1 2 3; 1 2 3; 1 2 3'), - density=0) - - np.testing.assert_almost_equal(data[0]['y'], expected[0]['y']) - np.testing.assert_almost_equal(data[0]['x'], expected[0]['x']) - np.testing.assert_almost_equal(data[1]['y'], expected[1]['y']) - np.testing.assert_almost_equal(data[1]['x'], expected[1]['x']) - assert data[0].keys() == expected[0].keys() - - -def test_complicated_streamline(): From 50d7dd111843b59076aeaa7fede216435e67dfb1 Mon Sep 17 00:00:00 2001 From: Chelsea Date: 2015年6月25日 12:41:29 -0400 Subject: [PATCH 33/34] Update TraceFactory and examples updated re: comment 222 @theengineear is this what you had in mind for TraceFactory? cc @chriddyp @aneda @etpinard --- plotly/tools.py | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/plotly/tools.py b/plotly/tools.py index 0ec1505128..e2450f1c61 100644 --- a/plotly/tools.py +++ b/plotly/tools.py @@ -1310,25 +1310,44 @@ def create_quiver(x, y, u, v, scale=.1, arrow_scale=.3, quiver = TraceFactory.create_quiver(x=[0], y=[0], u=[1], v=[1], scale=1) + # Plot + fig=Figure() + fig['data'].append(quiver) + py.iplot(fig, filename='quiver') ``` Example 2: ``` + # 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 quiver = TraceFactory.create_quiver(x, y, u, v) + + # Plot + fig=Figure() + fig['data'].append(quiver) + py.iplot(fig, filename='quiver') ``` Example 3: ``` + # 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 quiver = TraceFactory.create_quiver(x, y, u, v, scale=.2, arrow_scale=.3, angle=math.pi/6, line=Line(color='purple', width=1)) + # Plot + fig=Figure() + fig['data'].append(quiver) + py.iplot(fig, filename='quiver') ``` """ barb_x, barb_y = Quiver(x, y, u, v, scale, @@ -1376,13 +1395,19 @@ def create_streamline(x, y, u, v, u = u.T #transpose v = v.T #transpose - # create streamline + # Create streamline stream = TraceFactory.create_streamline(x, y, u, v, arrow_scale = .1) + + # Plot + fig=Figure() + fig['data'].append(stream) + py.iplot(fig, filename='stream') ``` Example 2: # from http://nbviewer.ipython.org/github/barbagroup/AeroPython ``` + # Add data N = 50 x_start, x_end = -2.0, 2.0 y_start, y_end = -1.0, 1.0 @@ -1403,10 +1428,11 @@ def create_streamline(x, y, u, v, # Add source point point = Scatter(x=[x_s], y=[y_s], mode='markers', marker=Marker(size=14), name='source point') + # Plot fig=Figure() fig['data'].append(stream) fig['data'].append(point) - py.iplot(fig, filename='stream', overwrite=True) + py.iplot(fig, filename='stream') ``` """ streams_x, streams_y = Streamline(x, y, u, v, From 3addb46795af5bf938b661305d3891727c3985b7 Mon Sep 17 00:00:00 2001 From: Chelsea Date: 2015年6月26日 11:45:14 -0400 Subject: [PATCH 34/34] renaming streamline MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Changing ‘stream’ to ‘streamline’ to avoid user confusion --- plotly/tools.py | 75 +++++++++++++++++++++++++------------------------ 1 file changed, 38 insertions(+), 37 deletions(-) diff --git a/plotly/tools.py b/plotly/tools.py index e2450f1c61..64b9dc8f5e 100644 --- a/plotly/tools.py +++ b/plotly/tools.py @@ -1396,12 +1396,12 @@ def create_streamline(x, y, u, v, v = v.T #transpose # Create streamline - stream = TraceFactory.create_streamline(x, y, u, v, arrow_scale = .1) + streamline = TraceFactory.create_streamline(x, y, u, v, arrow_scale= 1) # Plot fig=Figure() - fig['data'].append(stream) - py.iplot(fig, filename='stream') + fig['data'].append(streamline) + py.iplot(fig, filename='streamline') ``` Example 2: @@ -1422,30 +1422,30 @@ def create_streamline(x, y, u, v, v_s = ss/(2*np.pi) * (Y-y_s)/((X-x_s)**2 + (Y-y_s)**2) # Create streamline - stream = TraceFactory.create_streamline(x, y, u_s, v_s, density=2, - name='stream') + streamline = TraceFactory.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=Figure() - fig['data'].append(stream) + fig['data'].append(streamline) fig['data'].append(point) - py.iplot(fig, filename='stream') + py.iplot(fig, filename='streamline') ``` """ - streams_x, streams_y = Streamline(x, y, u, v, - density, angle, - arrow_scale).get_total_streams() + 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() - stream = Scatter(x=streams_x + arrow_x, - y=streams_y + arrow_y, - mode='lines', **kwargs) - return stream + streamline = Scatter(x=streamline_x + arrow_x, + y=streamline_y + arrow_y, + mode='lines', **kwargs) + return streamline class Quiver(TraceFactory): @@ -1644,8 +1644,8 @@ def __init__(self, x, y, u, v, self.validate() self.st_x = [] self.st_y = [] - self.get_stream() - streams_x, streams_y = self.get_total_streams() + self.get_streamlines() + streamline_x, streamline_y = self.sum_streamlines() arrows_x, arrows_y = self.get_streamline_arrows() def validate(self): @@ -1662,7 +1662,8 @@ def validate(self): :raises: (PlotlyError) If u and v are not the same shape. """ if _numpy_imported is False: - raise ImportError("To use Streamline() please import numpy as np") + raise ImportError("To use TraceFactory.create_streamline()" + " please import numpy as np") if self.arrow_scale <= 0: raise ValueError("arrow_scale must be> 0") if self.density <= 0: @@ -1687,7 +1688,7 @@ def blank_pos(self, xi, yi): def value_at(self, a, xi, yi): """ - Set up for RK4 function, taken from Bokeh's streamline code + Set up for RK4 function, based on Bokeh's streamline code """ if type(xi) == np.ndarray: self.x = xi.astype(np.int) @@ -1784,7 +1785,7 @@ def rk4(x0, y0, f): def traj(self, xb, yb): """ - Integrate trajectories, used in get_stream() + Integrate trajectories, used in get_streamlines() """ if xb < 0 or xb>= self.density or yb < 0 or yb>= self.density: return @@ -1793,12 +1794,12 @@ def traj(self, xb, yb): if t is not None: self.trajectories.append(t) - def get_stream(self): + def get_streamlines(self): """ Get streamlines by building trajectory set. - :rtype (list of lists) st_x: lists of x-values for each stream - :rtype (list of lists) st_y: lists of y-values for each stream + :rtype (list of lists) st_x: lists of x-values for each streamline + :rtype (list of lists) st_y: lists of y-values for each streamline """ for indent in range((max(self.density, self.density))//2): for xi in range(max(self.density, self.density)-2*indent): @@ -1822,12 +1823,12 @@ def get_stream(self): def get_streamline_arrows(self): """ - Makes an arrow for each stream. Gets angle of streamline at 1/3 mark - and creates arrow coordinates based off of user defined angle and + Makes an arrow for each streamline. Gets angle of streamline at 1/3 + mark and creates arrow coordinates based off of user defined angle and arrow_scale - :param (array) st_x: x-values for all streams - :param (array) st_y: y-values for all streams + :param (array) st_x: x-values for all streamlines + :param (array) st_y: y-values for all streamlines :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 @@ -1847,10 +1848,10 @@ def get_streamline_arrows(self): dif_x = ArrowEnd_x - ArrowStart_x dif_y = ArrowEnd_y - ArrowStart_y - stream_ang = np.arctan(dif_y/dif_x) + streamline_ang = np.arctan(dif_y/dif_x) - ang1 = stream_ang + (self.angle) - ang2 = stream_ang - (self.angle) + ang1 = streamline_ang + (self.angle) + ang2 = streamline_ang - (self.angle) seg1_x = np.cos(ang1)*self.arrow_scale seg1_y = np.sin(ang1)*self.arrow_scale @@ -1891,13 +1892,13 @@ def get_streamline_arrows(self): return arrows_x, arrows_y - def get_total_streams(self): + def sum_streamlines(self): """ - :rtype (list) streams_x: all x values for each stream combined into - single list - :rtype (list) streams_y: all y values for each stream combined into - single list + :rtype (list) streamline_x: all x values for each streamline combined + into single list + :rtype (list) streamline_y: all y values for each streamline combined + into single list """ - streams_x = sum(self.st_x, []) - streams_y = sum(self.st_y, []) - return streams_x, streams_y + streamline_x = sum(self.st_x, []) + streamline_y = sum(self.st_y, []) + return streamline_x, streamline_y

AltStyle によって変換されたページ (->オリジナル) /