0
$\begingroup$

I have a code which reproduces some mathematical calculation. And it goes like this:

ClearAll["Global`*"]
HeadingForTable = {"x",
   "R1", "R1A1", "R1A2", "R1A3", "R1A4",
   "R2", "R2A1", "R2A2", "R2A3", "R2A4",
   "R3", "R3A1", "R3A2", "R3A3", "R3A4"};
R2A1 = {-1, -2, -3, -4, -50, -60, -70, -80, -90, -7, -6, -5, -4, -3, \
-2, -1, 0, 1, 2, 3};
R2A3 = {0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.007, 0.008, 0.009, 0.01, 
   0.011, 0.012, 0.013, 0.014, 0.015, 0.016, 0.017, 0.018, 0.019, 
   0.02};
TableFormat = 
  Table[{i, 
    1100 + (i - 1)*50, -135 - (i - 
        1)*10, (-135 - (i - 1)*10)*-10*0.25, -0.00056 - (i - 
        1)*0.00001, (-0.00056 - (i - 1)*0.00001)*-10*0.5, 
    2100 + (i - 1)*50, R2A1[[i]], R2A1[[i]]*10*56, R2A3[[i]], 
    R2A3[[i]]/100, 3300 + (i - 1)*50, 
    0.001 + (i)*0.001, -1*(0.001 + (i)*0.001), 10*i, -10*i}, {i, 1, 
    20}];
RowLength = Length[TableFormat];
TableForm[Join[{HeadingForTable}, TableFormat]]

This table consists of 20 rows and 16 columns. Now, I first need to plot a graph with 1st column as x axis and 2nd, 7th and 12th column in y-axis, which can be accomplished by:

xaxis = Table[TableFormat[[i, 1]], {i, 1, RowLength}];
YAxis1 = Table[TableFormat[[i, 2]], {i, 1, RowLength}];
YAxis2 = Table[TableFormat[[i, 7]], {i, 1, RowLength}];
YAxis3 = Table[TableFormat[[i, 12]], {i, 1, RowLength}];

ListLinePlot[Transpose[{xaxis, #}] & /@ {YAxis1, YAxis2, YAxis3}, 
 PlotRange -> Full, ImageSize -> Large]

giving the plot:

enter image description here

I then want to plot the same with certain conditions imposed:

for the 2nd (7th and 12th ) column, I want to plot only when the absolute value of 3rd (8th and 13th) and 4th (9th and 14th ) column becomes greater than 10 and absolute value of 5th (10th and 15th) and 6th (11th and 16th) column less than 0.01.

UpperCutoff = 10;
LowerCutoff = 0.01;

TableFor1stRoot = Select[TableFormat,
   Abs[#[[3]]] > UpperCutoff && Abs[# [[4]]] > UpperCutoff && 
     Abs[# [[5]]] < LowerCutoff && Abs[# [[6]]] < LowerCutoff &];
TableFor2ndRoot = Select[TableFormat,
   Abs[#[[8]]] > UpperCutoff && Abs[# [[9]]] > UpperCutoff && 
     Abs[# [[10]]] < LowerCutoff && Abs[# [[11]]] < LowerCutoff &];
TableFor3rdRoot = Select[TableFormat,
   Abs[#[[13]]] > UpperCutoff && Abs[# [[14]]] > UpperCutoff && 
     Abs[# [[15]]] < LowerCutoff && Abs[# [[16]]] < LowerCutoff &];

SiftedTable1stRoot = TableFor1stRoot[[All, {1, 2}]] ;
SiftedTable2ndRoot = TableFor2ndRoot[[All, {1, 7}]] ;
SiftedTable3rdRoot = TableFor3rdRoot[[All, {1, 12}]] ;

RowLength1stRoot = Length[SiftedTable1stRoot];
RowLength2ndRoot = Length[SiftedTable2ndRoot];
RowLength3rdRoot = Length[SiftedTable3rdRoot];

xaxis1stRoot = 
  Table[SiftedTable1stRoot [[i, 1]], {i, 1, RowLength1stRoot}];
yaxis1stRoot = 
  Table[SiftedTable1stRoot [[i, 2]], {i, 1, RowLength1stRoot}];
xaxis2ndRoot = 
  Table[SiftedTable2ndRoot [[i, 1]], {i, 1, RowLength2ndRoot}];
yaxis2ndRoot = 
  Table[SiftedTable2ndRoot [[i, 2]], {i, 1, RowLength2ndRoot}];
xaxis3rdRoot = 
  Table[SiftedTable3rdRoot [[i, 1]], {i, 1, RowLength3rdRoot}];
yaxis3rdRoot = 
  Table[SiftedTable3rdRoot [[i, 2]], {i, 1, RowLength3rdRoot}];

ListLinePlot[Transpose[{xaxis1stRoot, #}] & /@ {yaxis1stRoot}, 
 AxesStyle -> Directive[GrayLevel[0], AbsoluteThickness[1.6]], 
 LabelStyle -> {16, GrayLevel[0]}]
ListLinePlot[Transpose[{xaxis2ndRoot, #}] & /@ {yaxis2ndRoot}, 
 AxesStyle -> Directive[GrayLevel[0], AbsoluteThickness[1.6]], 
 LabelStyle -> {16, GrayLevel[0]}]
ListLinePlot[Transpose[{xaxis3rdRoot, #}] & /@ {yaxis3rdRoot}, 
 AxesStyle -> Directive[GrayLevel[0], AbsoluteThickness[1.6]], 
 LabelStyle -> {16, GrayLevel[0]}]

This gives the plot:

enter image description here

Since, for the 12th column, these conditions will not get satisfied (and for the same reason I cannot give all these conditions simultaneously), we will get empty plot. I could do these by checking the conditions individually (as shown in the above code), but is there any way of doing it any simpler and combining the plots together? I am expecting a plot somewhat like this:

enter image description here

Thanks in advance.

$\endgroup$
1
$\begingroup$
$Version

(* "12.3.0 for Mac OS X x86 (64-bit) (May 10, 2021)" *)

Clear["Global`*"]

HeadingForTable = {"x", "R1", "R1A1", "R1A2", "R1A3", "R1A4", "R2", "R2A1", 
   "R2A2", "R2A3", "R2A4", "R3", "R3A1", "R3A2", "R3A3", "R3A4"};
R2A1 = {-1, -2, -3, -4, -50, -60, -70, -80, -90, -7, -6, -5, -4, -3, -2, -1, 
   0, 1, 2, 3};
R2A3 = {0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.007, 0.008, 0.009, 0.01, 0.011, 0.012,
    0.013, 0.014, 0.015, 0.016, 0.017, 0.018, 0.019, 0.02};
TableFormat = 
  Table[{i, 
    1100 + (i - 1)*50, -135 - (i - 
        1)*10, (-135 - (i - 1)*10)*-10*0.25, -0.00056 - (i - 
        1)*0.00001, (-0.00056 - (i - 1)*0.00001)*-10*0.5, 2100 + (i - 1)*50, 
    R2A1[[i]], R2A1[[i]]*10*56, R2A3[[i]], R2A3[[i]]/100, 3300 + (i - 1)*50, 
    0.001 + (i)*0.001, -1*(0.001 + (i)*0.001), 10*i, -10*i}, {i, 1, 20}];
RowLength = Length[TableFormat];
TableForm[Join[{HeadingForTable}, TableFormat]];

Use Part to extract columns from a matrix

ListLinePlot[TableFormat[[All, {1, #}]] & /@ {2, 7, 12}, PlotRange -> Full, 
 ImageSize -> Medium]

enter image description here

sel[n_] := 
 Pick[TableFormat[[All, {1, n}]], 
  Thread[(And @@ Thread[Greater[Abs[#], 10]] & /@ 
      TableFormat[[All, {n + 1, n + 2}]]) && (And @@ 
        Thread[Less[Abs[#], 0.01]] & /@ TableFormat[[All, {n + 3, n + 4}]])]]

ListLinePlot[(sel /@ {2, 7, 12}) /. {} :> Nothing,
 PlotRange -> {800, 2800}, ImageSize -> Medium]

enter image description here

EDIT: Combining,

Show[
 ListLinePlot[
  Callout[
     TableFormat[[All, {1, #}]],
     HeadingForTable[[#]]] & /@ {2, 7, 12},
  PlotStyle -> Dotted,
  PlotRange -> Full,
  ImageSize -> Medium],
 ListLinePlot[
  (sel /@ {2, 7, 12}) /. {} :> Nothing,
  PlotStyle -> Thick,
  ImageSize -> Medium]]

enter image description here

$\endgroup$
2
  • $\begingroup$ Thanks, it worked. But could you please tell me what {} :> Nothing does and also what would be arguments of sel[n_]? Is it 2,7,12 ? $\endgroup$ – sreeraj t Jun 9 at 2:44
  • $\begingroup$ See documentation for Map sel /@ {2, 7, 12} maps sel onto the arguments {2, 7, 12} Since sel[12] evaluates to an empty list {} and this would interfere with plotting, {} :> Nothing removes the empty list. See documentation for Nothing When there is a symbol or operator that you don't understand, highlight it in Mathematica and press F1 for help. $\endgroup$ – Bob Hanlon Jun 9 at 2:59

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.