Show Sidebar Hide Sidebar

Carpet Plots in Python

How to make carpet plots in Python with Plotly.

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!

Version Check

Plotly's python package is updated frequently. Run pip install plotly --upgrade to use the latest version.

In [1]:
import plotly
plotly.__version__
Out[1]:
'2.0.15'

Set X and Y Coordinates

To set the x and y coordinates use x and y attributes. If x coordindate values are ommitted a cheater plot will be created. The plot below has a y array specified but requires a and b parameter values before an axis may be plotted.

In [2]:
import plotly.graph_objs as go
import plotly.plotly as py

trace1 = go.Carpet(
    y = [2, 3.5, 4, 3, 4.5, 5, 5.5, 6.5, 7.5, 8, 8.5, 10]
)

data = [trace1]

fig = go.Figure(data = data)
py.iplot(fig, filename = "carpet/basic")
Out[2]:

Add Parameter Values

To save parameter values use the a and b attributes.

In [3]:
import plotly.graph_objs as go
import plotly.plotly as py

trace1 = go.Carpet(
    a = [4, 4, 4, 4.5, 4.5, 4.5, 5, 5, 5, 6, 6, 6],
    b = [1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3],
    y = [2, 3.5, 4, 3, 4.5, 5, 5.5, 6.5, 7.5, 8, 8.5, 10]
)

data = [trace1]

fig = go.Figure(data = data)
py.iplot(fig, filename = "carpet/add-parameters")
Out[3]:

Add A and B axis

Use aaxis or baxis list to make changes to the axes. For a more detailed list of attributes refer to R reference.

In [4]:
import plotly.graph_objs as go
import plotly.plotly as py

trace1 = go.Carpet(
    a = [4, 4, 4, 4.5, 4.5, 4.5, 5, 5, 5, 6, 6, 6],
    b = [1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3],
    y = [2, 3.5, 4, 3, 4.5, 5, 5.5, 6.5, 7.5, 8, 8.5, 10],
    aaxis = dict(
        tickprefix = 'a = ',
        ticksuffix = 'm',
        smoothing = 1,
        minorgridcount = 9,
    ),
    baxis = dict(
        tickprefix = 'b = ',
        ticksuffix = 'pa',
        smoothing = 1,
        minorgridcount = 9,
    )
)

data = [trace1]

fig = go.Figure(data = data)
py.iplot(fig, filename = "carpet/add-axes")
Out[4]:

Alternate input format

The data arrays x, y may either be specified as one-dimensional arrays of data or as arrays of arrays. If one-dimensional, then x, y, a, and b should all be the same length. If x and y are arrays of arrays, then the length of a should match the inner dimension and the length of b the outer dimension. The plot below represents the same plot as those above.

In [5]:
import plotly.graph_objs as go
import plotly.plotly as py

trace1 = go.Carpet(
    a = [4, 4.5, 5, 6],
    b = [1, 2, 3],
    y = [[2, 3, 5.5, 8],
         [3.5, 4.5, 6.5, 8.5],
         [4, 5, 7.5, 10]]
)

data = [trace1]

fig = go.Figure(data = data)
py.iplot(fig, filename = "carpet/input-format")
Out[5]:

Cheater plot layout

The layout of cheater plots is not unique and depends upon the cheaterslope and axis cheatertype parameters. If x is not specified, each row of the x array is constructed based on the the formula a + cheaterslope * b, where a and b are either the value or the integer index of a and b respectively, depending on the corresponding axis cheatertype. Although the layout of the axis below is different than the plots above, it represents the same data as the axes above.

In [6]:
import plotly.graph_objs as go
import plotly.plotly as py

trace1 = go.Carpet(
    a = [4, 4.5, 5, 6],
    b = [1, 2, 3],
    y = [[2, 3, 5.5, 8],
         [3.5, 4.5, 6.5, 8.5],
         [4, 5, 7.5, 10]],
    cheaterslope = -5,
    aaxis = dict(cheatertype = 'index'),
    baxis = dict(cheatertype = 'value')
)

data = [trace1]

fig = go.Figure(data = data)
py.iplot(fig, filename = "carpet/cheater-layout")
Out[6]:

Style A and B axis

In [7]:
import plotly.graph_objs as go
import plotly.plotly as py

trace1 = go.Carpet(
    a = [4, 4, 4, 4.5, 4.5, 4.5, 5, 5, 5, 6, 6, 6],
    b = [1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3],
    y = [2, 3.5, 4, 3, 4.5, 5, 5.5, 6.5, 7.5, 8, 8.5, 10],
    aaxis = dict(
        tickprefix = 'a = ',
        ticksuffix = 'm',
        smoothing = 1,
        minorgridcount = 9,
        minorgridwidth = 0.6,
        minorgridcolor = 'white',
        gridcolor = 'white',
        color = 'white'
    ),
    baxis = dict(
        ticksuffix = 'Pa',
        smoothing = 1,
        minorgridcount = 9,
        minorgridwidth = 0.6,
        gridcolor = 'white',
        minorgridcolor = 'white',
        color = 'white'
    )
)

data = [trace1]

layout = go.Layout(
    plot_bgcolor = 'black', 
    paper_bgcolor = 'black',
    xaxis = dict(
        showgrid = False, 
        showticklabels = False
    ),
    yaxis = dict(
        showgrid = False, 
        showticklabels = False
    )
)

fig = go.Figure(data = data, layout = layout)
py.iplot(fig, filename = "carpet/styled")
Out[7]:

Add Points and Contours

To add points and lines see Carpet Scatter Plots or to add contours see Carpet Contour Plots

Reference

See https://plot.ly/python/reference/#carpet for more information and chart attribute options!

Still need help?
Contact Us

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