Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change the text color of cells in Plotly table based on value (string)

I have the following script that turns a pandas dataframe into an interactive html table with Plotly:


goals_output_df = pd.read_csv(os.path.join(question_dir, "data/output.csv"))

fig = go.Figure(data=[go.Table(
    columnwidth = [15,170,15,35,35],
    header=dict(values=['<b>' + x + '</b>' for x in list(goals_output_df.columns)],
                # fill_color='#b9e2ff',
                line_color='darkslategray',
                align='center',
                font=dict(color='black', family="Lato", size=20),
                height=30
                ),
    cells=dict(values=[goals_output_df[column] for column in goals_output_df.columns],
            #    fill_color='#e6f2fd',
               line_color='darkslategray',
               align='left',
               font=dict(color='black', family="Lato", size=20),
               height=30
               ))
])

fig.update_layout(
title="<b>Output summary for %s</b>"%question.strip('question'),
font=dict(
    family="Lato",
    size=18,
    color="#000000"))

fig.write_html(os.path.join(question_dir, "results/output.html"))

The table contains a column named "Output" which can have one of three values for each row: "YES", "NO" and "BORDERLINE".

I want to be able to change the color of the text in the "Output" column such that "YES" is green, "NO" is red and "BORDERLINE" is blue.

Any idea how I could do that? Plotly documentation doesn't seem to help.

like image 958
Paul Miller Avatar asked May 08 '20 19:05

Paul Miller


2 Answers

It's kind of implicit on this section cell-color-based-on-variable. Here what I'll do in your case.

Data

import pandas as pd
import plotly.graph_objects as go
df = pd.DataFrame({"name":["a", "b", "c", "d"],
                   "value":[100,20,30,40],
                   "output":["YES", "NO", "BORDERLINE", "NO"]})

map_color = {"YES":"green", "NO":"red", "BORDERLINE":"blue"}

df["color"] = df["output"].map(map_color)

cols_to_show = ["name", "value", "output"]

Where cols_to_show are the only columns you want to show in your table.

Fill color

Here you want to have a standard cell background in all columns but the output one

fill_color = []
n = len(df)
for col in cols_to_show:
    if col!='output':
        fill_color.append(['#e6f2fd']*n)
    else:
        fill_color.append(df["color"].to_list())

Table

data=[go.Table(
#     columnwidth = [15,20,30],
    header=dict(values=[f"<b>{col}</b>" for col in cols_to_show],
                # fill_color='#b9e2ff',
                line_color='darkslategray',
                align='center',
                font=dict(color='black', family="Lato", size=20),
                height=30
                ),
    cells=dict(values=df[cols_to_show].values.T,
               fill_color=fill_color,
               line_color='darkslategray',
               align='left',
               font=dict(color='black', family="Lato", size=20),
               height=30
               ))
]

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

enter image description here

UPDATE

Change text color

In case you want to change text color

text_color = []
n = len(df)
for col in cols_to_show:
    if col!='output':
        text_color.append(["black"] * n)
    else:
        text_color.append(df["color"].to_list())

and

data=[go.Table(
#     columnwidth = [15,20,30],
    header=dict(values=[f"<b>{col}</b>" for col in cols_to_show],
                # fill_color='#b9e2ff',
                line_color='darkslategray',
                align='center',
                font=dict(color='black', family="Lato", size=20),
                height=30
                ),
    cells=dict(values=df[cols_to_show].values.T,

               line_color='darkslategray',
               align='left',
               font=dict(color=text_color, family="Lato", size=20),
               height=30
               ))
]

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

enter image description here

like image 86
rpanai Avatar answered Nov 18 '22 12:11

rpanai


You could add an if statement in the font_color section:

  import pandas as pd
  import plotly
  import plotly.graph_objs as go
  df = pd.DataFrame({"name":["a", "b", "c", "d"],
            "value":[100,20,30,40],
            "output":["YES", "NO", "BORDERLINE", "NO"]})

  fig = go.Figure(data=[go.Table(
            columnwidth = [30,30,30],
            header=dict(values=["Name","Value", "Output"],
            # fill_color='#b9e2ff',
            line_color='darkslategray',
            align='center',
            font=dict(color='black', family="Lato", size=20),
            height=30
            ),
    cells=dict(values=[df.name, df.value, df.output],
        #    fill_color='#e6f2fd',
           line_color='darkslategray',
           align='left',
           font_color=['darkslategray','darkslategray',['green' if x == "YES" else       
           "red" if x == "NO" else "blue" if x == "BORDERLINE" else 'darkslategray' 
           for x in list(df.output)]],
           height=30
           ))
     ])


     fig.show()

This will produce the table: enter image description here

like image 2
Sanch Avatar answered Nov 18 '22 14:11

Sanch