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

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 =
  {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.

enter image description here

  • $\begingroup$ +1 You can post images now. All your "new user" restrictions are gone (mathematica.stackexchange.com/privileges/new-user). Glad to see you around. $\endgroup$ – Dr. belisarius Jan 24 '13 at 15:27
  • $\begingroup$ 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). $\endgroup$ – Mr.Wizard Jan 24 '13 at 16:11
  • 3
    $\begingroup$ @DavidPark : Thanks but I could not use Presentations Application... $\endgroup$ – Karami Jan 24 '13 at 17:06

Just slide your tick coordinates into position programmatically:

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

Mathematica graphics

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

Mathematica graphics

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


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.