5
$\begingroup$

I have the data:

data={{r,a,b},{0,1.06471,1.16909},{1,1.06463,1.169},{2,1.06456,1.06456},{3,1.06448,1.16884},{4,1.0644,1.16875},{5,1.06433,1.06433},{6,1.06425,1.06425},{7,1.06418,1.1685},{8,1.0641,1.16841}}

I want to draw a thick yellow line when a=b and a thick red line when a>b or b>a, for each value of r. The output will look something like this (for a larger dataset): enter image description here

Any help would really be appreciated.

$\endgroup$
2
  • 1
    $\begingroup$ I don't see any documentation for ConditionalPlot. Where did you run across that symbol? $\endgroup$
    – lericr
    Commented Jul 31, 2023 at 15:34
  • $\begingroup$ Oh! then that does not work. $\endgroup$ Commented Jul 31, 2023 at 15:37

5 Answers 5

5
$\begingroup$

We first evaluate the colors by:

data = {{0, 1.06471, 1.16909}, {1, 1.06463, 1.169}, {2, 1.06456, 
    1.06456}, {3, 1.06448, 1.16884}, {4, 1.0644, 1.16875}, {5, 
    1.06433, 1.06433}, {6, 1.06425, 1.06425}, {7, 1.06418, 
    1.1685}, {8, 1.0641, 1.16841}};
dat = data /. {r_, a_, b_} -> {r, If[a == b, Yellow, Red]}

enter image description here

And now we draw the lines:

Graphics[{Thickness[0.12], #[[2]], Line[{{#[[1]], 0}, {#[[1]], 1}}]} & /@ dat]

enter image description here

$\endgroup$
5
  • $\begingroup$ I'm getting the error: " Coordinate {$CellContext`r, 0} should be a pair of numbers, or a Scaled or Offset form. " $\endgroup$ Commented Jul 31, 2023 at 16:31
  • $\begingroup$ Did you eliminate the first sublist: {r,a,b} in your data? $\endgroup$ Commented Jul 31, 2023 at 16:57
  • $\begingroup$ Oh yes, it works now. How can I check for the sign of a and b and not their magnitude? (If sign(a)=sign(b) then yellow line, if not equal then a red line) @Daniel Huber $\endgroup$ Commented Aug 1, 2023 at 7:29
  • $\begingroup$ Try If[Sign[a]== Sign[b],...] $\endgroup$ Commented Aug 1, 2023 at 8:35
  • $\begingroup$ Yes it works. Thank you so much! @Daniel Huber $\endgroup$ Commented Aug 1, 2023 at 9:16
8
$\begingroup$

Transform data into a list of colors:

colorlist = data[[2 ;;, 2 ;;]] /. { {a_, a_} -> Yellow, {_, _} -> Red}

enter image description here

1. ArrayPlot

You can use colorlist as the first argument of ArrayPlot

ArrayPlot[{colorlist}, ImageSize -> Large]

enter image description here

MatrixPlot[{colors}] gives a similar result.

2. Image

Alternatively, use it in the first argument of Image:

Image[{colorlist}, ImageSize -> Large]

enter image description here

3. Raster + Graphics

Finally, you can use it with a combination of Raster and Graphics:

Graphics[Raster[{List @@@ colorlist}], ImageSize -> Large]

enter image description here

$\endgroup$
6
$\begingroup$

You could start with something like this:

ArrayPlot[{Boole[Equal @@@ data[[2 ;;, 2 ;;]]]}, ColorRules -> {1 -> Yellow, 0 -> Red}]

Looks like we can force it to work with True|False values:

ArrayPlot[{Equal @@@ data[[2 ;;, 2 ;;]]}, ColorRules -> {True -> Yellow, False -> Red}]
$\endgroup$
2
  • $\begingroup$ As I increase the dataset, the plot is getting extremely thin. $\endgroup$ Commented Jul 31, 2023 at 16:04
  • 2
    $\begingroup$ @Math_student, use AspectRatio option. $\endgroup$
    – Domen
    Commented Jul 31, 2023 at 16:14
6
$\begingroup$
dt = Join[#, #, #, #, #] &[Rest @ data];

MatrixPlot[{Boole[#[[2]] == #[[3]] & /@ dt]}, 
 ColorRules -> {1 -> Yellow, 0 -> Red}, AspectRatio -> 1/4]

enter image description here

$\endgroup$
0
$\begingroup$

Using ImageAssemble:

cols = If[Equal[#[[2]], #[[3]]], Yellow, Red] & /@ (Rest@data)
ImageAssemble[ConstantImage[#, {20, 50}] & /@ cols]

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.