2
$\begingroup$

I have a map what I would like color according to predefined RGB colors. So first I make an array of the ImageData:

Download the attached data source here: map, colors

input = Import[
  "d:\\bitbucket\\diversity\\data\\nlcd_200x200.tif", {"GeoTIFF", 
   "Image"}] 

a = ImageData[input]*255;

Then I load the colors from a CSV:

Colors = ReadList[
  "d:\\bitbucket\\diversity\\data\\nlcd\\nlcd-colors.csv", {Word, 
   RGBColor[Word, Word, Word]}, WordSeparators -> {"\t", " ", ","}]

Then I try to use the colors to make a colorful map (it's bad...):

ArrayPlot[a, ColorRules -> {[[#1]] -> Apply[[#2]] & Colors}]

Code sample:

Take[a, 2]

{{42., 41., 41., 41., 41., 21., 41., 41., 41., 41., 41., 41., 41., 
  41., 41., 21., 41., 41., 41., 41., 41., 41., 41., 41., 41., 41., 
  41., 41., 41., 41., 41., 41., 41., 41., 41., 41., 41., 41., 41., 
  41., 41., 41., 41., 41., 41., 11., 11., 11., 11., 11., 11., 11., 
  11., 11., 11., 11., 11., 11., 41., 21., 52., 52., 52., 52., 52., 
  52., 52., 52., 41., 41., 41., 41., 41., 41., 41., 52., 52., 41., 
  52., 52., 41., 41., 41., 41., 41., 41., 41., 41., 41., 41., 41., 
  21., 41., 41., 41., 41., 41., 41., 41., 41., 41., 41., 41., 41., 
  41., 52., 52., 95., 95., 42., 21., 95., 90., 90., 41., 95., 95., 
  11., 11., 95., 11., 11., 11., 95., 95., 95., 95., 90., 90., 90., 
  95., 90., 90., 90., 90., 41., 41., 81., 81., 81., 81., 81., 81., 
  81., 81., 81., 81., 81., 71., 71., 71., 41., 41., 41., 21., 41., 
  11., 11., 11., 11., 11., 11., 11., 11., 11., 11., 11., 11., 11., 
  11., 11., 11., 11., 11., 11., 11., 11., 11., 11., 11., 11., 11., 
  90., 90., 41., 21., 41., 41., 41., 41., 41., 41., 95., 95., 41., 
  41., 41., 95., 71., 71.}, {42., 41., 41., 41., 21., 21., 41., 41., 
  41., 41., 41., 41., 41., 41., 41., 41., 21., 21., 21., 21., 21., 
  21., 41., 41., 41., 41., 41., 41., 41., 41., 41., 41., 41., 41., 
  41., 41., 41., 41., 41., 41., 41., 41., 41., 41., 41., 11., 11., 
  11., 11., 11., 11., 11., 11., 11., 11., 11., 11., 41., 41., 21., 
  52., 52., 52., 52., 52., 52., 41., 41., 41., 41., 41., 41., 41., 
  52., 52., 52., 41., 52., 52., 52., 52., 41., 41., 41., 41., 41., 
  41., 41., 52., 52., 52., 21., 41., 41., 41., 41., 41., 41., 41., 
  41., 41., 41., 41., 41., 52., 52., 52., 52., 42., 42., 22., 90., 
  95., 90., 41., 41., 95., 11., 95., 95., 11., 95., 95., 95., 90., 
  90., 90., 90., 41., 90., 90., 95., 90., 90., 90., 41., 41., 81., 
  81., 81., 81., 81., 81., 81., 81., 81., 81., 81., 41., 71., 41., 
  41., 41., 21., 41., 11., 11., 11., 11., 11., 11., 11., 11., 11., 
  11., 11., 11., 11., 11., 11., 11., 11., 11., 11., 11., 11., 11., 
  11., 11., 11., 90., 90., 90., 90., 21., 21., 41., 41., 41., 41., 
  41., 95., 95., 90., 95., 95., 95., 41., 71., 71.}}

Colors

{{"0.", RGBColor["0.00000000000", "0.00000000000", 
   "0.00000000000"]}, {"11.", 
  RGBColor["0.27843137255", "0.41960784314", 
   "0.62745098039"]}, {"12.", 
  RGBColor["0.81960784314", "0.86666666667", 
   "0.97647058824"]}, {"21.", 
  RGBColor["0.86666666667", "0.78823529412", 
   "0.78823529412"]}, {"22.", 
  RGBColor["0.84705882353", "0.57647058824", 
   "0.50980392157"]}, {"23.", 
  RGBColor["0.92941176471", "0.00000000000", 
   "0.00000000000"]}, {"24.", 
  RGBColor["0.66666666667", "0.00000000000", 
   "0.00000000000"]}, {"31.", 
  RGBColor["0.69803921569", "0.67843137255", 
   "0.63921568628"]}, {"41.", 
  RGBColor["0.40784313726", "0.66666666667", 
   "0.38823529412"]}, {"42.", 
  RGBColor["0.10980392157", "0.38823529412", 
   "0.18823529412"]}, {"43.", 
  RGBColor["0.70980392157", "0.78823529412", 
   "0.55686274510"]}, {"51.", 
  RGBColor["0.64705882353", "0.54901960784", 
   "0.18823529412"]}, {"52.", 
  RGBColor["0.80000000000", "0.72941176471", 
   "0.48627450980"]}, {"71.", 
  RGBColor["0.88627450980", "0.88627450980", 
   "0.75686274510"]}, {"72.", 
  RGBColor["0.78823529412", "0.78823529412", 
   "0.46666666667"]}, {"73.", 
  RGBColor["0.60000000000", "0.75686274510", 
   "0.27843137255"]}, {"74.", 
  RGBColor["0.46666666667", "0.67843137255", 
   "0.57647058824"]}, {"81.", 
  RGBColor["0.85882352941", "0.84705882353", 
   "0.23921568628"]}, {"82.", 
  RGBColor["0.66666666667", "0.43921568628", 
   "0.15686274510"]}, {"90.", 
  RGBColor["0.72941176471", "0.84705882353", 
   "0.91764705882"]}, {"95.", 
  RGBColor["0.43921568628", "0.63921568628", "0.72941176471"]}}
$\endgroup$
  • $\begingroup$ try ArrayPlot[a, ColorRules -> (Rule@@@Colors)] or ArrayPlot[a /. (Rule@@@Colors)] ? $\endgroup$ – kglr Dec 20 '17 at 19:19
  • $\begingroup$ None of them work, it's still gray-scale :/. Maybe the problem comes from the data types of Colors. $\endgroup$ – pnz Dec 20 '17 at 19:26
  • $\begingroup$ pnz, could you post a small portion of your a and Colors? $\endgroup$ – kglr Dec 20 '17 at 19:34
  • $\begingroup$ Yes, of course. $\endgroup$ – pnz Dec 20 '17 at 19:55
1
$\begingroup$

Try with this, as you are importing strings:

Colors = ToExpression@ReadList["nlcd-colors.csv", {"Word", "Word", "Word", "Word"}, 
 WordSeparators -> {"\t", " ", ","}];
rules = ColorRules -> Flatten@{IntegerPart@ToExpression@#[[1]] -> 
   RGBColor[#[[2]], #[[3]], #[[4]]] & /@ Colors}
ArrayPlot[IntegerPart@a, rules]

enter image description here

Edit

Including the style of and suggestions by @kglr:

Colors = ReadList["nlcd-colors.csv", {"Word", {"Word", "Word", "Word"}}, 
WordSeparators -> {"\t", " ", ","}] /. x_String :> ToExpression[x];

rules = ColorRules -> Flatten@{IntegerPart@#[[1]] -> RGBColor @@ #[[2]] & /@ Colors};
ArrayPlot[IntegerPart@a, rules]
$\endgroup$
1
$\begingroup$

Your Colors have Strings inside. For example,

Colors[[2]]

{"11.", RGBColor["0.27843137255", "0.41960784314", "0.62745098039"]}

After replacing strings with expressions

Colors = Colors /. x_String :> ToExpression[x];

and using

ArrayPlot[a, AspectRatio -> 1/10, ColorRules -> (Rule @@@ Colors)]

or

ArrayPlot[a /. (Rule @@@ COlors), AspectRatio -> 1/10]

we get

enter image description here

$\endgroup$
  • $\begingroup$ I have to learn many things from all like you when using WL elegantly ;)).. $\endgroup$ – José Antonio Díaz Navas Dec 20 '17 at 21:09
  • $\begingroup$ @JoséAntonioDíazNavas, thank you for the upvote and kind words. $\endgroup$ – kglr Dec 20 '17 at 21:21
  • $\begingroup$ Colors = ToExpression@ ReadList[ "d:\\bitbucket\\diversity\\data\\nlcd\\nlcd-colors.csv", {Word, RGBColor[Word, Word, Word]} /. x_String :> ToExpression[x], WordSeparators -> {"\t", " ", ","}]; ToExpression::notstrbox: RGBColor[0.00000000000,0.00000000000,0.00000000000] is not a string or a box. ToExpression can only interpret strings or boxes as Wolfram Language input. Unfortunately, it's not working :/ $\endgroup$ – pnz Dec 20 '17 at 21:28

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.