11
$\begingroup$

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?

$\endgroup$
1
  • 1
    $\begingroup$ It always helps if you give some example data and list some Mathematica code that you gave tried. $\endgroup$
    – Jens
    Commented Jul 15, 2013 at 23:55

3 Answers 3

22
$\begingroup$

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]

$\endgroup$
1
  • $\begingroup$ I'm embarrassed to be getting so many upvotes for my stolen version of your answer... :) $\endgroup$
    – cormullion
    Commented Jul 16, 2013 at 21:21
22
$\begingroup$

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

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

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.

$\endgroup$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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