Exemplo n.º 1
0
    def test_default_dendrogram(self):
        X = np.array([[1, 2, 3, 4], [1, 1, 3, 4], [1, 2, 1, 4], [1, 2, 3, 1]])
        dendro = tls.FigureFactory.create_dendrogram(X=X)

        expected_dendro = go.Figure(
            data=go.Data([
                go.Scatter(x=np.array([25., 25., 35., 35.]),
                           y=np.array([0., 1., 1., 0.]),
                           marker=go.Marker(color='rgb(61,153,112)'),
                           mode='lines',
                           xaxis='x',
                           yaxis='y'),
                go.Scatter(x=np.array([15., 15., 30., 30.]),
                           y=np.array([0., 2.23606798, 2.23606798, 1.]),
                           marker=go.Marker(color='rgb(61,153,112)'),
                           mode='lines',
                           xaxis='x',
                           yaxis='y'),
                go.Scatter(x=np.array([5., 5., 22.5, 22.5]),
                           y=np.array([0., 3.60555128, 3.60555128,
                                       2.23606798]),
                           marker=go.Marker(color='rgb(0,116,217)'),
                           mode='lines',
                           xaxis='x',
                           yaxis='y')
            ]),
            layout=go.Layout(autosize=False,
                             height='100%',
                             hovermode='closest',
                             showlegend=False,
                             width='100%',
                             xaxis=go.XAxis(mirror='allticks',
                                            rangemode='tozero',
                                            showgrid=False,
                                            showline=True,
                                            showticklabels=True,
                                            tickmode='array',
                                            ticks='outside',
                                            ticktext=np.array(
                                                ['3', '2', '0', '1']),
                                            tickvals=[5.0, 15.0, 25.0, 35.0],
                                            type='linear',
                                            zeroline=False),
                             yaxis=go.YAxis(mirror='allticks',
                                            rangemode='tozero',
                                            showgrid=False,
                                            showline=True,
                                            showticklabels=True,
                                            ticks='outside',
                                            type='linear',
                                            zeroline=False)))

        self.assertEqual(len(dendro['data']), 3)

        # this is actually a bit clearer when debugging tests.
        self.assert_dict_equal(dendro['data'][0], expected_dendro['data'][0])
        self.assert_dict_equal(dendro['data'][1], expected_dendro['data'][1])
        self.assert_dict_equal(dendro['data'][2], expected_dendro['data'][2])

        self.assert_dict_equal(dendro['layout'], expected_dendro['layout'])
Exemplo n.º 2
0
def make_YAxis(yaxis_title):
    """
    Makes the y-axis for a violin plot.
    """
    yaxis = graph_objs.YAxis(title=yaxis_title,
                             showticklabels=True,
                             autorange=True,
                             ticklen=4,
                             showline=True,
                             zeroline=False,
                             showgrid=False,
                             mirror=False)
    return yaxis
Exemplo n.º 3
0
def get_subplots(rows=1,
                 columns=1,
                 horizontal_spacing=0.1,
                 vertical_spacing=0.15,
                 print_grid=False):
    """Return a dictionary instance with the subplots set in 'layout'.

    Example 1:
        # stack two subplots vertically
        fig = tools.get_subplots(rows=2)
        fig['data'] += [Scatter(x=[1,2,3], y=[2,1,2], xaxis='x1', yaxis='y1')]
        fig['data'] += [Scatter(x=[1,2,3], y=[2,1,2], xaxis='x2', yaxis='y2')]

    Example 2:
        # print out string showing the subplot grid you've put in the layout
        fig = tools.get_subplots(rows=3, columns=2, print_grid=True)

    key (types, default=default):
        description.

    rows (int, default=1):
        Number of rows, evenly spaced vertically on the figure.

    columns (int, default=1):
        Number of columns, evenly spaced horizontally on the figure.

    horizontal_spacing (float in [0,1], default=0.1):
        Space between subplot columns. Applied to all columns.

    vertical_spacing (float in [0,1], default=0.05):
        Space between subplot rows. Applied to all rows.

    print_grid (True | False, default=False):
        If True, prints a tab-delimited string representation of your plot grid.

    """
    fig = dict(layout=graph_objs.Layout())  # will return this at the end
    plot_width = (1 - horizontal_spacing * (columns - 1)) / columns
    plot_height = (1 - vertical_spacing * (rows - 1)) / rows
    plot_num = 0
    for rrr in range(rows):
        for ccc in range(columns):
            xaxis_name = 'xaxis{0}'.format(plot_num + 1)
            x_anchor = 'y{0}'.format(plot_num + 1)
            x_start = (plot_width + horizontal_spacing) * ccc
            x_end = x_start + plot_width

            yaxis_name = 'yaxis{0}'.format(plot_num + 1)
            y_anchor = 'x{0}'.format(plot_num + 1)
            y_start = (plot_height + vertical_spacing) * rrr
            y_end = y_start + plot_height

            xaxis = graph_objs.XAxis(domain=[x_start, x_end], anchor=x_anchor)
            fig['layout'][xaxis_name] = xaxis
            yaxis = graph_objs.YAxis(domain=[y_start, y_end], anchor=y_anchor)
            fig['layout'][yaxis_name] = yaxis
            plot_num += 1
    if print_grid:
        print("This is the format of your plot grid!")
        grid_string = ""
        plot = 1
        for rrr in range(rows):
            grid_line = ""
            for ccc in range(columns):
                grid_line += "[{0}]\t".format(plot)
                plot += 1
            grid_string = grid_line + '\n' + grid_string
        print(grid_string)
    return graph_objs.Figure(fig)  # forces us to validate what we just did...
Exemplo n.º 4
0
def get_subplots(rows=1, columns=1, print_grid=False, **kwargs):
    """Return a dictionary instance with the subplots set in 'layout'.

    Example 1:
    # stack two subplots vertically
    fig = tools.get_subplots(rows=2)
    fig['data'] += [Scatter(x=[1,2,3], y=[2,1,2], xaxis='x1', yaxis='y1')]
    fig['data'] += [Scatter(x=[1,2,3], y=[2,1,2], xaxis='x2', yaxis='y2')]

    Example 2:
    # print out string showing the subplot grid you've put in the layout
    fig = tools.get_subplots(rows=3, columns=2, print_grid=True)

    Keywords arguments with constant defaults:

    rows (kwarg, int greater than 0, default=1):
        Number of rows, evenly spaced vertically on the figure.

    columns (kwarg, int greater than 0, default=1):
        Number of columns, evenly spaced horizontally on the figure.

    horizontal_spacing (kwarg, float in [0,1], default=0.1):
        Space between subplot columns. Applied to all columns.

    vertical_spacing (kwarg, float in [0,1], default=0.05):
        Space between subplot rows. Applied to all rows.

    print_grid (kwarg, True | False, default=False):
        If True, prints a tab-delimited string representation
        of your plot grid.

    Keyword arguments with variable defaults:

    horizontal_spacing (kwarg, float in [0,1], default=0.2 / columns):
        Space between subplot columns.

    vertical_spacing (kwarg, float in [0,1], default=0.3 / rows):
        Space between subplot rows.

    """

    warnings.warn(
        "tools.get_subplots is depreciated. "
        "Please use tools.make_subplots instead."
    )

    # Throw exception for non-integer rows and columns
    if not isinstance(rows, int) or rows <= 0:
        raise Exception("Keyword argument 'rows' "
                        "must be an int greater than 0")
    if not isinstance(columns, int) or columns <= 0:
        raise Exception("Keyword argument 'columns' "
                        "must be an int greater than 0")

    # Throw exception if non-valid kwarg is sent
    VALID_KWARGS = ['horizontal_spacing', 'vertical_spacing']
    for key in kwargs.keys():
        if key not in VALID_KWARGS:
            raise Exception("Invalid keyword argument: '{0}'".format(key))

    # Set 'horizontal_spacing' / 'vertical_spacing' w.r.t. rows / columns
    try:
        horizontal_spacing = float(kwargs['horizontal_spacing'])
    except KeyError:
        horizontal_spacing = 0.2 / columns
    try:
        vertical_spacing = float(kwargs['vertical_spacing'])
    except KeyError:
        vertical_spacing = 0.3 / rows

    fig = dict(layout=graph_objs.Layout())  # will return this at the end
    plot_width = (1 - horizontal_spacing * (columns - 1)) / columns
    plot_height = (1 - vertical_spacing * (rows - 1)) / rows
    plot_num = 0
    for rrr in range(rows):
        for ccc in range(columns):
            xaxis_name = 'xaxis{0}'.format(plot_num + 1)
            x_anchor = 'y{0}'.format(plot_num + 1)
            x_start = (plot_width + horizontal_spacing) * ccc
            x_end = x_start + plot_width

            yaxis_name = 'yaxis{0}'.format(plot_num + 1)
            y_anchor = 'x{0}'.format(plot_num + 1)
            y_start = (plot_height + vertical_spacing) * rrr
            y_end = y_start + plot_height

            xaxis = graph_objs.XAxis(domain=[x_start, x_end], anchor=x_anchor)
            fig['layout'][xaxis_name] = xaxis
            yaxis = graph_objs.YAxis(domain=[y_start, y_end], anchor=y_anchor)
            fig['layout'][yaxis_name] = yaxis
            plot_num += 1

    if print_grid:
        print("This is the format of your plot grid!")
        grid_string = ""
        plot = 1
        for rrr in range(rows):
            grid_line = ""
            for ccc in range(columns):
                grid_line += "[{0}]\t".format(plot)
                plot += 1
            grid_string = grid_line + '\n' + grid_string
        print(grid_string)

    return graph_objs.Figure(fig)  # forces us to validate what we just did...
def create_trisurf(x,
                   y,
                   z,
                   simplices,
                   colormap=None,
                   show_colorbar=True,
                   scale=None,
                   color_func=None,
                   title='Trisurf Plot',
                   plot_edges=True,
                   showbackground=True,
                   backgroundcolor='rgb(230, 230, 230)',
                   gridcolor='rgb(255, 255, 255)',
                   zerolinecolor='rgb(255, 255, 255)',
                   edges_color='rgb(50, 50, 50)',
                   height=800,
                   width=800,
                   aspectratio=None):
    """
    Returns figure for a triangulated surface plot

    :param (array) x: data values of x in a 1D array
    :param (array) y: data values of y in a 1D array
    :param (array) z: data values of z in a 1D array
    :param (array) simplices: an array of shape (ntri, 3) where ntri is
        the number of triangles in the triangularization. Each row of the
        array contains the indicies of the verticies of each triangle
    :param (str|tuple|list) colormap: either a plotly scale name, an rgb
        or hex color, a color tuple or a list of colors. An rgb color is
        of the form 'rgb(x, y, z)' where x, y, z belong to the interval
        [0, 255] and a color tuple is a tuple of the form (a, b, c) where
        a, b and c belong to [0, 1]. If colormap is a list, it must
        contain the valid color types aforementioned as its members
    :param (bool) show_colorbar: determines if colorbar is visible
    :param (list|array) scale: sets the scale values to be used if a non-
        linearly interpolated colormap is desired. If left as None, a
        linear interpolation between the colors will be excecuted
    :param (function|list) color_func: The parameter that determines the
        coloring of the surface. Takes either a function with 3 arguments
        x, y, z or a list/array of color values the same length as
        simplices. If None, coloring will only depend on the z axis
    :param (str) title: title of the plot
    :param (bool) plot_edges: determines if the triangles on the trisurf
        are visible
    :param (bool) showbackground: makes background in plot visible
    :param (str) backgroundcolor: color of background. Takes a string of
        the form 'rgb(x,y,z)' x,y,z are between 0 and 255 inclusive
    :param (str) gridcolor: color of the gridlines besides the axes. Takes
        a string of the form 'rgb(x,y,z)' x,y,z are between 0 and 255
        inclusive
    :param (str) zerolinecolor: color of the axes. Takes a string of the
        form 'rgb(x,y,z)' x,y,z are between 0 and 255 inclusive
    :param (str) edges_color: color of the edges, if plot_edges is True
    :param (int|float) height: the height of the plot (in pixels)
    :param (int|float) width: the width of the plot (in pixels)
    :param (dict) aspectratio: a dictionary of the aspect ratio values for
        the x, y and z axes. 'x', 'y' and 'z' take (int|float) values

    Example 1: Sphere
    ```
    # Necessary Imports for Trisurf
    import numpy as np
    from scipy.spatial import Delaunay

    import plotly.plotly as py
    from plotly.figure_factory import create_trisurf
    from plotly.graph_objs import graph_objs

    # Make data for plot
    u = np.linspace(0, 2*np.pi, 20)
    v = np.linspace(0, np.pi, 20)
    u,v = np.meshgrid(u,v)
    u = u.flatten()
    v = v.flatten()

    x = np.sin(v)*np.cos(u)
    y = np.sin(v)*np.sin(u)
    z = np.cos(v)

    points2D = np.vstack([u,v]).T
    tri = Delaunay(points2D)
    simplices = tri.simplices

    # Create a figure
    fig1 = create_trisurf(x=x, y=y, z=z, colormap="Rainbow",
                          simplices=simplices)
    # Plot the data
    py.iplot(fig1, filename='trisurf-plot-sphere')
    ```

    Example 2: Torus
    ```
    # Necessary Imports for Trisurf
    import numpy as np
    from scipy.spatial import Delaunay

    import plotly.plotly as py
    from plotly.figure_factory import create_trisurf
    from plotly.graph_objs import graph_objs

    # Make data for plot
    u = np.linspace(0, 2*np.pi, 20)
    v = np.linspace(0, 2*np.pi, 20)
    u,v = np.meshgrid(u,v)
    u = u.flatten()
    v = v.flatten()

    x = (3 + (np.cos(v)))*np.cos(u)
    y = (3 + (np.cos(v)))*np.sin(u)
    z = np.sin(v)

    points2D = np.vstack([u,v]).T
    tri = Delaunay(points2D)
    simplices = tri.simplices

    # Create a figure
    fig1 = create_trisurf(x=x, y=y, z=z, colormap="Viridis",
                          simplices=simplices)
    # Plot the data
    py.iplot(fig1, filename='trisurf-plot-torus')
    ```

    Example 3: Mobius Band
    ```
    # Necessary Imports for Trisurf
    import numpy as np
    from scipy.spatial import Delaunay

    import plotly.plotly as py
    from plotly.figure_factory import create_trisurf
    from plotly.graph_objs import graph_objs

    # Make data for plot
    u = np.linspace(0, 2*np.pi, 24)
    v = np.linspace(-1, 1, 8)
    u,v = np.meshgrid(u,v)
    u = u.flatten()
    v = v.flatten()

    tp = 1 + 0.5*v*np.cos(u/2.)
    x = tp*np.cos(u)
    y = tp*np.sin(u)
    z = 0.5*v*np.sin(u/2.)

    points2D = np.vstack([u,v]).T
    tri = Delaunay(points2D)
    simplices = tri.simplices

    # Create a figure
    fig1 = create_trisurf(x=x, y=y, z=z, colormap=[(0.2, 0.4, 0.6), (1, 1, 1)],
                          simplices=simplices)
    # Plot the data
    py.iplot(fig1, filename='trisurf-plot-mobius-band')
    ```

    Example 4: Using a Custom Colormap Function with Light Cone
    ```
    # Necessary Imports for Trisurf
    import numpy as np
    from scipy.spatial import Delaunay

    import plotly.plotly as py
    from plotly.figure_factory import create_trisurf
    from plotly.graph_objs import graph_objs

    # Make data for plot
    u=np.linspace(-np.pi, np.pi, 30)
    v=np.linspace(-np.pi, np.pi, 30)
    u,v=np.meshgrid(u,v)
    u=u.flatten()
    v=v.flatten()

    x = u
    y = u*np.cos(v)
    z = u*np.sin(v)

    points2D = np.vstack([u,v]).T
    tri = Delaunay(points2D)
    simplices = tri.simplices

    # Define distance function
    def dist_origin(x, y, z):
        return np.sqrt((1.0 * x)**2 + (1.0 * y)**2 + (1.0 * z)**2)

    # Create a figure
    fig1 = create_trisurf(x=x, y=y, z=z,
                          colormap=['#FFFFFF', '#E4FFFE',
                                    '#A4F6F9', '#FF99FE',
                                    '#BA52ED'],
                          scale=[0, 0.6, 0.71, 0.89, 1],
                          simplices=simplices,
                          color_func=dist_origin)
    # Plot the data
    py.iplot(fig1, filename='trisurf-plot-custom-coloring')
    ```

    Example 5: Enter color_func as a list of colors
    ```
    # Necessary Imports for Trisurf
    import numpy as np
    from scipy.spatial import Delaunay
    import random

    import plotly.plotly as py
    from plotly.figure_factory import create_trisurf
    from plotly.graph_objs import graph_objs

    # Make data for plot
    u=np.linspace(-np.pi, np.pi, 30)
    v=np.linspace(-np.pi, np.pi, 30)
    u,v=np.meshgrid(u,v)
    u=u.flatten()
    v=v.flatten()

    x = u
    y = u*np.cos(v)
    z = u*np.sin(v)

    points2D = np.vstack([u,v]).T
    tri = Delaunay(points2D)
    simplices = tri.simplices


    colors = []
    color_choices = ['rgb(0, 0, 0)', '#6c4774', '#d6c7dd']

    for index in range(len(simplices)):
        colors.append(random.choice(color_choices))

    fig = create_trisurf(
        x, y, z, simplices,
        color_func=colors,
        show_colorbar=True,
        edges_color='rgb(2, 85, 180)',
        title=' Modern Art'
    )

    py.iplot(fig, filename="trisurf-plot-modern-art")
    ```
    """
    if aspectratio is None:
        aspectratio = {'x': 1, 'y': 1, 'z': 1}

    # Validate colormap
    colors.validate_colors(colormap)
    colormap, scale = colors.convert_colors_to_same_type(
        colormap, colortype='tuple', return_default_colors=True, scale=scale)

    data1 = trisurf(x,
                    y,
                    z,
                    simplices,
                    show_colorbar=show_colorbar,
                    color_func=color_func,
                    colormap=colormap,
                    scale=scale,
                    edges_color=edges_color,
                    plot_edges=plot_edges)

    axis = dict(
        showbackground=showbackground,
        backgroundcolor=backgroundcolor,
        gridcolor=gridcolor,
        zerolinecolor=zerolinecolor,
    )
    layout = graph_objs.Layout(title=title,
                               width=width,
                               height=height,
                               scene=graph_objs.Scene(
                                   xaxis=graph_objs.XAxis(axis),
                                   yaxis=graph_objs.YAxis(axis),
                                   zaxis=graph_objs.ZAxis(axis),
                                   aspectratio=dict(x=aspectratio['x'],
                                                    y=aspectratio['y'],
                                                    z=aspectratio['z']),
                               ))

    return graph_objs.Figure(data=data1, layout=layout)
Exemplo n.º 6
0
    def test_dendrogram_colorscale(self):
        X = np.array([[1, 2, 3, 4], [1, 1, 3, 4], [1, 2, 1, 4], [1, 2, 3, 1]])
        greyscale = [
            'rgb(0,0,0)',  # black
            'rgb(05,105,105)',  # dim grey
            'rgb(128,128,128)',  # grey
            'rgb(169,169,169)',  # dark grey
            'rgb(192,192,192)',  # silver
            'rgb(211,211,211)',  # light grey
            'rgb(220,220,220)',  # gainsboro
            'rgb(245,245,245)'
        ]  # white smoke

        dendro = tls.FigureFactory.create_dendrogram(X, colorscale=greyscale)

        expected_dendro = go.Figure(
            data=go.Data([
                go.Scatter(x=np.array([25., 25., 35., 35.]),
                           y=np.array([0., 1., 1., 0.]),
                           marker=go.Marker(color='rgb(128,128,128)'),
                           mode='lines',
                           xaxis='x',
                           yaxis='y'),
                go.Scatter(x=np.array([15., 15., 30., 30.]),
                           y=np.array([0., 2.23606798, 2.23606798, 1.]),
                           marker=go.Marker(color='rgb(128,128,128)'),
                           mode='lines',
                           xaxis='x',
                           yaxis='y'),
                go.Scatter(x=np.array([5., 5., 22.5, 22.5]),
                           y=np.array([0., 3.60555128, 3.60555128,
                                       2.23606798]),
                           marker=go.Marker(color='rgb(0,0,0)'),
                           mode='lines',
                           xaxis='x',
                           yaxis='y')
            ]),
            layout=go.Layout(autosize=False,
                             height='100%',
                             hovermode='closest',
                             showlegend=False,
                             width='100%',
                             xaxis=go.XAxis(mirror='allticks',
                                            rangemode='tozero',
                                            showgrid=False,
                                            showline=True,
                                            showticklabels=True,
                                            tickmode='array',
                                            ticks='outside',
                                            ticktext=np.array(
                                                ['3', '2', '0', '1']),
                                            tickvals=[5.0, 15.0, 25.0, 35.0],
                                            type='linear',
                                            zeroline=False),
                             yaxis=go.YAxis(mirror='allticks',
                                            rangemode='tozero',
                                            showgrid=False,
                                            showline=True,
                                            showticklabels=True,
                                            ticks='outside',
                                            type='linear',
                                            zeroline=False)))

        self.assertEqual(len(dendro['data']), 3)

        # this is actually a bit clearer when debugging tests.
        self.assert_dict_equal(dendro['data'][0], expected_dendro['data'][0])
        self.assert_dict_equal(dendro['data'][1], expected_dendro['data'][1])
        self.assert_dict_equal(dendro['data'][2], expected_dendro['data'][2])
Exemplo n.º 7
0
    def test_dendrogram_random_matrix(self):

        # create a random uncorrelated matrix
        X = np.random.rand(5, 5)

        # variable 2 is correlated with all the other variables
        X[2, :] = sum(X, 0)

        names = ['Jack', 'Oxana', 'John', 'Chelsea', 'Mark']
        dendro = tls.FigureFactory.create_dendrogram(X, labels=names)

        expected_dendro = go.Figure(
            data=go.Data([
                go.Scatter(marker=go.Marker(color='rgb(61,153,112)'),
                           mode='lines',
                           xaxis='x',
                           yaxis='y'),
                go.Scatter(marker=go.Marker(color='rgb(61,153,112)'),
                           mode='lines',
                           xaxis='x',
                           yaxis='y'),
                go.Scatter(marker=go.Marker(color='rgb(61,153,112)'),
                           mode='lines',
                           xaxis='x',
                           yaxis='y'),
                go.Scatter(marker=go.Marker(color='rgb(0,116,217)'),
                           mode='lines',
                           xaxis='x',
                           yaxis='y')
            ]),
            layout=go.Layout(autosize=False,
                             height='100%',
                             hovermode='closest',
                             showlegend=False,
                             width='100%',
                             xaxis=go.XAxis(
                                 mirror='allticks',
                                 rangemode='tozero',
                                 showgrid=False,
                                 showline=True,
                                 showticklabels=True,
                                 tickmode='array',
                                 ticks='outside',
                                 tickvals=[5.0, 15.0, 25.0, 35.0, 45.0],
                                 type='linear',
                                 zeroline=False),
                             yaxis=go.YAxis(mirror='allticks',
                                            rangemode='tozero',
                                            showgrid=False,
                                            showline=True,
                                            showticklabels=True,
                                            ticks='outside',
                                            type='linear',
                                            zeroline=False)))

        self.assertEqual(len(dendro['data']), 4)

        # it's random, so we can only check that the values aren't equal
        y_vals = [
            dendro['data'][0].pop('y'), dendro['data'][1].pop('y'),
            dendro['data'][2].pop('y'), dendro['data'][3].pop('y')
        ]
        for i in range(len(y_vals)):
            for j in range(len(y_vals)):
                if i != j:
                    self.assertFalse(np.allclose(y_vals[i], y_vals[j]))

        x_vals = [
            dendro['data'][0].pop('x'), dendro['data'][1].pop('x'),
            dendro['data'][2].pop('x'), dendro['data'][3].pop('x')
        ]
        for i in range(len(x_vals)):
            for j in range(len(x_vals)):
                if i != j:
                    self.assertFalse(np.allclose(x_vals[i], x_vals[j]))

        # we also need to check the ticktext manually
        xaxis_ticktext = dendro['layout']['xaxis'].pop('ticktext')
        self.assertEqual(xaxis_ticktext[0], 'John')

        # this is actually a bit clearer when debugging tests.
        self.assert_dict_equal(dendro['data'][0], expected_dendro['data'][0])
        self.assert_dict_equal(dendro['data'][1], expected_dendro['data'][1])
        self.assert_dict_equal(dendro['data'][2], expected_dendro['data'][2])
        self.assert_dict_equal(dendro['data'][3], expected_dendro['data'][3])

        self.assert_dict_equal(dendro['layout'], expected_dendro['layout'])