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)
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)
Well, colors don't go through in text, so put an image of result.
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With