4

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

Hana
  • 101
  • 5
  • BTW try to make code in question runnable. Screenshots are not so helpful when trying to get the situation replicated. – Dan Getz Sep 19 '22 at 11:06

2 Answers2

3

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.

Dan Getz
  • 17,002
  • 2
  • 23
  • 41
  • Thank you so much for your reply. However, I got the following error ERROR: LoadError: MethodError: no method matching isless(::String, ::Int64) Closest candidates are: isless(::Union{StatsBase.PValue, StatsBase.TestStat}, ::Real) at C:\Users\Hind\.julia\packages\StatsBase\XgjIN\src\statmodels.jl:90 isless(::AbstractString, ::AbstractString) at strings/basic.jl:344 isless(::ReverseDiff.TrackedReal, ::Integer) at C:\Users\Hind\.julia\packages\ReverseDiff\5MMPp\src\derivatives\scalars.jl:42 ... The AStrings = fill("", 9, 9) is defined as this Can you help ? – Hana Sep 19 '22 at 12:26
  • The error is caused by trying to compare a string and a number. Some of your data is converted to strings perhaps using `string.(...)`. Try to make any conversions to strings as late as possible, just before printing, or not at all, since `pretty_print` converts to strings itself. [in any case, it will be easier to find the problem with the actual code being run and not just the error] – Dan Getz Sep 19 '22 at 12:30
  • In the question `AStrings` is the data to print. It seems to be already in string format, which is mostly useless for the Highlighter. If you have another variable with the raw data, it might be used inside the Highlighter, as some hack. – Dan Getz Sep 19 '22 at 12:32
  • I see I saved it as a string because I want to save the first number then "," then the second number. However, these numbers were saved in 2 vectors b4 that c= zeros(81) c2 = zeros(81). Now I should write hl = Highlighter((c,c2,i,j)->c,c2[i,j][1]* c,c2[i,j][2] < 0, crayon"red") pretty_table(c,c2 , header = ([-1,-3/4, -1/2, -1/4, 0, 1/4, 1/2, 3/4, 1]), row_names= names , highlighters=hl) or how ? – Hana Sep 19 '22 at 12:39
  • `hl = Highlighter((d,i,j)->c[i*9+j]*c2[i*9+j] < 0, crayon"red")` might do it. The parameters can't be changed (they are what `pretty_print` expects, but the function can access `c` and `c2`. I hope the index calculation is right (perhaps `i` and `j` need to be swapped). – Dan Getz Sep 19 '22 at 12:45
1

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

Shayan
  • 5,165
  • 4
  • 16
  • 45