Show Sidebar Hide Sidebar

Transpose

Transposing a 2D NumPy array swaps the rows and columns of the array. It is reversible so transposing twice returns the original array.

New to Plotly?

Plotly's Python library is free and open source! Get started by downloading the client and reading the primer.
You can set up Plotly to work in online or offline mode, or in jupyter notebooks.
We also have a quick-reference cheatsheet (new!) to help you get started!

Imports

This tutorial imports Plotly and Numpy.

In [1]:
import plotly.plotly as py
from plotly.tools import FigureFactory as FF

import numpy as np

Transpose

np.transpose() returns a transposed version of an ND-array. The process of transposing an ND-array (or a matrix) is reflecting the array about it's main diagonal which runs from top left to bottom right.

In [2]:
import plotly.plotly as py
from plotly.tools import FigureFactory as FF

array = np.array([[1, 2, 3, 4, 5],
                 [6, 7, 8, 9, 10],
                 [11, 12, 13, 14, 15]])

fig = FF.create_annotated_heatmap(array, colorscale='Greens')
fig.layout.title = 'Untransposed Array'
py.iplot(fig, filename='numpy-untransposed')
Out[2]:
In [3]:
import plotly.plotly as py
from plotly.tools import FigureFactory as FF

array = np.array([[1, 2, 3, 4, 5],
                 [6, 7, 8, 9, 10],
                 [11, 12, 13, 14, 15]])

transposed_array = np.transpose(array)

fig = FF.create_annotated_heatmap(transposed_array, colorscale='Greens')
fig.layout.title = 'Transposed Array'
py.iplot(fig, filename='numpy-transposed')
Out[3]:
In [2]:
help(np.transpose)
Help on function transpose in module numpy.core.fromnumeric:

transpose(a, axes=None)
    Permute the dimensions of an array.
    
    Parameters
    ----------
    a : array_like
        Input array.
    axes : list of ints, optional
        By default, reverse the dimensions, otherwise permute the axes
        according to the values given.
    
    Returns
    -------
    p : ndarray
        `a` with its axes permuted.  A view is returned whenever
        possible.
    
    See Also
    --------
    moveaxis
    argsort
    
    Notes
    -----
    Use `transpose(a, argsort(axes))` to invert the transposition of tensors
    when using the `axes` keyword argument.
    
    Transposing a 1-D array returns an unchanged view of the original array.
    
    Examples
    --------
    >>> x = np.arange(4).reshape((2,2))
    >>> x
    array([[0, 1],
           [2, 3]])
    
    >>> np.transpose(x)
    array([[0, 2],
           [1, 3]])
    
    >>> x = np.ones((1, 2, 3))
    >>> np.transpose(x, (1, 0, 2)).shape
    (2, 1, 3)

Still need help?
Contact Us

For guaranteed 24 hour response turnarounds, upgrade to a Developer Support Plan.