haneulkim
haneulkim

Reputation: 4928

Plotly: Reverse heatmap ytick order

I use to create heatmaps with seaborn however I fell in love with plotly and ever since I am trying to change all the visualizations to plotly.

I've successfully created a heatmap however order of yaxis does not change when I create heatmap even though I try both ways.

Here is MRE:

import plotly.graph_objects as go

y = ["a", "b", "c", "d", "e"]
new_y = ["e", "d", "c", "b","a"]

data = [
    go.Heatmap(
        z=[[6,5,4,3,1], [5,4,3,2,np.nan], [6,4,3,np.nan, np.nan], [5,3,np.nan,np.nan,np.nan],[4,np.nan,np.nan,np.nan,np.nan]],
        x=[1,2,3,4,5],
        y=new_y,
        colorscale="YlOrRd"
    )
]

layout = go.Layout(
    title = "title"
)

fig = go.Figure(data=data, layout=layout)
fig.show()

Doesn't matter which list I insert for y variable. y or y_new it gives me same heatmap. enter image description here

How can I reverse yaxis so that "e" is at the top?

Also if anyone know name of colorscale they use in seaborn default heatmap let me know because I love that coloring.

Thanks in advance.

Upvotes: 0

Views: 1824

Answers (2)

user11989081
user11989081

Reputation: 8663

As the y-axis is categorical you should include yaxis_type = "category" in the layout. The built-in color scales are listed at this link: https://plotly.com/python/builtin-colorscales/.

import plotly.graph_objects as go
import numpy as np

data = go.Heatmap(
        z=[[6, 5, 4, 3, 1], [5, 4, 3, 2, np.nan], [6, 4, 3, np.nan, np.nan], [5, 3, np.nan, np.nan, np.nan],[4, np.nan, np.nan, np.nan, np.nan]],
        x=[1, 2, 3, 4, 5],
        y=["a", "b", "c", "d", "e"],
        colorscale="Inferno"
)

layout = go.Layout(yaxis_type="category")

fig = go.Figure(data=data, layout=layout)

fig.show()

Upvotes: 2

r-beginners
r-beginners

Reputation: 35205

To reverse the y-axis in the layout, add the following settings.

layout = go.Layout(
    title = "title",
    yaxis=dict(visible=True,autorange='reversed')
)

Upvotes: 4

Related Questions