# How can I shift the frame ticks to the corner of each element-cell in a Matrixplot?

I am using the following code,to create a Legend for my plot. I would like to shift (or re-position) the frameticks to the corner of each of the cell-elements instead of being in their middle. I wonder: is it possible?

legendwidth = 30;
plotheights = 400;
div = 10.;
ticks = Table[{i + 1, Rescale[i, {0, 10}, {-2., 30.}]}, {i, 0, 10}];

MatrixPlot[Transpose @ {Table[i, {i, 0, div}]/div},
AspectRatio -> plotheights/legendwidth,
FrameTicks -> {{None, ticks}, {None, None}},
FrameStyle -> Directive[Thick],
LabelStyle -> Directive[Black, 10, Bold],
ImageSize -> {legendwidth + 17, plotheights},
ImagePadding -> {{10, 20}, {33, 0}},
ColorFunctionScaling -> False,
ColorFunction -> (ColorData["Rainbow"]),
PlotRangePadding -> None,
DataReversed -> True
]

• Should the ticks labels be also positioned at the corner of each cell element? – VLC Jan 24 '13 at 10:25
• try changing Table[{i+1,... in your definition of ticks to Table[{i+1/2 ... or to Table[{i+3/2.... – kglr Jan 24 '13 at 10:27
• @VLC sorry for the mistake...I corrected it now. Answer to your second question is yes. – Karami Jan 24 '13 at 10:34
• @kguler Thanks for the help.It is exactly what I wanted... – Karami Jan 24 '13 at 10:35
• I also worked this out for my answer to this question. – Jens Jan 31 '13 at 7:24

## 2 Answers

I am going to do this using the Presentations Application. You have a rather special case because you are using even divisions with uneven intervals. Another possibility would be to have two legend scales, one for a larger range and one for close lying values.

It is easier to do this with a ContourPlot because the ticks will naturally go with the contour divisions. One of the commands in Presentations is the ColorFunction ContourColors. This uses distinct colors for each contour interval even though some may be close lying in value and others encompass a wide range. However, in the following the underlying intervals are evenly spaced so ContourColors is not doing much. It would be more useful in an actural contour plot.

Finally, I'm using CustomTicks to relabel the ticks values according to your values.

<< Presentations

tickTable =
MapThread[{#1, #2} &, {Range[0, 10, 1],
Table[Rescale[i, {0, 10}, {-2., 30.}], {i, 0, 10}]}];

contours = Range[0, 10, 1] // N;
yticks = CustomTicks[Identity, databased[tickTable]];
legend1 =
Draw2D[
{ContourDraw[y, {x, 0, 1}, {y, First[contours], Last[contours]},
Contours -> contours,
ColorFunction ->
ContourColors[contours, ColorData["Rainbow"][#] &],
ColorFunctionScaling -> False,
PlotPoints -> 51]},

AspectRatio -> Automatic,
PlotRange -> {{0, 1}, {First[contours], Last[contours]}},
Frame -> True,
FrameTicks -> {None, yticks, None, None},
ImagePadding -> {{25, 2}, {5, 5}},
ImageSize -> {50, 200}]


Unfortunately, after all my work I am not allowed to post the image! Well, now I can so here is the image - I hope.

• +1 You can post images now. All your "new user" restrictions are gone (mathematica.stackexchange.com/privileges/new-user). Glad to see you around. – Dr. belisarius Jan 24 '13 at 15:27
• Welcome, David Park! Per site rules please make a note in both your profile and this post that you are the proprietor of the Presentations package (in a manner of speaking). – Mr.Wizard Jan 24 '13 at 16:11
• @DavidPark : Thanks but I could not use Presentations Application... – Karami Jan 24 '13 at 17:06

Just slide your tick coordinates into position programmatically:

ticks = {# - 0.5, #2} & @@@ ticks;


Or manually slide them into position using the interactive drawing tools;
Quadruple-click the gradient bar in your graphic and then drag:

You can copy and paste the edited Graphics` or assign it to a symbol.