3
$\begingroup$

I have a dataset with normal numbers (either integers or real) and I would like to visualize the dataset in a table where, for every row a colormap shows the ranking of the numbers from Min (blue) to Max (red) but I cannot find an option to do that.

For example like this:

    myDataset = Dataset[{<|"a" -> 5, "b" -> 7, "c" -> 2|>,
   <|"a" -> 3.3, "b" -> 5.1, "c" -> 9.7|>}]

I came across something that includes this code here from this example, but I don't find a way to apply it to my datset

primarydiffs=primarywardvotes[All,<|"TJ-CS"->#["TISHAURA JONES"]-#["CARA SPENCER"],"TJ-LR"->#["TISHAURA JONES"]-#["LEWIS REED"],\[IndentingNewLine]"CS-LR"->#["CARA SPENCER"]-#["LEWIS REED"]\[IndentingNewLine]|>&] 
wdmax=primarydiffs[Max];wdmin=primarydiffs[Min]; 
addBG=Dataset[#,Background->{(ColorData["TemperatureMap"][(#1-wdmin)/(wdmax-wdmin)]&)}]&; 

The datasets that I want to apply it to are either ALL integers or ALL real numbers, not mixed as my example up.

Appreciate your help!

Edit:

And how would I do that if I would not the last two columns do be included in that? So only columns 2-8 should be "temperaturemapped", i.e. [All, 2;;8]. And again, the temperaturemap only per row, not vertically.

Edit2: Here is a bigger example dataset, I already included a proposed code from @Syed. So the first column should be ignored (is not part of the calculation, just a district number, and the last 2 also should not be included. Or get their own temperaturemap, but they would need it vertically in the column, i.e. across districts. Is that possible? Otherwise I am fine if it is per district (=row) for Party 1 to Party 7.

myDataset2 = 
 Dataset[{<|"District No." -> 1, 
    "Party 1" -> RandomInteger[10000; 100000], 
    "Party 2" -> RandomInteger[10000; 100000], 
    "Party 3" ->  RandomInteger[10000; 100000], 
    "Party 4" -> RandomInteger[10000; 100000], 
    "Party 5" -> RandomInteger[10000; 100000], 
    "Party 6" -> RandomInteger[10000; 100000],  
    "Party 7" -> RandomInteger[10000; 100000], 
    "Invalid" -> RandomInteger[10000; 100000], 
    "Absent" -> RandomInteger[10000; 100000]|>,
   <|"District No." -> 2, "Party 1" -> RandomInteger[10000; 100000], 
    "Party 2" -> RandomInteger[10000; 100000], 
    "Party 3" ->  RandomInteger[10000; 100000], 
    "Party 4" -> RandomInteger[10000; 100000], 
    "Party 5" -> RandomInteger[10000; 100000], 
    "Party 6" -> RandomInteger[10000; 100000],  
    "Party 7" -> RandomInteger[10000; 100000], 
    "Invalid" -> RandomInteger[10000; 100000], 
    "Absent" -> RandomInteger[10000; 100000]|>,
   <|"District No." -> 3, "Party 1" -> RandomInteger[10000; 100000], 
    "Party 2" -> RandomInteger[10000; 100000], 
    "Party 3" ->  RandomInteger[10000; 100000], 
    "Party 4" -> RandomInteger[10000; 100000], 
    "Party 5" -> RandomInteger[10000; 100000], 
    "Party 6" -> RandomInteger[10000; 100000],  
    "Party 7" -> RandomInteger[10000; 100000], 
    "Invalid" -> RandomInteger[10000; 100000], 
    "Absent" -> RandomInteger[10000; 100000]|>,
   <|"District No." -> 4, "Party 1" -> RandomInteger[10000; 100000], 
    "Party 2" -> RandomInteger[10000; 100000], 
    "Party 3" ->  RandomInteger[10000; 100000], 
    "Party 4" -> RandomInteger[10000; 100000], 
    "Party 5" -> RandomInteger[10000; 100000], 
    "Party 6" -> RandomInteger[10000; 100000],  
    "Party 7" -> RandomInteger[10000; 100000], 
    "Invalid" -> RandomInteger[10000; 100000], 
    "Absent" -> RandomInteger[10000; 100000]|>,
   <|"District No." -> 5, "Party 1" -> RandomInteger[10000; 100000], 
    "Party 2" -> RandomInteger[10000; 100000], 
    "Party 3" ->  RandomInteger[10000; 100000], 
    "Party 4" -> RandomInteger[10000; 100000], 
    "Party 5" -> RandomInteger[10000; 100000], 
    "Party 6" -> RandomInteger[10000; 100000],  
    "Party 7" -> RandomInteger[10000; 100000], 
    "Invalid" -> RandomInteger[10000; 100000], 
    "Absent" -> RandomInteger[10000; 100000]|>
   }, Background -> {(ColorData["LightTemperatureMap"][
       Rescale[#, MinMax[myDataset2]]] &)}] 
$\endgroup$
3
  • $\begingroup$ Could you please include a more representative dataset that is more similar in size and contents to the data you are actually dealing with? $\endgroup$
    – MarcoB
    Jun 21 at 20:23
  • $\begingroup$ @MarcoB I have added a big example dataset that is like the ones I have $\endgroup$
    – Kai
    Jun 21 at 22:11
  • 1
    $\begingroup$ What's the purpose of all the 10000;? $\endgroup$
    – Michael E2
    Jun 22 at 13:38

3 Answers 3

3
$\begingroup$

Here's how I would do it:

Dataset[
 Table[
  <|
   "District No." -> i,
   # -> RandomInteger[100000] & /@ {"Party 1", "Party 2", "Party 3", 
     "Party 4", "Party 5", "Party 6", "Party 7", "Invalid", "Absent"}
   |>, {i, 10}
  ],
 Background -> {
   s : "Invalid" | "Absent" -> (
     ColorData["Rainbow"]@Rescale[#, MinMax@#3[[All, #2[[2]]]]] &
     ),
   p_String /; StringStartsQ[p, "Party"] -> (
     ColorData["TemperatureMap"]@
       Rescale[#, MinMax@#3[[#2[[1]], 2 ;; -3]]] &
     )
   }]

enter image description here

This uses three ingredients:

  • The colors are computed using ColorData[map][Rescale[value,MinMax@values]]. This colors the value value using the colormap map, assuming the values over the range covered by values.
  • The positions where to apply the coloring can be specified using patterns: We use this to apply some rules to elements in "Invalid" and "Absent", and one for anything starting with "Party".
  • The style specifications can be functions with three arguments, being called as func[value,position,dataset]. We get the relevant subset of values for the color function using the position information in the second argument, and extract the corresponding range from the last argument
$\endgroup$
0
$\begingroup$

Here is an example of mapping each column separately (only Background-> shown). I made Party 3 going from 1000 to 10000, and Party 4 going from 100000 to 1000000. Other columns are 10000 to 100000.

Background -> ({
   {All, 
     "Party 1"} -> {(ColorData["LightTemperatureMap"][
        Rescale[#, MinMax[myDataset2[All, {{"Party 1"}}]]]] &)},
   {All, 
     "Party 2"} -> {(ColorData["LightTemperatureMap"][
        Rescale[#, MinMax[myDataset2[All, {{"Party 2"}}]]]] &)},
   {All, 
     "Party 3"} -> {(ColorData["LightTemperatureMap"][
        Rescale[#, MinMax[myDataset2[All, {{"Party 3"}}]]]] &)},
   {All, 
     "Party 4"} -> {(ColorData["LightTemperatureMap"][
        Rescale[#, MinMax[myDataset2[All, {{"Party 4"}}]]]] &)},
   {All, 
     "Party 5"} -> {(ColorData["LightTemperatureMap"][
        Rescale[#, MinMax[myDataset2[All, {{"Party 5"}}]]]] &)},
   {All, 
     "Party 6"} -> {(ColorData["LightTemperatureMap"][
        Rescale[#, MinMax[myDataset2[All, {{"Party 6"}}]]]] &)},
   {All, 
     "Party 7"} -> {(ColorData["LightTemperatureMap"][
        Rescale[#, MinMax[myDataset2[All, {{"Party 7"}}]]]] &)}
   })

enter image description here

Here is an example where the mapping is applied to each row separately. In this case, I entered each individual cell. I also defined variable partyList to save some typing.

partyList={"Party 1", "Party 2", "Party 3", "Party 4", "Party 5", "Party 6","Party 7"}; 

Here is the Background:

Background -> ({
   {1, "Party 1"} -> {(ColorData["LightTemperatureMap"][
        Rescale[#, MinMax[myDataset2[[1, partyList]]]]] &)},
   {1, "Party 2"} -> {(ColorData["LightTemperatureMap"][
        Rescale[#, MinMax[myDataset2[[1, partyList]]]]] &)},
   {1, "Party 3"} -> {(ColorData["LightTemperatureMap"][
        Rescale[#, MinMax[myDataset2[[1, partyList]]]]] &)},
   {1, "Party 4"} -> {(ColorData["LightTemperatureMap"][
        Rescale[#, MinMax[myDataset2[[1, partyList]]]]] &)},
   {1, "Party 5"} -> {(ColorData["LightTemperatureMap"][
        Rescale[#, MinMax[myDataset2[[1, partyList]]]]] &)},
   {1, "Party 6"} -> {(ColorData["LightTemperatureMap"][
        Rescale[#, MinMax[myDataset2[[1, partyList]]]]] &)},
   {1, "Party 7"} -> {(ColorData["LightTemperatureMap"][
        Rescale[#, MinMax[myDataset2[[1, partyList]]]]] &)},
   
   {2, "Party 1"} -> {(ColorData["LightTemperatureMap"][
        Rescale[#, MinMax[myDataset2[[2, partyList]]]]] &)},
   {2, "Party 2"} -> {(ColorData["LightTemperatureMap"][
        Rescale[#, MinMax[myDataset2[[2, partyList]]]]] &)},
   {2, "Party 3"} -> {(ColorData["LightTemperatureMap"][
        Rescale[#, MinMax[myDataset2[[2, partyList]]]]] &)},
   {2, "Party 4"} -> {(ColorData["LightTemperatureMap"][
        Rescale[#, MinMax[myDataset2[[2, partyList]]]]] &)},
   {2, "Party 5"} -> {(ColorData["LightTemperatureMap"][
        Rescale[#, MinMax[myDataset2[[2, partyList]]]]] &)},
   {2, "Party 6"} -> {(ColorData["LightTemperatureMap"][
        Rescale[#, MinMax[myDataset2[[2, partyList]]]]] &)},
   {2, "Party 7"} -> {(ColorData["LightTemperatureMap"][
        Rescale[#, MinMax[myDataset2[[2, partyList]]]]] &)},
   
   {3, "Party 1"} -> {(ColorData["LightTemperatureMap"][
        Rescale[#, MinMax[myDataset2[[3, partyList]]]]] &)},
   {3, "Party 2"} -> {(ColorData["LightTemperatureMap"][
        Rescale[#, MinMax[myDataset2[[3, partyList]]]]] &)},
   {3, "Party 3"} -> {(ColorData["LightTemperatureMap"][
        Rescale[#, MinMax[myDataset2[[3, partyList]]]]] &)},
   {3, "Party 4"} -> {(ColorData["LightTemperatureMap"][
        Rescale[#, MinMax[myDataset2[[3, partyList]]]]] &)},
   {3, "Party 5"} -> {(ColorData["LightTemperatureMap"][
        Rescale[#, MinMax[myDataset2[[3, partyList]]]]] &)},
   {3, "Party 6"} -> {(ColorData["LightTemperatureMap"][
        Rescale[#, MinMax[myDataset2[[3, partyList]]]]] &)},
   {3, "Party 7"} -> {(ColorData["LightTemperatureMap"][
        Rescale[#, MinMax[myDataset2[[3, partyList]]]]] &)},
   
   {4, "Party 1"} -> {(ColorData["LightTemperatureMap"][
        Rescale[#, MinMax[myDataset2[[4, partyList]]]]] &)},
   {4, "Party 2"} -> {(ColorData["LightTemperatureMap"][
        Rescale[#, MinMax[myDataset2[[4, partyList]]]]] &)},
   {4, "Party 3"} -> {(ColorData["LightTemperatureMap"][
        Rescale[#, MinMax[myDataset2[[4, partyList]]]]] &)},
   {4, "Party 4"} -> {(ColorData["LightTemperatureMap"][
        Rescale[#, MinMax[myDataset2[[4, partyList]]]]] &)},
   {4, "Party 5"} -> {(ColorData["LightTemperatureMap"][
        Rescale[#, MinMax[myDataset2[[4, partyList]]]]] &)},
   {4, "Party 6"} -> {(ColorData["LightTemperatureMap"][
        Rescale[#, MinMax[myDataset2[[4, partyList]]]]] &)},
   {4, "Party 7"} -> {(ColorData["LightTemperatureMap"][
        Rescale[#, MinMax[myDataset2[[4, partyList]]]]] &)},
   
   {5, "Party 1"} -> {(ColorData["LightTemperatureMap"][
        Rescale[#, MinMax[myDataset2[[5, partyList]]]]] &)},
   {5, "Party 2"} -> {(ColorData["LightTemperatureMap"][
        Rescale[#, MinMax[myDataset2[[5, partyList]]]]] &)},
   {5, "Party 3"} -> {(ColorData["LightTemperatureMap"][
        Rescale[#, MinMax[myDataset2[[5, partyList]]]]] &)},
   {5, "Party 4"} -> {(ColorData["LightTemperatureMap"][
        Rescale[#, MinMax[myDataset2[[5, partyList]]]]] &)},
   {5, "Party 5"} -> {(ColorData["LightTemperatureMap"][
        Rescale[#, MinMax[myDataset2[[5, partyList]]]]] &)},
   {5, "Party 6"} -> {(ColorData["LightTemperatureMap"][
        Rescale[#, MinMax[myDataset2[[5, partyList]]]]] &)},
   {5, "Party 7"} -> {(ColorData["LightTemperatureMap"][
        Rescale[#, MinMax[myDataset2[[5, partyList]]]]] &)}
   
   })

enter image description here

This can be simplified as there are redundancies.

$\endgroup$
5
  • $\begingroup$ The procedure seems good, but, for example for District 1, why does it use the same blue tone for party 2, 3, 4, 6 and 7? The point would be to assign different colors to each party (horizontally) so I can see which party scored most and least in each district. Another step would be to do it vertically, to see in which district did a party score most. Of course this would not work with absolute number of votes and would need percentages. But regardless, how would I have to adjust the code? $\endgroup$
    – Kai
    Jun 21 at 23:24
  • $\begingroup$ What I posted up there is the color map of each column separately. So in Party 3, 8985 is red since it is the highest in the column, but in Party 2, a similar value (7356) is blue. $\endgroup$ Jun 22 at 0:02
  • $\begingroup$ Ah I had understood "across columns" as horizontally, showing the Min-Max of the parties per district. But that is what I need, how do I adjust it to color horizontally and show the best/worst parties of each district? $\endgroup$
    – Kai
    Jun 22 at 0:06
  • $\begingroup$ Good point! I will change the text. $\endgroup$ Jun 22 at 0:09
  • $\begingroup$ And please how I visualize the colormap horizontally. Knowing both options is good, so thank you for the vertical one. Just the horizontal one is important and I don't really understand in Mathematica how I adress specific rows and colums. I checked out the help a million times, but i can't make sense of it and usually just keep trying millions of possibilities until it eventually fits (or I decide that I don't want it after all) $\endgroup$
    – Kai
    Jun 22 at 0:16
0
$\begingroup$

Here is my first thought to handle this. First, define a color set (see This thread).

colorSet = Blend[{{0, Blue}, {1/2 - 0.1, White}, {1/2 + 0.1, White},{1,Red}}, #1] &

Then map it over a range:

colorSet /@ (Range[15]/15.)

Then define the data set and use Background with your color set.

myDataset = 
 Dataset[{<|"a" -> 5, "b" -> 7, "c" -> 2|>, <|"a" -> 3.3, "b" -> 5.1, 
    "c" -> 9.7|>}, Background -> (colorSet[#/Max[myDataset]] &)]

Result

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.