Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change cell color if the two values of this cell have opposite signs in Pretty tables in Julia

How to change cell color if the two values of this cell have opposite signs in Pretty tables in Julia below is my code and the table is attached.

names = string.(-1/1:1/4:1/1) pretty_table(AStrings , header = ([-1,-3/4, -1/2, -1/4, 0, 1/4, 1/2, 3/4, 1]), row_names= names)

enter image description here

like image 989
Hana Avatar asked Sep 12 '25 14:09

Hana


2 Answers

After digging through the docs:

using PrettyTables

# making some demo data
data = collect(zip(rand([-1.0,1.0],5,5),rand([-1.0,1.0],5,5)))
names = [-1, -1/2, 0, 1/2, 1]

# this is the Highlighter which makes text red when signs differ.
# signs differ if their product is negative.
hl = Highlighter((d,i,j)->d[i,j][1]*d[i,j][2] < 0, crayon"red")

Then the Highlighter is used as follows:

pretty_table(data ; header = names, row_names= names, highlighters=hl)

enter image description here

Well, colors don't go through in text, so put an image of result.

like image 174
Dan Getz Avatar answered Sep 14 '25 13:09

Dan Getz


This answer is beyond what was asked by the OP, but hopefully would be informative. In addition to Dan Getz's answer, one can apply more than one rule for highlighting the values. For example, if you want to make pairs with positive value green besides the first rule, you can pass a tuple of Highlighter to the highlighters keyword argument.

I will use Dan's example to show you the results:

julia> hl = (
           Highlighter((d,i,j)->d[i,j][1]*d[i,j][2]<0, crayon"red"),
           Highlighter((d,i,j)->d[i,j][1]>0 && d[i,j][2]>0, crayon"green")
       )

The result of pretty_table(data; header=names, row_names=names, highlighters=hl) would be:

enter image description here

like image 42
Shayan Avatar answered Sep 14 '25 14:09

Shayan