Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I have manipulated an image and want to ListPlot it. However, the result is not to my liking because the conventions for drawing images and plots are different. Images have the origin at the top left and increase in Y downwards while plots have the origin at the bottom left and increase in Y upwards.

What is the easiest way to reconcile the difference, preferably without manipulating the data?

share|improve this question
    
What do you mean by list plotting an image? –  Yu-Sung Chang Mar 31 '12 at 1:14
    
You start with an image, process it, and end up with an array which you display with ListPlot. –  Emre Mar 31 '12 at 1:15
    
Do you mean ListPlot or something like ArrayPlot or MatrixPlot. If you are refering to ArrayPlot/MatrixPlot then yes, they are different, the origin is in the bottom left vs. top left. You might try making your data an image again Image@data... –  s0rce Mar 31 '12 at 1:24
add comment

4 Answers

up vote 16 down vote accepted

1: Reversing the image in a 2D plot (ArrayPlot or MatrixPlot)

Simply use DataReversed -> True. This has the effect of flipping the image along the horizontal axis. For example:

func[x_, y_] := Sinc[y ^2 + x^3];
data = Table[func[x,y], {x, -π, π, 0.1}, {y, -π, π, 0.1}];
ArrayPlot[data, DataReversed -> #] & /@ {True, False} // GraphicsRow

enter image description here

2: Changing the origin in a 1D plot (ListPlot or Plot)

Use AxesOrigin -> {x, y} to change the origin to where ever you like. For example:

Plot[Sin[x], {x, 0, 2 Pi}, AxesOrigin -> {0.5, 0.5}]

enter image description here

3: Changing the direction of the y-axis (or x-axis) in a 1D plot

Flipping the y-axis in a 1D plot is a bit more involved and is a very common approach in displaying depth plots. You can implement this in Mathematica by negating your input to ListPlot and assigning custom ticks with a function. Here's an example:

x = Sin /@ Range[0, 2 π, 0.1];
ListPlot[-x, Ticks -> {Automatic, Function[{xmin, xmax}, 
    Table[{i, -i, {0.02, 0}}, {i, N@FindDivisions[{xmin, xmax}, 10]}]]}]

enter image description here

share|improve this answer
2  
+1 for covering the bases –  Mr.Wizard Mar 31 '12 at 2:01
add comment

I wrote some code for this a few years ago. Haven't looked at it in a long time but it may be useful. It is in the Wolfram library archive (that seems to have been all but abandoned by both users and Wolfram).

enter image description here

share|improve this answer
add comment

In the docs check the entry ImageData>Options>DataReversed and the first example in the section Properties & Relations to see if it helps with what you need.

share|improve this answer
    
I wish it did but I could not feed it to ListPlot. It says ArrayPlot et al only. Close... –  Emre Mar 31 '12 at 1:33
    
@Emre, a simple example showing how you go from an image to ListPlot would help everyone - what is the data in your ListPlot and how does it relate to the image? –  kguler Mar 31 '12 at 1:41
    
It's a 2D integer array. It can be from binarizing or segmenting the image. I want select all the pixels belonging to a particular cluster, using Position[filtered_image,n], which gives me a list of pixels, and run ListPlot. –  Emre Mar 31 '12 at 1:47
add comment

It's really not clear from your question what you want, but here is one simple interpretation:

dat = Table[{x, x^2 Exp[-x^2]}, {x, -5, 5, 0.1}];

ListLinePlot[dat, PlotRange -> All]

Mathematica graphics

ticks = Table[{-x, x}, {x, 0, 0.4, 0.05}];

ListLinePlot[{#, -#2} & @@@ dat, PlotRange -> All, Ticks -> {All, ticks}]

Mathematica graphics

This could be automated, but since I don't know if this is what you want I'm leaving it there for now.

share|improve this answer
1  
Purely graphical solution without touching data: ListLinePlot[dat, PlotRange -> All] /. Line[args___] :> Scale[Line[args], {1, -1}]. PlotRange goes nuts though (Probably a bug). –  Yu-Sung Chang Mar 31 '12 at 1:38
1  
@Yu-SungChang ListLinePlot is turning PlotRange -> All into explicit lower and upper bounds, which I don't consider to be a bug. Sometimes it's helpful, and sometimes it's not. –  Brett Champion Mar 31 '12 at 3:20
    
I actually ended up going with Apply[{#1, -#2}], as suggested here, but there were several useful answers and comments so I wanted to thank everyone. –  Emre Apr 1 '12 at 1:35
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.