4
$\begingroup$

I find the syntax of Background not clear. For instance in an example found in an old question

Grid[Table["g", {4}, {7}], 
 Background -> {None, 
   None, {{{2, 3}, {1, 3}} -> LightRed, {{3, 4}, {4, 7}} -> 
     LightBlue}}]

the elements from the line 2 to 3 and column 1 to 3. I would naturally think of {{2, 3}, {1, 3}} as a matrix imposing the coloring of the cells {2, 3} to {1, 3} whichever be the order. That is two cells on the third column not the square {{2, 3}, {3, 3}} as effectively done. I do not understand the two None. To resume could some one gives a better explanation than the one which is in the Mathematica documentation

$\endgroup$
9
$\begingroup$

The syntax for Background in Grid is Background -> {columns, rows, elements}. I will be referring to these as the columns list, the rows list, and elements list respectively. Example of coloring rows and columns:

bg = {
   {LightRed, LightBlue, LightGreen}, (* columns *)
   {LightYellow, LightOrange}, (* rows *)
   None
   };

data = Table["g", {4}, {7}];
Grid[data, Background -> bg]

Mathematica graphics

The image shows that the first element in the list columns gives the color of the first row, the second element in columns gives the color of the second column, and so on. Columns with no specified color remain transparent. Not white, but transparent. There is a difference, because when both the row color and the column color is specified for a certain element, then they appear to blend.

If you only specify one element instead of a list, then that color is used for all elements:

bg = {
   LightRed,
   None,
   None
   };

data = Table["g", {4}, {7}];
Grid[data, Background -> bg]

Mathematica graphics

You can think of {columns, rows, elements} as three layers since they can all be specified separately and blend if they overlap. None specifies that the particular layer is transparent. Which brings us to this example:

bg = {
   None,
   None,
   {{3, 4}, {4, 7}} -> LightPurple
   };

data = Table["g", {4}, {7}];
Grid[data, Background -> bg]

Mathematica graphics

The elements list uses the syntax {{rowStart, rowEnd}, {columnStart, columnEnd}}. All the rows and columns spanned by the rows rowStart to rowEnd and the columns columnStart to columnEnd are given a certain colors. You can put 2x2 matrices like this in a list if you want to specify several blocks. Note that the purple is not blended with any other color since the columns list and the rows list are None (i.e., transparent).

There is yet another syntax to keep track of. The colors will be repeated cyclically if you wrap the columns list or the rows list in another pair of brackets:

bg = {
   {{LightRed, LightBlue}},
   None,
   None
   };

data = Table["g", {4}, {7}];
Grid[data, Background -> bg]

Mathematica graphics

And, finally, you can also specify how rows and columns are to be colored by their indices:

bg = {
   {1 -> LightRed, 4 -> LightBlue},
   None,
   None
   };

data = Table["g", {4}, {7}];
Grid[data, Background -> bg]

Mathematica graphics

$\endgroup$
  • $\begingroup$ Nice answer & examples. You could add a link to the documentation section as a reference on how to get this information yourself. (Then all information of my answer would be fully contained in yours and easier to find in the future) $\endgroup$ – Lukas Lang Aug 23 '17 at 8:58
  • $\begingroup$ @Mathe172 Thanks. It's enough to have it here in the comments I think, I would only add that the documentation for Background also writes about this, under Generalizations & Extensions. $\endgroup$ – C. E. Aug 23 '17 at 11:28
2
$\begingroup$

Looking at the documentation of Grid (more specifically the section Options->Background), we can infer the following syntax:

Background->{<Columns>[,<Rows>][,<Cells>]}

The specifications for <Columns>/<Rows> can be:

  • A single color to apply to all columns/rows
  • A list of colors for the individual columns/rows
  • A list of rules for specific columns/rows

For <Cells>, the specifications are in the form of a list of rules {<Columns>,<Rows>}-><Color> where <Columns>/<Rows> can be a column/row index or a range thereof (specified by {<start>,<end>})

In all cases, omitted values default to None.

$\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.