Bring Drag & Drop to Dash with Dashboard Engine. 💫 Learn how at our next webinar!

Plot 2Dimensional arrays

Sorry for the noob question. I’m new to Plotly.
Given the below 2 two-dimensional arrays of ones and zeros:

arra1 = array([[0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0],
   [0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
   [0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
   [0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0],
   [0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
   [0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0]])
arr2 = array([[0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
   [0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
   [0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
   [0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0],
   [0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
   [0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0],
   [0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0]]) 

For the purpose of comparison, using Plotly, how to plot (in any kind of plot) the 2 arrays each on a subplot?

import plotly.graph_objects as go
import numpy as np

array1 = np.array([[0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0],
   [0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
   [0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
   [0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0],
   [0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
   [0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0]])
array2 = np.array([[0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
   [0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
   [0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
   [0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0],
   [0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
   [0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0],
   [0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0]]) 

# create figure
fig = go.Figure()

# Add surface trace
fig.add_trace(go.Surface(z=array1, colorscale="Reds", opacity=0.8))

fig.add_trace(go.Surface(z=array2, colorscale="Greens", opacity=0.8))

fig.show()
1 Like

Thank you @pepo83. Actually, the arrays are multi-label classes, the labels on the row index of the below data frame. i.e each row has 0 when label is not present and 1 when label is present:

	nosp	cons_turb	cons_sonor	klinker	explosief	fricatief	nasaal_cons	nasaal_vow	lateraal	labiaal	coronaal	dorsaal	frontaal	centraal	achteraan
0	1	0	0	0	0	0	0	0	0	0	0	0	0	0	0
1	0	0	0	1	0	0	0	0	0	0	0	0	0	1	0
2	1	0	0	0	0	0	0	0	0	0	0	0	0	0	0
3	1	0	0	0	0	0	0	0	0	0	0	0	0	0	0
4	0	0	0	1	0	0	0	0	0	0	0	0	0	1	0
5	1	0	0	0	0	0	0	0	0	0	0	0	0	0	0
6	0	0	0	1	0	0	0	0	0	0	0	0	0	1	0
7	0	0	0	1	0	0	0	0	0	0	0	0	0	1	0
8	0	0	0	1	0	0	0	0	0	0	0	0	0	1	0

Is there a way to plot this data frame to show the the changes of each label?

Thank you

import plotly.graph_objects as go
import numpy as np
import pandas as pd

array1 = np.array([ [0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0],
                    [0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
                    [0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1],
                    [0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0],
                    [0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0],
                    [0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0]
                  ])

columns=["nosp", "cons_turb", "cons_sonor",	"klinker",	"explosief",	"fricatief"	,"nasaal_cons"	,"nasaal_vow",
         "ateraal",	"labiaal"	,"coronaal", "saal",	"frontaal",	"centraal",	"achteraan"]

df=pd.DataFrame(array1, columns = columns)

# create figure
fig = go.Figure()

# Add surface trace
fig.add_trace(go.Surface(z=df, x=df.columns, y=df.index.values ,colorscale="Reds", opacity=0.8))

fig.update_layout(
        scene = {
            "xaxis": {"nticks": len(df.columns)},
            "zaxis": {"nticks": len(df.index.values)},
        })

fig.show()
1 Like

Thank you @pepo83.

it seems that adding more columns doesn’t show in the graph as shown in screenshot

columns=["nosp", "cons_turb", "cons_sonor", "klinker", "explosief", "fricatief", "nasaal_cons", "nasaal_vow", "lateraal", "labiaal", "coronaal", "dorsaal", "frontaal", "centraal", "achteraan", "hoge_tong", "gem_tong", "lage_tong", "ronde_lip", "diphg_lone", "alveolar"]

array1 = array([[0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0],
       [0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
       [0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]])

Thank you

@lima, @pepo83

The most suitable trace type for binary data is heatmap , not surface!

from plotly.subplots import make_subplots
import plotly.graph_objects as go
fig=make_subplots(cols=2, rows=1, horizontal_spacing=0.035)
colorsc= [[0, "#000000"], [1, "#ffffff"]]
fig.add_trace(go.Heatmap(z=arr1, coloraxis="coloraxis"), 1, 1)
fig.add_trace(go.Heatmap(z=arr2, coloraxis="coloraxis"), 1, 2)
fig.update_layout(width=800, height=300, coloraxis_colorscale=colorsc, 
                  coloraxis_showscale=False,
                  yaxis_autorange="reversed", yaxis2_autorange="reversed")

2 Likes