6
$\begingroup$

I have the following image (500*430pixels, 8bit, png): https://i.imgur.com/QVlIq3g.png

How can I show axes around (horizontal data: [1,15]; vertical data: [2,10])?

The result should be the following:

enter image description here

$\endgroup$
8
$\begingroup$
pic = Import["https://i.imgur.com/QVlIq3g.png"];


Graphics[
 Inset[pic, Scaled[{.5, .5}], Automatic, Scaled[1]],
 Frame -> True,
 PlotRange -> {{1, 15}, {2, 10}},
 AspectRatio -> ImageAspectRatio@pic
]

enter image description here

$\endgroup$
  • $\begingroup$ Great, thank you ... $\endgroup$ – mrz Nov 16 '16 at 16:00
4
$\begingroup$

I wrote you a function for this. It works by explicit setting the option FrameTicks.

ShowImageWithTicks[img_,xRange_,yRange_]:=Module[{dims=ImageDimensions[img]},Show[img,Frame->True,FrameTicks->{{Partition[Riffle[Range[0,dims[[2]],dims[[2]]/(Length[yRange]-1)],yRange],2],None},{Partition[Riffle[Range[0,dims[[1]],dims[[1]]/(Length[xRange]-1)],xRange],2],None}}]]

Which can simply be called with:

img=Import["https://i.imgur.com/QVlIq3g.png"];

ShowImageWithTicks[img,Range[1,15,2],Range[2,10]]

Blockquote

Your Labels can simply be introduced by calling to Show and setting FrameLabel:

Show[ShowImageWithTicks[img,Range[1,15,2],Range[2,10]],FrameLabel->{"time [s]","x [mm]"}]

Blockquote

$\endgroup$
  • $\begingroup$ You're using a Show inside of a Show. Why not combine it to be more elegant? $\endgroup$ – corey979 Nov 16 '16 at 15:42
  • $\begingroup$ Because its not a complex task to set FrameLabel so i didn't saw the reason to do it. If OP wants to, i'll combine it into a single function with optional parameters. $\endgroup$ – Julien Kluge Nov 16 '16 at 15:47
3
$\begingroup$

Assume, im is your image. Try this:

f[x_] := {(600 x)/8 - 150, x}
g[x_] := {(600*x)/(14), x}; 

    Show[im, Frame -> True, FrameTicks -> {{f[#] & /@ Range[2, 10, 2], None},
        {g[#] & /@ Range[2, 14, 2], None}}]

enter image description here

Have fun!

$\endgroup$
  • $\begingroup$ Thank you ... now I understand how to use FrameTicks ... $\endgroup$ – mrz Nov 18 '16 at 13:18
2
$\begingroup$

One possibility, use Show and turn the axes on with Axes->True. A rather crude solution for the ticks would be to scale them as follows (but there are surely more elegant solutions):

tx = Table[{500*i, 15*i}, {i, 0, 1, 1/15}];
ty = Table[{430*i, 10*i}, {i, 0, 1, 1/10}];
Show[img, Axes -> True, AxesLabel -> {"time (sec)", "x (mm)"}, 
 Ticks -> {tx, ty}]

enter image description here

$\endgroup$
  • $\begingroup$ Thanks ... I would like to have different axes values than the pixel numbers (see above). $\endgroup$ – mrz Nov 16 '16 at 15:04
1
$\begingroup$
img = Import["https://i.imgur.com/QVlIq3g.png"];

id = ImageDimensions @ img

{500, 430}

ticks = {Transpose @ {Subdivide[0, id[[1]], 7], Subdivide[1, 15, 7]}, 
         Transpose @ {Subdivide[0, id[[2]], 4], Subdivide[2, 10, 4]}}

Labeled[Show[img, Axes -> True, Ticks -> ticks],
 {"time (s)", "x (mm)"}, {Bottom, Left}, RotateLabel -> True]

enter image description here

Using Frame is more straightforward, but I'm providing a different approach. Additionally, one can define pl = {{1, 15}, {2, 10}} and insert specific parts in ticks (as well as the number of subdivisions) to make it automated; I'll leave it as it is for clarity.

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