Exemple #1
0
def make_non_outlier_interval(d1, d2):
    """
    Returns the scatterplot fig of most of a violin plot.
    """
    return graph_objs.Scatter(x=[0, 0],
                              y=[d1, d2],
                              name='',
                              mode='lines',
                              line=graph_objs.Line(width=1.5,
                                                   color='rgb(0,0,0)'))
Exemple #2
0
def make_quartiles(q1, q3):
    """
    Makes the upper and lower quartiles for a violin plot.
    """
    return graph_objs.Scatter(x=[0, 0],
                              y=[q1, q3],
                              text=[
                                  'lower-quartile: ' + '{:0.2f}'.format(q1),
                                  'upper-quartile: ' + '{:0.2f}'.format(q3)
                              ],
                              mode='lines',
                              line=graph_objs.Line(width=4,
                                                   color='rgb(0,0,0)'),
                              hoverinfo='text')
Exemple #3
0
def make_diff(m1, m2):
    """
    Makes the difference of medians for a violin plot.
    """
    return graph_objs.Scatter(x=[0, 0],
                              y=[m1, m2],
                              text=[
                                  'median 1: ' + '{:0.2f}'.format(m1),
                                  'median 2: ' + '{:0.2f}'.format(m2)
                              ],
                              mode='lines',
                              line=graph_objs.Line(width=2,
                                                   color='rgb(0,0,0)'),
                              hoverinfo='text')
    def test_more_kwargs(self):

        # we should be able to create 2 arrows and change the arrow_scale,
        # angle, and arrow using create_quiver

        quiver = tls.FigureFactory.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=graph_objs.Line(color='purple',
                                                                      width=3))
        expected_quiver = {'data': [{'line': {'color': 'purple', 'width': 3},
                                     'mode': 'lines',
                                     'type': u'scatter',
                                     'x': [1,
                                           1.0540302305868139,
                                           None,
                                           2,
                                           1.9583853163452858,
                                           None,
                                           1.052143029378767,
                                           1.0540302305868139,
                                           1.0184841899864512,
                                           None,
                                           1.9909870141679737,
                                           1.9583853163452858,
                                           1.9546151170949464,
                                           None],
                                     'y': [1,
                                           1.0841470984807897,
                                           None,
                                           2,
                                           2.0909297426825684,
                                           None,
                                           1.044191642387781,
                                           1.0841470984807897,
                                           1.0658037346225067,
                                           None,
                                           2.0677536925644366,
                                           2.0909297426825684,
                                           2.051107819102551,
                                           None]}],
                           'layout': {'hovermode': 'closest'}}
        self.assertEqual(quiver, expected_quiver)
Exemple #5
0
def make_half_violin(x, y, fillcolor='#1f77b4', linecolor='rgb(0, 0, 0)'):
    """
    Produces a sideways probability distribution fig violin plot.
    """
    text = ['(pdf(y), y)=(' + '{:0.2f}'.format(x[i]) +
            ', ' + '{:0.2f}'.format(y[i]) + ')'
            for i in range(len(x))]

    return graph_objs.Scatter(
        x=x,
        y=y,
        mode='lines',
        name='',
        text=text,
        fill='tonextx',
        fillcolor=fillcolor,
        line=graph_objs.Line(width=0.5, color=linecolor, shape='spline'),
        hoverinfo='text',
        opacity=0.5
    )
def trisurf(x,
            y,
            z,
            simplices,
            show_colorbar,
            edges_color,
            scale,
            colormap=None,
            color_func=None,
            plot_edges=False,
            x_edge=None,
            y_edge=None,
            z_edge=None,
            facecolor=None):
    """
    Refer to FigureFactory.create_trisurf() for docstring
    """
    # numpy import check
    if not np:
        raise ImportError("FigureFactory._trisurf() requires "
                          "numpy imported.")
    points3D = np.vstack((x, y, z)).T
    simplices = np.atleast_2d(simplices)

    # vertices of the surface triangles
    tri_vertices = points3D[simplices]

    # Define colors for the triangle faces
    if color_func is None:
        # mean values of z-coordinates of triangle vertices
        mean_dists = tri_vertices[:, :, 2].mean(-1)
    elif isinstance(color_func, (list, np.ndarray)):
        # Pre-computed list / array of values to map onto color
        if len(color_func) != len(simplices):
            raise ValueError("If color_func is a list/array, it must "
                             "be the same length as simplices.")

        # convert all colors in color_func to rgb
        for index in range(len(color_func)):
            if isinstance(color_func[index], str):
                if '#' in color_func[index]:
                    foo = colors.hex_to_rgb(color_func[index])
                    color_func[index] = colors.label_rgb(foo)

            if isinstance(color_func[index], tuple):
                foo = colors.convert_to_RGB_255(color_func[index])
                color_func[index] = colors.label_rgb(foo)

        mean_dists = np.asarray(color_func)
    else:
        # apply user inputted function to calculate
        # custom coloring for triangle vertices
        mean_dists = []
        for triangle in tri_vertices:
            dists = []
            for vertex in triangle:
                dist = color_func(vertex[0], vertex[1], vertex[2])
                dists.append(dist)
            mean_dists.append(np.mean(dists))
        mean_dists = np.asarray(mean_dists)

    # Check if facecolors are already strings and can be skipped
    if isinstance(mean_dists[0], str):
        facecolor = mean_dists
    else:
        min_mean_dists = np.min(mean_dists)
        max_mean_dists = np.max(mean_dists)

        if facecolor is None:
            facecolor = []
        for index in range(len(mean_dists)):
            color = map_face2color(mean_dists[index], colormap, scale,
                                   min_mean_dists, max_mean_dists)
            facecolor.append(color)

    # Make sure facecolor is a list so output is consistent across Pythons
    facecolor = np.asarray(facecolor)
    ii, jj, kk = simplices.T

    triangles = graph_objs.Mesh3d(x=x,
                                  y=y,
                                  z=z,
                                  facecolor=facecolor,
                                  i=ii,
                                  j=jj,
                                  k=kk,
                                  name='')

    mean_dists_are_numbers = not isinstance(mean_dists[0], str)

    if mean_dists_are_numbers and show_colorbar is True:
        # make a colorscale from the colors
        colorscale = colors.make_colorscale(colormap, scale)
        colorscale = colors.convert_colorscale_to_rgb(colorscale)

        colorbar = graph_objs.Scatter3d(
            x=x[:1],
            y=y[:1],
            z=z[:1],
            mode='markers',
            marker=dict(size=0.1,
                        color=[min_mean_dists, max_mean_dists],
                        colorscale=colorscale,
                        showscale=True),
            hoverinfo='None',
            showlegend=False)

    # the triangle sides are not plotted
    if plot_edges is False:
        if mean_dists_are_numbers and show_colorbar is True:
            return graph_objs.Data([triangles, colorbar])
        else:
            return graph_objs.Data([triangles])

    # define the lists x_edge, y_edge and z_edge, of x, y, resp z
    # coordinates of edge end points for each triangle
    # None separates data corresponding to two consecutive triangles
    is_none = [ii is None for ii in [x_edge, y_edge, z_edge]]
    if any(is_none):
        if not all(is_none):
            raise ValueError("If any (x_edge, y_edge, z_edge) is None, "
                             "all must be None")
        else:
            x_edge = []
            y_edge = []
            z_edge = []

    # Pull indices we care about, then add a None column to separate tris
    ixs_triangles = [0, 1, 2, 0]
    pull_edges = tri_vertices[:, ixs_triangles, :]
    x_edge_pull = np.hstack(
        [pull_edges[:, :, 0],
         np.tile(None, [pull_edges.shape[0], 1])])
    y_edge_pull = np.hstack(
        [pull_edges[:, :, 1],
         np.tile(None, [pull_edges.shape[0], 1])])
    z_edge_pull = np.hstack(
        [pull_edges[:, :, 2],
         np.tile(None, [pull_edges.shape[0], 1])])

    # Now unravel the edges into a 1-d vector for plotting
    x_edge = np.hstack([x_edge, x_edge_pull.reshape([1, -1])[0]])
    y_edge = np.hstack([y_edge, y_edge_pull.reshape([1, -1])[0]])
    z_edge = np.hstack([z_edge, z_edge_pull.reshape([1, -1])[0]])

    if not (len(x_edge) == len(y_edge) == len(z_edge)):
        raise exceptions.PlotlyError("The lengths of x_edge, y_edge and "
                                     "z_edge are not the same.")

    # define the lines for plotting
    lines = graph_objs.Scatter3d(x=x_edge,
                                 y=y_edge,
                                 z=z_edge,
                                 mode='lines',
                                 line=graph_objs.Line(color=edges_color,
                                                      width=1.5),
                                 showlegend=False)

    if mean_dists_are_numbers and show_colorbar is True:
        return graph_objs.Data([triangles, lines, colorbar])
    else:
        return graph_objs.Data([triangles, lines])