Python plotly.graph_objects.Heatmap() Examples

The following are 3 code examples of plotly.graph_objects.Heatmap(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module plotly.graph_objects , or try the search function .
Example #1
Source File: basic.py    From vectorbt with GNU General Public License v3.0 6 votes vote down vote up
def update_data(self, data):
        """Update the data of the plot efficiently.

        Args:
            data (array_like): Data in any format that can be converted to NumPy.

                Must be of shape (any, `trace_names`).
        """
        data = reshape_fns.to_2d(np.asarray(data))
        checks.assert_same_shape(data, self._trace_names, axis=(1, 0))

        # Update traces
        with self.batch_update():
            for i, box in enumerate(self.data):
                if self._horizontal:
                    box.x = data[:, i]
                    box.y = None
                else:
                    box.x = None
                    box.y = data[:, i]


# ############# Heatmap ############# # 
Example #2
Source File: basic.py    From vectorbt with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, x_labels, y_labels, data=None, horizontal=False, trace_kwargs={}, **layout_kwargs):
        """Create an updatable heatmap plot.

        Args:
            x_labels (list of str): X-axis labels, corresponding to columns in pandas.
            y_labels (list of str): Y-axis labels, corresponding to index in pandas.
            data (array_like): Data in any format that can be converted to NumPy.
            horizontal (bool): Plot horizontally.
            trace_kwargs (dict): Keyword arguments passed to `plotly.graph_objects.Heatmap`.
            **layout_kwargs: Keyword arguments for layout.
        Example:
            ```py
            vbt.Heatmap(['a', 'b'], ['x', 'y'], data=[[1, 2], [3, 4]])
            ```
            ![](/vectorbt/docs/img/Heatmap.png)
            """

        self._x_labels = x_labels
        self._y_labels = y_labels
        self._horizontal = horizontal

        super().__init__()
        self.update_layout(**layout_kwargs)

        # Add traces
        heatmap = go.Heatmap(
            hoverongaps=False,
            colorscale='Plasma'
        )
        if self._horizontal:
            heatmap.y = x_labels
            heatmap.x = y_labels
        else:
            heatmap.x = x_labels
            heatmap.y = y_labels
        heatmap.update(**trace_kwargs)
        self.add_trace(heatmap)

        if data is not None:
            self.update_data(data) 
Example #3
Source File: visualization.py    From AeroSandbox with MIT License 4 votes vote down vote up
def spy(
        matrix,
        show=True,
):
    """
    Plots the sparsity pattern of a matrix.
    :param matrix: The matrix to plot the sparsity pattern of. [2D ndarray or CasADi array]
    :param show: Whether or not to show the sparsity plot. [boolean]
    :return: The figure to be plotted [go.Figure]
    """
    try:
        matrix = matrix.toarray()
    except:
        pass
    abs_m = np.abs(matrix)
    sparsity_pattern = abs_m >= 1e-16
    matrix[sparsity_pattern] = np.log10(abs_m[sparsity_pattern] + 1e-16)
    j_index_map, i_index_map = np.meshgrid(np.arange(matrix.shape[1]), np.arange(matrix.shape[0]))

    i_index = i_index_map[sparsity_pattern]
    j_index = j_index_map[sparsity_pattern]
    val = matrix[sparsity_pattern]
    val = np.ones_like(i_index)
    fig = go.Figure(
        data=go.Heatmap(
            y=i_index,
            x=j_index,
            z=val,
            # type='heatmap',
            colorscale='RdBu',
            showscale=False,
        ),
    )
    fig.update_layout(
        plot_bgcolor="black",
        xaxis=dict(showgrid=False, zeroline=False),
        yaxis=dict(showgrid=False, zeroline=False, autorange="reversed", scaleanchor="x", scaleratio=1),
        width=800,
        height=800 * (matrix.shape[0] / matrix.shape[1]),
    )
    if show:
        fig.show()
    return fig