Alex
Alex

Reputation: 1131

create a carpet plot with plotly

i am looking for a solution to plot a dataframe with a datetimeindex as a "carpet plot". i prefer plotly, but I also would use other libs. maybe "carpet plot" is not the correct name for the plot?!

i expect the index as xaxis label and for every column a "bucket". maybe a stacked area plot is a solution. i am not able to figure it out.

import pandas as pd
import numpy as np
from datetime import datetime, timedelta

date_today = datetime.now()
days = pd.date_range(date_today, date_today + timedelta(7), freq='D')

np.random.seed(seed=1111)
data = [1,1,1,0,1,1,0,1]
data1 = [1,1,1,0,0,1,0,1]
data2 = [1,0,1,0,1,1,0,1]
df = pd.DataFrame({'test': days, 'col1': data, 'col2': data1, 'col3': data2})
df = df.set_index('test')

as example: enter image description here

Upvotes: 1

Views: 471

Answers (1)

Albo
Albo

Reputation: 1644

You can simply use imshow from plotly:

# using your data sample:

fig = px.imshow(df)
fig.show()

this gives you:

enter image description here

With a little bit more styling:

fig = px.imshow(df, width=300, height=500,
                labels=dict(x="Columns", y="Days"),
                x=df.columns,
                y=df.index,
               )
fig.show()

you can get:

enter image description here

Or horizontal layout, with no coloraxis and another colorscale (Viridis), if you like his better. For further details see the API reference

fig = px.imshow(df.T, width=500, height=300,
                labels=dict(x="Columns", y="Days"),
                x=df.T.columns,
                y=df.T.index,
                color_continuous_scale = 'Viridis',
               )
fig.update_layout(coloraxis_showscale=False)
fig.show()

enter image description here

Upvotes: 1

Related Questions