# Hatching pattern in ColorRules for MatrixPlot

Using ColorRules with MatrixPlot, how does one get a hatching pattern in one of the cells?

For example, consider the data,

data = { {1, 2, 1}, {2, 2, 3} }


And now I consider the MatrixPlot,

MatrixPlot[data,
ColorRules -> {
1 -> Blue,
2 -> Red,
3 -> Purple
}
]


Question: For the cell with the value 3 and currently with the color Purple, how can I get it to have a Purple color with a hatched pattern?

• Somewhat related mathematica.stackexchange.com/q/31221/193 Oct 29, 2015 at 18:58
• @belisariusisforth Thanks. I took a look at that link. Perhaps I'm completely missing something, but a "direct" fix is to replace Purple above in ColorRules with Graphics[{Black, Line[{{0, 0}, {1, 1}}]}] (I can fudge with the color later; the hatching is most important to me). But with that, I simply get this weird maroon color with no hatching. And even if I switch out Black to, say, Yellow in the above, there is no effect. Oct 29, 2015 at 19:08
• As of v9 Mathematica haven't had direct support for hatching. Don't know if they did something about it later Oct 29, 2015 at 19:15
• Oct 30, 2015 at 5:37

## 1 Answer

As belisarius said, there is no current support for hatching but you could use a graphics overlay as a workaround.

First create the desired pattern and texture a Polygon with it:

t = Table[{0, n}, {n, -1, 1, 0.1}];

g[c_] := Graphics[{AbsoluteThickness[8], Line /@ Transpose[{t, t + 1}]},
PlotRange -> {{0, 1}, {0, 1}}, Background -> c];

pattern2[p_, c_] := Graphics[{Texture[g[c]], Polygon[{
{p[[1]] - 1, p[[2]] - 1},
{p[[1]], p[[2]] - 1},
{p[[1]], p[[2]]},
{p[[1]] - 1, p[[2]]}},
VertexTextureCoordinates -> {{0, 0}, {0, 1}, {1, 1}, {1, 0}}
]}]

pattern2[{1, 1}, Purple]


Create a function to place the pattern over any field of a given value n. matrixLength is the length of the input data, c the color.

overlay[patternFunc_, n_, c_, matrixLength_] :=
Show[patternFunc[#,c] & /@ ({#2,matrixLength +
1 - #1} & @@@ Position[data, n])];


Example:

plot = MatrixPlot[data, ColorRules -> {1 -> Red, 2 -> Orange, 3 -> Purple}]


Show[
plot,
overlay[pattern2, 3, Purple, Length@data]
]


Not the most elegant/efficient solution but it might be useful as a starting point.

• Very useful! Thanks! Oct 29, 2015 at 20:06