# How to separate a plot into grids

I have made a plot using the built in function DensityPlot using the code below

DensityPlot[(E^-(x^2 +
y^2)^2)^2 + ((E^-(x^2 + y^2)^2) (x^2 + y^2) Cos[
2 Pi])^2, {x, -3, 3}, {y, -3, 3}, PlotTheme -> "Minimal",
PlotRange -> All, PlotPoints -> 50, ColorFunction -> "Rainbow"]


Which generated this plot here

Now what I would like to is separate the plot into four grids (or n grids). The reason I would like to have these four grids, is that ultimately I would use the center point of each grid (some x,y value) and substitute it in a formula to get an ellipse for that grid. Hopefully the why, helps a bit.

I have tried reading the documentation online, but I have not been able to find anything that can help me with this specific task.

• "The reason I would like to have these four grids, is that ultimately I would use the center point of each grid (some x,y value) and substitute it in a formula to get an ellipse for that grid" this sounds like the question you actually mean to ask. Are you sure you don't want to ask bout how to solve that one? Commented May 28, 2021 at 17:50

Update: To get the centers of grid cells, you can simply work with the plot range:

means[n_] := MovingAverage[Subdivide[##, n] & @@ #, 2] &

centers[{nc_, nr_}, {xrange_, yrange_}] := Tuples[{means[nc]@xrange, means[nr]@yrange}]

{xrange, yrange} = {{-3, 3}, {-3, 3}};

{nc, nr} = {2, 2};

centers[{nc, nr}, {xrange, yrange}]

{{-(3/2), -(3/2)}, {-(3/2), 3/2}, {3/2, -(3/2)}, {3/2, 3/2}}

DensityPlot[(E^-(x^2 + y^2)^2)^2 + ((E^-(x^2 + y^2)^2) (x^2 + y^2) Cos[2 Pi])^2,
{x, -3, 3}, {y, -3, 3}, PlotTheme -> "Minimal",
PlotRange -> All, PlotPoints -> 50, ColorFunction -> "Rainbow",
Mesh -> {nc, nr} - 1, MeshStyle -> White,
Epilog -> {White, PointSize[Medium],
Tooltip[Point @ #, #] & /@ centers[{nc, nr}, {xrange, yrange}]}]


Using {nc, nr} = {4, 6}; we get

dp = DensityPlot[(E^-(x^2 + y^2)^2)^2 + ((E^-(x^2 + y^2)^2) (x^2 + y^2) Cos[2 Pi])^2,
{x, -3, 3}, {y, -3, 3}, PlotTheme -> "Minimal",
PlotRange -> All, PlotPoints -> 50, ColorFunction -> "Rainbow"]


You can use Show with the option PlotRange to get the sections of dp you want:

ClearAll[divs, pieces]
divs[n_] := Partition[Subdivide[##, n] & @@ #, 2, 1] &

pieces[g_, {nc_, nr_}, {xrange_, yrange_}] :=
Show[g, PlotRange -> #, AspectRatio -> Automatic] & /@
Tuples[{divs[nc]@xrange, divs[nr]@yrange}];


Examples:

{xrange, yrange} = {{-3, 3}, {-3, 3}};

{nc, nr} = {2, 2};

GraphicsGrid[Reverse @ Transpose @
ArrayReshape[pieces[dp, {nc, nr}, {xrange, yrange}], {nc, nr}],
Spacings -> {0, 0}, Dividers -> All, FrameStyle -> White]


Use {nc, nr} = {4, 6} to get

Since you say that you want four grids but do not specify the points, let me show you something that is perhaps a good starting point.

DensityPlot[(E^-(x^2 + y^2)^2)^2 + ((E^-(x^2 + y^2)^2) (x^2 +
y^2) Cos[2 Pi])^2, {x, -3, 3}, {y, -3, 3},
PlotTheme -> "Minimal", PlotRange -> All, PlotPoints -> 50,
ColorFunction -> "Rainbow", GridLines -> Automatic,
Method -> {"GridLinesInFront" -> True},
GridLinesStyle -> Directive[Black, Thick]]


which gives

You can look up GridLines in the documentation to make it look better.

• Thanks for your answer! By any chance do you know how I might be able to get the center point of each grid? Commented May 28, 2021 at 12:05
• @PracticalFruit do you mean something like getting the coordinates for the central point of each single grid?
– user49048
Commented May 28, 2021 at 13:33
• That is exactly, what I want to do Commented May 28, 2021 at 21:08
• @PracticalFruit sorry but I have not managed to do it yet
– user49048
Commented May 28, 2021 at 21:22

If I understand your question right, you intend to partition your image.

pic="your image"
ImagePartition[pic, ImageDimensions[pic]/2]