Imshow with double yaxis

I have a wide matrix that I render using plotly express. Let’s say:

import plotly.express as px
data=[[1, 25, 30, 50, 1], [20, 1, 60, 80, 30], [30, 60, 1, 5, 20]]
fig = px.imshow(data,
                labels=dict(x="Day of Week", y="Time of Day", color="Productivity"),
                x=['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'],
                y=['Morning', 'Afternoon', 'Evening']
               )
fig.update_xaxes(side="top")

fig.layout.height = 500
fig.layout.width = 500

fig.show()

For enhancing readability, I would like to repeat (or add an identical) yaxis on the right side of the matrix. Is there a way of doing this?

hi @aaa
As per our documentation,

At this time, Plotly Express does not support multiple Y axes on a single figure. To make such a figure, use the make_subplots() function in conjunction with graph objects.

I was able to build a multi Y axes with Graph Objects.

import plotly.graph_objects as go
from plotly.subplots import make_subplots

# Create figure with secondary y-axis
fig = make_subplots(specs=[[{"secondary_y": True}]])

data=[[1, 25, 30, 50, 1], [20, 1, 60, 80, 30], [30, 60, 1, 5, 20]]

fig.add_trace(go.Heatmap(
                z=data,
                x=['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'],
                y=['Morning', 'Afternoon', 'Evening']
),secondary_y=False)
fig.add_trace(go.Heatmap(
                z=data,
                x=['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'],
                y=['Morning', 'Afternoon', 'Evening']
),secondary_y=True)

fig.update_xaxes(side="top")
fig.update_layout(xaxis_title="Day of Week", yaxis_title="Time of Day")

fig.show()

I’m not sure if adding the same trace twice is best practice, though. So take this code with a grain of salt.

2 Likes