3
$\begingroup$

I am looking to plot a line of 250 discrete sites with a specified RGB color value based on the import file values. The import file has the following format:

import file format

I want the first element in each row to be the blue color value, and the 2nd element to be the red color value. For, example, the color of the first site would be (0.8, 0, 0.1) using RGB color.

I am trying to use MatrixPlot with the color function, but I can't seem to get it working. I cant seem to get the color function to line up with each site, and loop through all of the sites. I have a simple model up and running where the import file is just 1 column of 0,1,2 with 0=white, 1=red,2=blue, and looks like

simple model

I just can't seem to figure out how to do matrix plot for each site using the RGB value plot based on 2 values.

$\endgroup$
4
  • $\begingroup$ Each row of the import file is a site. So row 1 is site 1, row 2 is site 2, etc $\endgroup$
    – Sean
    Commented Jan 22, 2018 at 17:59
  • $\begingroup$ it would be helpful if you provide some code you have tried thus far together the data (or partial)... $\endgroup$ Commented Jan 22, 2018 at 18:02
  • $\begingroup$ list = {{0.8, 0.1}, {0.2, 0.4}, {0.3, 0.3}, {0.1, 0.1}, {0.9, 0.0}, {0.5, 0.1}, {0.1, 0.5}, {0.3, 0.4}, {0.4, 0.2}};RGBColor[#] & /@ (Insert[#, 0, 2] & /@ list) $\endgroup$ Commented Jan 22, 2018 at 18:13
  • $\begingroup$ (0.8, 0, 0.1) should be (0.1, 0, 0.8), since RGB={Red,Green,Blue} $\endgroup$ Commented Jan 22, 2018 at 18:27

4 Answers 4

2
$\begingroup$

With simulated data:

data = Table[{RandomReal[], RandomReal[]}, {n, 250}];
colorrules = Flatten@Table[{i -> (RGBColor[#[[2]], 0, #[[1]]] & /@ data)[[i]]}, 
{i, Length[data]}];
MatrixPlot[{Range[Length@data]}, ColorRules -> colorrules, 
FrameTicks -> {{None, None}, {Automatic, Automatic}}]

enter image description here

$\endgroup$
1
  • $\begingroup$ Awesome! This is exactly what I was looking for! Thank you Jose $\endgroup$
    – Sean
    Commented Jan 23, 2018 at 16:00
2
$\begingroup$

you can also use ColorFunction to specify color in MatrixPlot

data = RandomReal[{0, 1}, {100, 2}];
color[i_] := RGBColor[data[[i, 2]], 0, data[[i, 1]]]
MatrixPlot[{Range@Length@data}, ColorFunctionScaling -> False, 
 ColorFunction -> color ]
$\endgroup$
2
$\begingroup$

A matrix of number pairs:

data = RandomReal[1, {10, 10, 2}];

Insert 0 in the middle of each entry:

data2 = Apply[{#, 0, #2} &, data, {-2}];

Use directly (without having to wrap each entry with RGBColor) as input in Image:

Image[data2, ImageSize -> 300]

enter image description here

Or use the combination Graphics @ Raster@#:

Graphics[Raster[Reverse@data2], ImageSize -> 300]

enter image description here

Rows are reversed to get the same picture as the output of Image.

$\endgroup$
1
$\begingroup$
list = RandomReal[{0, 1}, {250, 2}];

col = # -> (RGBColor[#] & /@ (Insert[#, 0, 2] & /@ list))[[#]] & /@ 
   Range@Length@list;

ArrayPlot[{Range@Length@list}, ColorRules -> col, 
 FrameTicks -> {{All, All}, {Automatic, Automatic}}, ImageSize -> 800]

enter image description here

or MatrixPlot

MatrixPlot[{Range@Length@list}, ColorRules -> col, 
 FrameTicks -> {{All, All}, {Automatic, Automatic}}, ImageSize -> 800]

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.