# Plotting a 2D mapping of a 2 column ASCII

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!

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

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]


• Actually, Rescale[pix2] is sufficient. :) Commented Jun 18, 2015 at 10:15
• @Guesswhoitis. wow! thanks, I did not know this. I'll update. Commented Jun 18, 2015 at 10:17
• If memory serves, you don't need the Flatten[], too. Commented Jun 18, 2015 at 10:27
• @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. Commented Jun 18, 2015 at 10:33

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]