3
$\begingroup$

From my SIMS I can export the image mappings in form of a two column ASCII file. For example if it were a 5x5 pixel mapping, it would give something like this:

{{0, 0}, {1, 10}, {2, 20}, {3, 30}, {4, 40}, {5, 50}, {6, 60}, {7,70},{8, 80}, {9, 90}, {10, 100}, {11, 110}, {12, 120}, {13, 130}, {14, 140}, {15, 150}, {16, 160}, {17, 170}, {18, 180}, {19, 190}, {20, 200}, {21, 210}, {22, 220}, {23, 230}, {24, 240}}

whereas the first column represents the pixel and the second column the counted events at that position. The image is rastered from left to right going downwards, so the first row are pixels 0-4, the second one 5-9 and so forth.

Now I want to plot this as the 5x5 pixel mapping as it was recorded. How would I go about that? I find it hard to rearrange the list back into the 5x5 format to begin with. Additionally the number of events should correspond to certain colors (just like a heat map). Thanks!

$\endgroup$
3
  • $\begingroup$ Is this what you mean? Image[Partition[pix, 5], ImageSize -> 400] where pix is the data you show. !Mathematica graphics $\endgroup$
    – Nasser
    Commented Jun 18, 2015 at 9:58
  • $\begingroup$ Probably something like ArrayPlot[Partition[list[[All, 2]], 5]]? $\endgroup$ Commented Jun 18, 2015 at 9:59
  • $\begingroup$ Yes, I needed to do [[All, 2]] first, forgot about the first element is not part of data. $\endgroup$
    – Nasser
    Commented Jun 18, 2015 at 10:01

2 Answers 2

4
$\begingroup$

This scales the data to 0..1 and then uses Image

pix = {{0, 0}, {1, 10}, {2, 20}, {3, 30}, {4, 40}, {5, 50}, {6, 
    60}, {7, 70}, {8, 80}, {9, 90}, {10, 100}, {11, 110}, {12, 
    120}, {13, 130}, {14, 140}, {15, 150}, {16, 160}, {17, 170}, {18, 
    180}, {19, 190}, {20, 200}, {21, 210}, {22, 220}, {23, 230}, {24, 
    240}};
pix2 = Flatten@pix[[All, 2]]
Image[Partition[Rescale[#, {Min[pix2], Max[pix2]}] & /@ pix2, 5], 
 ImageSize -> 500]

Update:

Thanks to @Guesswhoitis comment, rescaling can be done much easier. Here is the new version:

pix2 = Rescale[pix[[All, 2]]];
Image[Partition[pix2, 5], ImageSize -> 500]

Mathematica graphics

$\endgroup$
4
  • $\begingroup$ Actually, Rescale[pix2] is sufficient. :) $\endgroup$ Commented Jun 18, 2015 at 10:15
  • $\begingroup$ @Guesswhoitis. wow! thanks, I did not know this. I'll update. $\endgroup$
    – Nasser
    Commented Jun 18, 2015 at 10:17
  • $\begingroup$ If memory serves, you don't need the Flatten[], too. $\endgroup$ Commented Jun 18, 2015 at 10:27
  • $\begingroup$ @Guesswhoitis. yes ofcourse, Flatten is no longer needed in version 2. I had it there from version 1 in which it is needed, because the way I did it before, and I forgot to remove it again for version 2. Thanks, will remove it now. $\endgroup$
    – Nasser
    Commented Jun 18, 2015 at 10:33
1
$\begingroup$

A very similar way is to use Raster (instead of Image) which may be combined with other graphics objects if needed. Also, the Raster object can be displayed as any rectangular shape, with an explicit clipping range for the values and you can apply an arbitrary color function.

For example, given your initial mapping :

map1d = {{0, 0}, {1, 10}, {2, 20}, {3, 30}, {4, 40}, {5, 50}, {6, 60}, {7, 70},
{8, 80}, {9, 90}, {10, 100}, {11, 110}, {12, 120},{13, 130}, {14, 140}, {15, 150},
{16, 160}, {17, 170}, {18, 180}, {19, 190}, {20, 200}, {21, 210}, {22, 220}, 
{23, 230}, {24, 240}};

we define

map2d = Reverse@Partition[map1d[[All, 2]], 5];
clip = MinMax@map2d; (*or for v.<10 : Through[{Min, Max}[map2d]]*)

and

ras1=Raster[map2d, {{0, 0}, {5, 5}}, clip];
ras2=Raster[map2d, {{5, 5}, {8, 10}}, {100,200}, ColorFunction -> "TemperatureMap"];

Then

Graphics[{ras1, ras2}, Frame -> True]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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