简体   繁体   中英

How can I create a tile plot using plotly, where each tile has a text label and a variable color intensity?

Here's my data

import pandas as pd

df = pd.DataFrame({
    'row': ['A','A','A','B','B','B'],
    'col': [1,2,3,1,2,3],
    'val': [0.1,0.9,0.2,0.5,0.2,0.2],
    'animal': ['duck', 'squirrel', 'horse', 'cow', 'pig', 'cat']
})


df
  row  col  val    animal
0   A    1  0.1      duck
1   A    2  0.9  squirrel
2   A    3  0.2     horse
3   B    1  0.5       cow
4   B    2  0.2       pig
5   B    3  0.2       cat

I would like to make a plot like this

在此处输入图像描述

But the closest I can get (using imshow ) is this

在此处输入图像描述

Note : imshow doesn't provide a border between tiles (as far as I'm aware) which is also important to me.

My attempt

import pandas as pd
import plotly.express as px

df = pd.DataFrame({
    'row': ['A','A','A','B','B','B'],
    'col': [1,2,3,1,2,3],
    'val': [0.1,0.9,0.2,0.5,0.2,0.2],
    'animal': ['duck', 'squirrel', 'horse', 'cow', 'pig', 'cat']
})

df_wide = pd.pivot_table(
    data=df,
    index='row',
    columns='col',
    values='val',
    aggfunc='first'
)

fig = px.imshow(
    img=df_wide,
    zmin=0,
    zmax=1,
    origin="upper",
    color_continuous_scale='gray'
)
fig.update_layout(coloraxis_showscale=False)
fig.show()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM