3
$\begingroup$

Column layout construct has the BaselinePosition option where you can specify the origin placement for the "bars", e.g. Bottom (Vertical orientation).

Here is some data to play with:

wrdBars = {{"one", "two", "three", "four", "five", "six", "seven", 
"eight"}, {"one", "two", "three", "four"}, {"one", "two", "three",
 "four", "five", "six", "seven", "eight", "nine", "ten", "eleven",
 "twelve"}, {"one", "two", "three"}, {"one", "two", "three", 
"four", "five", "six", "seven"}, {"one", "two", "three", "four", 
"five"}};

I can get a nice horizontal orientation of my word bars with alternating background using

Column[#, Background->{{LightRed, LightGreen}}, Frame->All]& @ wrdBars

and I can switch to vertical orientation, although I am not sure if this is the right way to do it, with

Column[#, BaselinePosition->Bottom, Frame->All] & /@ wrdBars    

although I am missing the alternating background for each bar, how can I get this ?

And how can I use a Grid construct to get a similar presentation on my data ?

$\endgroup$

2 Answers 2

6
$\begingroup$

Bob has shown one way to set the Background property of Column. Here is another way:

coloredColumn[list_, {i_}] := Column[list,
  BaselinePosition -> Bottom,
  Frame -> All,
  Background -> If[EvenQ[i], LightGreen, LightRed]
  ]
Row[MapIndexed[coloredColumn, wrdBars], Spacer[5]]

Mathematica graphics

You can use Item to style cells individually, and it could look like this (could also be written with If like above instead of having two definitions of color):

Clear[color]
color[el_, {_?OddQ, _}] := Item[el, Background -> LightRed]
color[el_, {_?EvenQ, _}] := Item[el, Background -> LightGreen]

coloredBars = MapIndexed[color, wrdBars, {2}];
Row[Column[#, BaselinePosition -> Bottom, Frame -> All] & /@ coloredBars, Spacer[5]]

Mathematica graphics

Coloring the rows in each bar is a bit more difficult because we have to transform the row index before we can test if it is even or odd counted from the bottom as opposed to from the top.

Clear[color]
color[el_, pos_] := Item[el, Background -> color[wrdBars, pos]]

color[list_List, {i_, j_}] := With[{len = Length@Part[list, i]},
  If[OddQ[len - j], LightRed, LightGreen]
  ]

coloredBars = MapIndexed[color, wrdBars, {2}];
Row[Column[#, BaselinePosition -> Bottom, Frame -> All] & /@ coloredBars, Spacer[5]]

Mathematica graphics

In order to use Grid I'll first color the list of words, then pad it so that it's a rectangular matrix, and then transpose it.

padded = PadLeft[#, Max[Length /@ wrdBars], ""] & /@ coloredBars;
Grid[Transpose@padded]

Mathematica graphics

This works regardless of what coloring function you have applied. Note that you can provide styling for this grid either at the cell level, by modifying color, or at the grid level, by providing options to Grid.

$\endgroup$
2
  • $\begingroup$ I think you should reverse the order at each column in order to align the words. It is better presented this way, this is also what Bod did. $\endgroup$ Jun 4, 2016 at 15:25
  • 1
    $\begingroup$ @Athanassios More importantly, it's not what you did in your question. You presented it as if the formatting was what you wanted, except for the coloring. It is easily done with Reverse, I'll leave it as an exercise. $\endgroup$
    – C. E.
    Jun 4, 2016 at 15:32
5
$\begingroup$
wrdBars = 
  Range@{8, 4, 12, 3, 7, 5} /. 
   Thread[Range[12] -> {"one", "two", "three", "four", "five", "six", 
      "seven", "eight", "nine", "ten", "eleven", "twelve"}];

n = 0; Row[
 Column[#, BaselinePosition -> Bottom, Frame -> All, 
    Background -> If[EvenQ[n++], LightRed, LightGreen]] & /@ 
  Reverse /@ wrdBars]

enter image description here

$\endgroup$

Your Answer

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

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