0
$\begingroup$

in most of my job I have to repeat something for different part of data, for example making plot, usually the only thing need to change is index. Below is the example of the code which I have to change it manually, I want to know how to use for loop, or another method, to make the change automatically.

Rse1xytyp1 = Select[section1, #[[5]] == 1 &];
se1xytyp1 = SortBy[Rse1xytyp1[[All, {3, 4}]], Last];
Length[se1xytyp1]
se1Arctyp1 = SortBy[Rse1xytyp1[[All, {2, 5}]], Last];
Length[se1Arctyp1]
s1xyplot1 = 


ListPlot[se1xytyp1, PlotStyle -> RGBColor[1.0000, 0, 0.3647],
AxesLabel -> {"X", "Y"},
PlotLabels -> Placed[{"Type 1, Section 1"}, Above]];

s1arcplot1 =
ListPlot[se1Arctyp1, PlotStyle -> RGBColor[1.0000, 0, 0.3647],
 AxesLabel -> {ArcLength, "Type"},
PlotLabels -> Placed[{"Type 1, Section 1"}, Above]];

In This code, in first line Rse1xytyp1 = Select[section1, #[[5]] == 1 &]; I need to change the #[[5]] == 1 & from 1 to 15 then plot everything. Also I like to change the plot's names too. How can I do it?

$\endgroup$
2

1 Answer 1

2
$\begingroup$

I would like to suggest not using a for loop neither naming each plot with a different name. First, create a function that does everything you need, which takes this variable quantity as an argument and returns a list with the two plots:

selectAndPlot[sect_,key_]:= 
Module[{Rse1xytyp1,se1xytyp1,selArctyp1,s1xyplot1,s1arcplot1},
Rse1xytyp1 = Select[sect, #[[5]] == key &];
(* here follows all the code you have included in the question *)
{s1xyplot1,s1arcplot1}
] (* closes Module[] *)

Then, Map[ ] this function in the required range and keep the result in a list:

plotList=selectAndPlot[#,section1]&/@Range[1,15]; 
(* keep this semicolon to avoid useless output *)

Finally, generate any array of graphics you need. Note that each plot has a single id, for instance plotList[[3,2]] is s1arcplot1 for key==3. Having all plots in a list helps:

GraphicsGrid[plotList[[1;;3]] ]  
(* draws a stacked pair of the two plots for keys 1, 2, and 3 *)

I could not test the program, since I don't have the data, hence this code snippet may have typos or mistakes; I hope the general idea is clear.

$\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.