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

Ben

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

enter image description here

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

enter image description here

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()
Hamzah

You can try this:

import pandas as pd
import plotly.graph_objects as go

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'
)

Then, I use heatmaps which have more control over the gaps between tiles:

fig = go.Figure(data=go.Heatmap(
                    z=df_wide.values,
                    x=df.col.unique(),
                    y=df.row.unique(),
                    text=df['animal'].values.reshape(df_wide.values.shape),
                    texttemplate="%{text}",
                    textfont={"size":15,"color":'red'},
                    colorscale='gray',
                    showscale=False,
                    ygap = 3, 
                    xgap = 3)
               )

fig.show()

enter image description here

I use ygap = 3, xgap = 3 to add gaps between tiles.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to Create Grid/Tile View?

How to change color of "tile grout" in R waffle plot to match background

How can I create a tile layout in Bootstrap

ggplot add text inside each tile of geom tile

How can I plot two geom_tile next to each-other so they align as in a Heatmap, using ggplot2?

Variable Size of geom_tile plot

How to create a simple tile in wpf?

Python - Minesweeper using Numpy: How do I count bombs around each tile?

Create geom_tile plot with two dataframes

How can I get the tile gameObject by position?

How do I create weighted scatter plot / bubble chart with color gradient using plotly R

How can i create a chess board using buttons where each square has a defined x and y value?

When using rasterize=True with datashader, how do I get transparency where count=0 to see the underlying tile?

I have a matrix in which each element is a label and a value. How can I represent it so that the label gives the color and the value the intensity?

Plotly: How to create a line plot of a time series variable that has a multiple-color label?

Plotly: How to create a line plot with different style and color for each variable?

How do I add a separate legend for each variable in geom_tile?

How can I create a custom live tile in Windows Phone 8.1?

How can I change the color of label with * in text

Can I change the background color of the primary tile in WinRT?

How can I create a bar-chart which has multiple bars for each label in MS Excell?

How can I create a label text with shadow?

GGplot heatmap has 2 labels on each tile

How can I fill color the tile leaflet?

How can I merge <div class="tile"> with another <div class="tile">?

How can i add multiple expand tile with checkbox list tile using Json in flutter

How to plot geom_tile() with offset geom_text() labels indicated using arrows?

How can I label a Matplotlib plot using a mix of a variable and string?

How can I manually color each point in my scatter-plot in plotly?