# Is any way to print DataFrames using heat maps as background?

**URL:** https://discourse.julialang.org/t/is-any-way-to-print-dataframes-using-heat-maps-as-background/77744
**Category:** General Usage
**Tags:** dataframes, vscode, notebooks, prettytables
**Created:** [March 11, 2022, 11:36am UTC](https://discourse.julialang.org/t/is-any-way-to-print-dataframes-using-heat-maps-as-background/77744 "2022-03-11T11:36:26Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![Dan\_Micsa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan_micsa/32/30233_2.png) [@Dan\_Micsa](https://discourse.julialang.org/u/Dan_Micsa)
#### Post date: [March 11, 2022, 11:36am UTC](https://discourse.julialang.org/t/is-any-way-to-print-dataframes-using-heat-maps-as-background/77744/1 "2022-03-11T11:36:27Z")

</div>

I want to print in various notebooks dataframes setting the background of each cell. This is possible in Python. Here is an example using a heatmap.

Something that looks like:

![image](https://global.discourse-cdn.com/julialang/original/3X/5/0/5080e94e7163f05272fb95565e61a072ab31b383.png)

Is some library or code out there to do this? I know how to do this using plotting, I need something different.

---

<div class="post-metadata">

### Author: ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)
#### Post date: [March 11, 2022, 1:01pm UTC](https://discourse.julialang.org/t/is-any-way-to-print-dataframes-using-heat-maps-as-background/77744/2 "2022-03-11T13:01:08Z")

</div>

If anything this would probably be a job for PrettyTables HTML backend

---

<div class="post-metadata">

### Author: ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)
#### Post date: [March 11, 2022, 1:03pm UTC](https://discourse.julialang.org/t/is-any-way-to-print-dataframes-using-heat-maps-as-background/77744/3 "2022-03-11T13:03:13Z")

</div>

It should be also possible with text/plain. @Ronis_BR is the best person to comment.

---

<div class="post-metadata">

### Author: ![Dan\_Micsa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan_micsa/32/30233_2.png) [@Dan\_Micsa](https://discourse.julialang.org/u/Dan_Micsa)
#### Post date: [March 11, 2022, 1:10pm UTC](https://discourse.julialang.org/t/is-any-way-to-print-dataframes-using-heat-maps-as-background/77744/4 "2022-03-11T13:10:45Z")

</div>

It will be cripple by various consoles constrains on various platforms. I’m more interested by decent printing in various notebooks around like in Python.

---

<div class="post-metadata">

### Author: ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)
#### Post date: [March 11, 2022, 1:18pm UTC](https://discourse.julialang.org/t/is-any-way-to-print-dataframes-using-heat-maps-as-background/77744/5 "2022-03-11T13:18:31Z")

</div>

Well, the

> PrettyTables HTML backend

should work just fine in Jupyter notebooks.

---

<div class="post-metadata">

### Author: ![Dan\_Micsa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan_micsa/32/30233_2.png) [@Dan\_Micsa](https://discourse.julialang.org/u/Dan_Micsa)
#### Post date: [March 11, 2022, 1:47pm UTC](https://discourse.julialang.org/t/is-any-way-to-print-dataframes-using-heat-maps-as-background/77744/6 "2022-03-11T13:47:00Z")

</div>

Very good idea! At least in VSCode works great.

```julia
using PrettyTables

data = Any[1 false 1.0 0x01
    2 true 2.0 0x02
    3 false 3.0 0x03
    4 true 4.0 0x04
    5 false 5.0 0x05
    6 true 6.0 0x06]

h1 = Highlighter((data, i, j) -> j in (1, 3, 4) && data[i, j] == 1, bold = true, background = 0x220044)
h2 = Highlighter((data, i, j) -> j in (1, 3, 4) && data[i, j] == 2, bold = true, background = 0x440088)
h3 = Highlighter((data, i, j) -> j in (1, 3, 4) && data[i, j] == 3, bold = true, background = 0x6600AA)
h4 = Highlighter((data, i, j) -> j in (1, 3, 4) && data[i, j] == 4, bold = true, background = 0x8800CC)

pretty_table(data, highlighters = (h1, h2, h3, h4))

```

![image](https://global.discourse-cdn.com/julialang/original/3X/7/c/7c88d9e48014dc3cdc38b420d1b6b17254b60883.png)

---

<div class="post-metadata">

### Author: ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)
#### Post date: [March 11, 2022, 1:48pm UTC](https://discourse.julialang.org/t/is-any-way-to-print-dataframes-using-heat-maps-as-background/77744/7 "2022-03-11T13:48:59Z")

</div>

Hi @Dan_Micsa !

You can do this using PrettyTables with the following code:

```julia
using Colors
using ColorSchemes
using DataFrames
using PrettyTables

# Function that defines the decoration in each cell.
function colormap_decoration(h, data, i, j)
    max = maximum(eachcol(df)) |> maximum
    min = minimum(eachcol(df)) |> minimum

    # Get the color of the current cell.
    color = get(
        colorschemes[:coolwarm],
        data[i, j],
        (min, max)
    )

    # Convert to hexadecimal.
    hex_color = color |> hex

    # Add to the decoration.
    return HTMLDecoration(background = "#" * hex_color)
end

# Create a ramdom DataFrames.
df = DataFrame(rand(20, 5), :auto)

# Define the highlighter that will add the color to each cell.
hl = HTMLHighlighter(
    (data, i, j) -> true,
    colormap_decoration
)

# Print the table.
pretty_table(df, highlighters = (hl,), backend = :html)

```

Leading to:

 ![Captura de Tela 2022-03-11 às 10.48.08](https://global.discourse-cdn.com/julialang/original/3X/a/e/aeb4cf418671355e1950a9f64416cad07f1c4f3b.png)

I used the `master` branch of PrettyTables.jl, but it should work in the released version. Notice that PrettyTables is passing thought a huge rewrite and next version should be some breaking changes.

Side note: this code is not optimized (max and min values are being computed when analyzing each cell), but it works 🙂

---

<div class="post-metadata">

### Author: ![Dan\_Micsa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan_micsa/32/30233_2.png) [@Dan\_Micsa](https://discourse.julialang.org/u/Dan_Micsa)
#### Post date: [March 11, 2022, 1:54pm UTC](https://discourse.julialang.org/t/is-any-way-to-print-dataframes-using-heat-maps-as-background/77744/8 "2022-03-11T13:54:16Z")

</div>

I close this case thank you all for answers!

 ![image](https://global.discourse-cdn.com/julialang/original/3X/a/f/af79fb92601f686f9711203bbafe4aa764a9f673.png)

---

<div class="post-metadata">

### Author: ![Dan\_Micsa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan_micsa/32/30233_2.png) [@Dan\_Micsa](https://discourse.julialang.org/u/Dan_Micsa)
#### Post date: [March 11, 2022, 1:59pm UTC](https://discourse.julialang.org/t/is-any-way-to-print-dataframes-using-heat-maps-as-background/77744/9 "2022-03-11T13:59:49Z")

</div>

thank you very much for this example.

---

<div class="post-metadata">

### Author: ![Dan\_Micsa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan_micsa/32/30233_2.png) [@Dan\_Micsa](https://discourse.julialang.org/u/Dan_Micsa)
#### Post date: [March 11, 2022, 2:30pm UTC](https://discourse.julialang.org/t/is-any-way-to-print-dataframes-using-heat-maps-as-background/77744/10 "2022-03-11T14:30:46Z")

</div>

> > max and min values are being computed when analyzing each cell

I saw that “attrocity” but I was a gentleman. 😄

---

<div class="post-metadata">

### Author: ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)
#### Post date: [March 11, 2022, 2:54pm UTC](https://discourse.julialang.org/t/is-any-way-to-print-dataframes-using-heat-maps-as-background/77744/11 "2022-03-11T14:54:15Z")

</div>

😅

---

<div class="post-metadata">

### Author: ![Dan\_Micsa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan_micsa/32/30233_2.png) [@Dan\_Micsa](https://discourse.julialang.org/u/Dan_Micsa)
#### Post date: [March 11, 2022, 10:33pm UTC](https://discourse.julialang.org/t/is-any-way-to-print-dataframes-using-heat-maps-as-background/77744/12 "2022-03-11T22:33:00Z")

</div>

Another question, if you have a bit of time, how can I pass min and max for color map in your example w/o the ugly globals?

```julia
function colormap_decoration(h, data, i, j)
    max = maximum(eachcol(df)) |> maximum
    min = minimum(eachcol(df)) |> minimum
...

```

---

<div class="post-metadata">

### Author: ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)
#### Post date: [March 12, 2022, 12:08am UTC](https://discourse.julialang.org/t/is-any-way-to-print-dataframes-using-heat-maps-as-background/77744/13 "2022-03-12T00:08:41Z")

</div>

The first and easiest is to create a closure:

```julia
using Colors
using ColorSchemes
using DataFrames
using PrettyTables

function print_df(df)
    max = maximum(eachcol(df)) |> maximum
    min = minimum(eachcol(df)) |> minimum

    # Function that defines the decoration in each cell.
    function colormap_decoration(h, data, i, j)
        # Get the color of the current cell.
        color = get(
            colorschemes[:coolwarm],
            data[i, j],
            (min, max)
        )

        # Convert to hexadecimal.
        hex_color = color |> hex

        # Add to the decoration.
        return HTMLDecoration(background = "#" * hex_color)
    end

    # Define the highlighter that will add the color to each cell.
    hl = HTMLHighlighter(
        (data, i, j) -> true,
        colormap_decoration
    )

    # Print the table.
    pretty_table(df, highlighters = (hl,), backend = :html)
end

# Create a ramdom DataFrames.
df = DataFrame(rand(20, 5), :auto)
print_df(df)

```

The second is to define your own highlighter structure:

```julia
using Colors
using ColorSchemes
using DataFrames
using PrettyTables

struct MyHighlighter{T}
    f::Function
    fd::Function
    min::T
    max::T
end

# Function that defines the decoration in each cell.
function colormap_decoration(h, data, i, j)
    # Get the color of the current cell.
    color = get(
        colorschemes[:coolwarm],
        data[i, j],
        (h.min, h.max)
    )

    # Convert to hexadecimal.
    hex_color = color |> hex

    # Add to the decoration.
    return HTMLDecoration(background = "#" * hex_color)
end

function print_df(df)
    max = maximum(eachcol(df)) |> maximum
    min = minimum(eachcol(df)) |> minimum

    # Define the highlighter that will add the color to each cell.
    hl = MyHighlighter(
        (data, i, j) -> true,
        colormap_decoration,
        min,
        max
    )

    # Print the table.
    pretty_table(df, highlighters = (hl,), backend = :html)
end

# Create a ramdom DataFrames.
df = DataFrame(rand(20, 5), :auto)
print_df(df)

```

---

<div class="post-metadata">

### Author: ![Dan\_Micsa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan_micsa/32/30233_2.png) [@Dan\_Micsa](https://discourse.julialang.org/u/Dan_Micsa)
#### Post date: [March 12, 2022, 12:39am UTC](https://discourse.julialang.org/t/is-any-way-to-print-dataframes-using-heat-maps-as-background/77744/14 "2022-03-12T00:39:59Z")

</div>

Thank you. I have over 40y of programming and 2 days of Julia. 🙂

I will play with it. New concepts for me. In C++ I have to write a local class, a function is not possible inside.

---

<div class="post-metadata">

### Author: ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)
#### Post date: [March 12, 2022, 6:40pm UTC](https://discourse.julialang.org/t/is-any-way-to-print-dataframes-using-heat-maps-as-background/77744/15 "2022-03-12T18:40:17Z")

</div>

This is what I get.  
Do I need to set something on VS Code?

```julia
julia> print_df(df)
<!DOCTYPE html>
<html>
<meta charset="UTF-8">
<style>
  table, td, th {
      border-collapse: collapse;
      font-family: sans-serif;
  }

  td, th {
      border-bottom: 0;
      padding: 4px
  }

```

---

<div class="post-metadata">

### Author: ![Dan\_Micsa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan_micsa/32/30233_2.png) [@Dan\_Micsa](https://discourse.julialang.org/u/Dan_Micsa)
#### Post date: [March 12, 2022, 6:47pm UTC](https://discourse.julialang.org/t/is-any-way-to-print-dataframes-using-heat-maps-as-background/77744/16 "2022-03-12T18:47:23Z")

</div>

I think this is using HTML back end in a console mode. HTML should be used for conversion to HTML or notebooks that can render it graphically.

try this if you using the text like method:

```julia
using PrettyTables

data = Any[1 false 1.0 0x01
    2 true 2.0 0x02
    3 false 3.0 0x03
    4 true 4.0 0x04
    5 false 5.0 0x05
    6 true 6.0 0x06]

h1 = Highlighter((data, i, j) -> j in (1, 3, 4) && data[i, j] == 1, bold = true, background = 0x220044)
h2 = Highlighter((data, i, j) -> j in (1, 3, 4) && data[i, j] == 2, bold = true, background = 0x440088)
h3 = Highlighter((data, i, j) -> j in (1, 3, 4) && data[i, j] == 3, bold = true, background = 0x6600AA)
h4 = Highlighter((data, i, j) -> j in (1, 3, 4) && data[i, j] == 4, bold = true, background = 0x8800CC)

#pretty_table(data; highlighters = (h))
pretty_table(data; highlighters = (h1, h2, h3, h4))
#pretty_table(data; highlighters = (h1, h2, h3, h4), backend = Val(:html))

```

More information here: [Home · Pretty Tables](https://ronisbr.github.io/PrettyTables.jl/stable/)

---

<div class="post-metadata">

### Author: ![Dan\_Micsa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan_micsa/32/30233_2.png) [@Dan\_Micsa](https://discourse.julialang.org/u/Dan_Micsa)
#### Post date: [March 12, 2022, 10:27pm UTC](https://discourse.julialang.org/t/is-any-way-to-print-dataframes-using-heat-maps-as-background/77744/18 "2022-03-12T22:27:48Z")

</div>

[Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR),

This feels like a bug in the PrettyTables.Crayon. I can’t set both fc and bc. If i set only one of them it works.

```julia
using Colors
using ColorSchemes
using DataFrames
using PrettyTables

function pp(df, min_ = nothing, max_ = nothing, isCentered = true, gradientCentered = :coolwarm, gradient = :YlGn_9)    
    if isnothing(min_)
        min_ = minimum(eachcol(df)) |> minimum
    end
    
    if isnothing(max_)
        max_ = maximum(eachcol(df)) |> maximum
    end

    if isCentered && min_ < -0.00001
        min_ = min(min, -max)
        max_ = max(-min, max)
        gradient = gradientCentered
    end

    function colormapDecorationHTML(h, data, i, j)
        color = get(colorschemes[gradient], data[i, j], (min_, max_))
        return HTMLDecoration(color = "#" * (color |> hex), background = convert(Bool, i % 2) ? "#222222" : "#111111")
        #return HTMLDecoration(background = "#" * (color |> hex), color = convert(Bool, i % 2) ? "#222222" : "#111111")
    end

    function colormapDecorationText(h, data, i, j)
        color = get(colorschemes[gradient], data[i, j], (min_, max_))
        color2 = "0x" * (color |> hex)
        color = parse(UInt32, color2)
        #return Crayon(;background = color)
        #return Crayon(;foreground = color)
        #return Crayon(;foreground = 0x888888, background = color)
        return Crayon(;foreground = 0xFFFF00, background = 0x0000FF)#! I can't set both fc and bc !
    end

    # Define the highlighter that will add the color to each cell.
    hlHTML = HTMLHighlighter(f = (data, i, j) -> true, fd = colormapDecorationHTML)
    hlText = Highlighter(f = (data, i, j) -> true, fd = colormapDecorationText)
    
    # Print the table.
    pretty_table(df, highlighters = (hlHTML), backend = :html, formatters = ft_printf("%.4f"))
    pretty_table(df, highlighters = (hlText), formatters = ft_printf("%.4f"))
end

# Create a random DataFrames.
df = DataFrame(rand(10, 5), :auto)
pp(df)

```

---

<div class="post-metadata">

### Author: ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)
#### Post date: [March 13, 2022, 2:08pm UTC](https://discourse.julialang.org/t/is-any-way-to-print-dataframes-using-heat-maps-as-background/77744/19 "2022-03-13T14:08:34Z")

</div>

Hi @Dan_Micsa !

Sorry for the delay, I missed the notification…

I run this code and everything seems to be working correctly:

 ![Captura de Tela 2022-03-13 às 11.08.16](https://global.discourse-cdn.com/julialang/original/3X/1/c/1ce51ea156fba413cb24973e236fdbbf08feb3cb.png)

What is the problem in your case?

---

<div class="post-metadata">

### Author: ![Dan\_Micsa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan_micsa/32/30233_2.png) [@Dan\_Micsa](https://discourse.julialang.org/u/Dan_Micsa)
#### Post date: [March 13, 2022, 2:14pm UTC](https://discourse.julialang.org/t/is-any-way-to-print-dataframes-using-heat-maps-as-background/77744/20 "2022-03-13T14:14:15Z")

</div>

This code looks OK in console but not in VS Code. I believe I disturb you with things that are not from the PrettyTable library.

 ![image](https://global.discourse-cdn.com/julialang/original/3X/1/e/1ef5ad53f59a7a99e169992bc9323a36c08df855.png)

---

<div class="post-metadata">

### Author: ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)
#### Post date: [March 13, 2022, 2:20pm UTC](https://discourse.julialang.org/t/is-any-way-to-print-dataframes-using-heat-maps-as-background/77744/21 "2022-03-13T14:20:45Z")

</div>

> This code looks OK in console but not in VS Code. I believe i disturb you with thinks that are not from the PrettyTable library.

No problem! Please, feel free to contact me anytime 🙂

It seems to me it is a problem of neither PrettyTables nor Crayons. I believe it might be something related to the console in VSCode. Can you execute the following code in Julia inside the VSCode console please:

```julia
julia> println("\e[38;2;255;255;0;48;2;0;0;255mThis must have blue background\e[0m")
julia> println("\e[33;44mThis must have blue background\e[0m")

```

[Next page](https://discourse.julialang.org/t/is-any-way-to-print-dataframes-using-heat-maps-as-background/77744.md?page=2)
