Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I would like to make a grid where each cell background is partially filled according to the numerical value being represented (on a scale from 0 to 1). Similar functionality to the one requested in this TeX.SE question.

How can I achieve this with Mathematica?

share|improve this question
1  
It always helps if you give some example data and list some Mathematica code that you gave tried. –  Jens Jul 15 '13 at 23:55

3 Answers 3

up vote 21 down vote accepted

You can also use the HorizontalGauge function introduced in version 9. For example:

bar = HorizontalGauge[#, {0, 100}, GaugeMarkers -> "ScaleRange", 
    GaugeStyle -> {Darker@Green, GrayLevel[0.95]}, TicksStyle -> None,
    GaugeFrameSize -> None, ScalePadding -> 0, ImageSize -> 200, AspectRatio -> 1/5, 
    LabelStyle -> None, Epilog -> Text[Style[#, FontSize -> 15], Scaled[{0.8, 0.5}]]] &;

Grid[Map[bar, RandomInteger[{10, 90}, {4, 4}], {2}], Frame -> All]

share|improve this answer
    
Thanks that looks great! –  dabd Jul 16 '13 at 17:05
    
I'm embarrassed to be getting so many upvotes for my stolen version of your answer... :) –  cormullion Jul 16 '13 at 21:21

I liked rm-rf's gauged solution so much that I made an interactive version:

bar[n_] := DynamicModule[{x = n}, 
HorizontalGauge[Dynamic[x], {0, 100}, GaugeMarkers -> "ScaleRange", 
 GaugeStyle -> {Darker@Green, GrayLevel[0.95]}, TicksStyle -> None, 
 GaugeFrameSize -> None, ScalePadding -> 0, ImageSize -> 200, 
 AspectRatio -> 1/5, LabelStyle -> None, 
 Epilog -> 
  Text[Style[Dynamic[x], FontSize -> 15], Scaled[{0.8, 0.5}]]]]
Grid[Table[bar[RandomInteger[{10, 90}]], {4}, {4}], Frame -> All]

Ideal for those last minute adjustments to that troublesome dataset...

animated

share|improve this answer
1  
"Ideal for those last minute adjustments to that troublesome dataset..." or to show on conference that global warming is a fact. :p –  Kuba Jul 16 '13 at 7:38
1  
@kuba :) I'd laugh if I thought it never happened... –  cormullion Jul 16 '13 at 7:49
1  
"All that pressure about results...". But let's not talk about this. I like this +1. –  Kuba Jul 16 '13 at 8:05
    
Very nice! Thanks –  dabd Jul 16 '13 at 17:06

Something like this?

Grid[Map[Graphics[{GrayLevel[0.8], 
     Rectangle[Scaled[{0, 0}], Scaled[{#, 1}]], Black, 
     Style[Text[#], Large]}, AspectRatio -> 0.2] &, 
  RandomReal[{0, 1}, {4, 3}], {2}], Frame -> All]

enter image description here

Of course you can place the Text and style to taste. Here is a slightly more complex version:

Grid[Map[Graphics[{GrayLevel[0.8], 
     Rectangle[Scaled[{0, 0}], Scaled[{#, 1}]], Black, 
     Inset[Text[
       Style[NumberForm[#, {3, 3}], Large, 
        FontFamily -> "Franklin Gothic"]], Scaled[{0.8, 0.5}]]}, 
    AspectRatio -> 0.2] &, RandomReal[{0, 1}, {4, 3}], {2}], 
 Frame -> All, FrameStyle -> Directive[GrayLevel[0.9]], Spacings -> 0]

enter image description here

It would be straightforward to create a function that took a matrix of numbers and generated this.

share|improve this answer
    
Thanks that looks great! –  dabd Jul 16 '13 at 17:06

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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