I want to plot a 2x2 grid using MatrixPlot and I want to use a custom range for the x- and y axes. DataRange does almost what I want but when I plot it looks like this:

 PlotRangePadding -> None,
 Frame -> True,
 DataRange -> {{-kmax, kmax}, {-kmax, kmax}}]

enter image description here

As you can see the last plot tick has many decimals and this looks really ugly. I'd rather not show the last plot tick. I'm hesitant to use custom plot ticks because it's nice to have Mathematica choose a good number of plot ticks.

Clear[kmax, f]
kmax = 8.16

Define a function for Ticks.

f[min_, max_] := Range[Ceiling[min], Floor[max], 2]

Using random data: (Try without the FrameTicks as well to replicate OP)

MatrixPlot[RandomReal[{-1, 1}, {40, 40}]
 , PlotRangePadding -> None
 , FrameTicks -> {{f, f}, {f, f}}
 , Frame -> True
 , DataRange -> {{-kmax, kmax}, {-kmax, kmax}}

enter image description here

  • 1
    $\begingroup$ FrameTicks -> Automatic would work too. $\endgroup$
    – Syed
    Jan 15 at 13:34
  • $\begingroup$ FrameTicks -> Automatic works like a charm! thanks for the effort $\endgroup$ Jan 15 at 14:20

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.