# Use Show to Display an Arbitrary Number of Objects

(As always, feel free to inform me on how I can better ask questions if my formatting isn't preferred)

Just to use an arbitrary case, say I have a function in Mathematica

cylinder[i_]:=RegionPlot3D[x^2+y^2 <= 1 && i-1 <= z <= i, {x,-1,1},{y,-1,1},{z,0,10}];

And say I don't know what i should be yet other than it will be an integer between 1 and 10. How would I use the Show function to display an unknown number of the cylinder functions?

My attempt at a solution came in the form

n=5(*User defines n*); Show[For[j=1,j<=n,j++,cylinder[j]]]

• why not do Show[cylinder[#] & /@ Range[1, n]] ? For loops are not recommended in Mathematica :) !Mathematica graphics Mar 21, 2022 at 21:09
• This works with my code! Although, can you explain the code in English? I am unsure what & /@ is supposed to represent specifically. Mar 21, 2022 at 21:49
• it is a map command, same as writing Show[Map[cylinder[#] &, Range[1, n]]] you are mapping or applying your function to Range 1 to n. So instead of applying the function one by one in a loop as you were doing, you can use map and it is done automatically for you. Mar 21, 2022 at 21:59
• I'll definitely need to re-read this a couple times, but I appreciate your time explaining!! Mar 21, 2022 at 22:37
• The map command is one of the most used commands in Mathematica. If you have function f[x] and want to apply it on list of values {1,2,3,4} one by one, then you can use Map for this. Just look up help on Mathematica Map and you'll see many examples. Mar 21, 2022 at 22:45

For the specific example, multiple cylinders can be included in a single RegionPlot3D

Clear["Global`*"]

cylinder[i_] :=
RegionPlot3D[
x^2 + y^2 <= 1 && i - 1 <= z <= i, {x, -1, 1}, {y, -1, 1}, {z, 0, 10}]

Manipulate[
RegionPlot3D[x^2 + y^2 <= 1 &&
Or @@ ((# - 1 <= z <= #) & /@ cyl),
{x, -1, 1}, {y, -1, 1}, {z, 0, 10},
PlotPoints -> 75,
MaxRecursion -> 4],
{{cyl, {1,5,9}, "cylinders"},
(# -> (# - 1 <= z <= #)) & /@ Range[10],
ControlType -> CheckboxBar}]

• May be a bit much for my use case, but definitely great study material to better use Mathematica in the future! Thank you for your time! Mar 21, 2022 at 22:40

You already have a function that takes a single integer, and you want a function that applies that function to a range of integers. This is a perfect fit for Array:

cylinders[n_] := Array[cylinder, n]

This, of course, gives a list. You can wrap it in Show:

Show[cylinders[3]]

to keep cylinders independent of display, or you could make Show part of the definition if you don't need that independence:

cylinders[n_] := Show[Array[cylinder, n]]
• This worked with my code! Thank you so much! Mar 21, 2022 at 21:43