2
$\begingroup$

Here's some data:

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

Here's the data plotted:

ArrayPlot[data,FrameTicks->Automatic]

enter image description here

I want to modify the frame ticks such that the horizontal axis is {Sin[2],Sin[3],Sin[4]} and the vertical axis is {0.3,0.6,0.9}. How can I do this?

The documentation on FrameTicks suggests that I should define a function and input that. However, I can't parse the example given. What are the arguments passed to the function when used in this way? Naively I'd guess that it's just the index of the x- and y- arrays (so e.g. {1,1} would be the top-left box), but attempting to write my own function, e.g. with xaxis[min_,max_]:=min+1 and yaxis[min_,max_]:=min+2, doesn't work. ArrayPlot[data,FrameTicks->{xaxis,yaxis}] leads to an ArrayPlot with no frame ticks.

$\endgroup$

2 Answers 2

4
$\begingroup$

Update: For arbitrary tick labels

hticklabels = {Sin[2], Sin[3], Sin[4]} ;
vticklabels = {0.3, 0.6, .9};

{hticks, vticks} = MapIndexed[{#2[[1]], #} &, #] & /@ {hticklabels, vticklabels};

ArrayPlot[data, FrameTicks -> {{vticks, vticks}, {hticks, hticks}}]

enter image description here

Alternatively, you can use

{hticks, vticks} = Transpose[{Range@Length@#, #}] & /@ {hticklabels, vticklabels}

Original answer:

You can use the option DataRange:

ArrayPlot[data, FrameTicks -> Automatic, DataRange -> {{2, 4}, {3, 5}}]

enter image description here

Alternatively,

ArrayPlot[data, 
 FrameTicks -> {{#, #} & @ Thread[{Range[3], 1 + Range[3]}],
   {#, #} & @ Thread[{Range[3], 2 + Range[3]}]}]

enter image description here

$\endgroup$
2
  • $\begingroup$ I think I simplified my minimal working example too much ... going to edit the question. $\endgroup$
    – Allure
    Mar 31, 2020 at 12:45
  • $\begingroup$ @Allure, please see the update. $\endgroup$
    – kglr
    Mar 31, 2020 at 13:20
1
$\begingroup$

Sort of an exposition to kglr's answer, which works, but is also a bit hard to parse.

FrameTicks is looking for input of the form {{1,[func]},{2,[func]},{3,[func]}}, where [func] is the function you want. Therefore one can manually set the horizontal labels to be {{1,Sin[2]},{2,Sin[3]},{3,Sin[4]}}. The MapIndexed command as used by kglr is a fancy way of generating this list.

You can also try something like

vticklabels = Range[0.3,0.9,0.3] (generates a list from 0.3 to 0.9 in steps of 0.3, see the documentation on Range) followed by

vticks = MapIndexed[{First[#2], #} &, vticklabels] (First[#2] is the index of the argument - 1,2,3, etc. & puts the elements of vticklabels into the corresponding slot. This generates the list {{1,0.3},{2,0.6},{3,0.9}} which is the list to give FrameTicks)

FrameTicks accepts up to four arguments. In the command FrameTicks -> {a,b,c,d}, the list in a goes on the left-hand vertical axis of the frame, b goes into the bottom horizontal axis of the frame, c goes to the right-hand vertical axis, and d goes to the top horizontal axis.

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