3
$\begingroup$

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 enter image description 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.

I appreciate your help

$\endgroup$
1
  • 1
    $\begingroup$ "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? $\endgroup$
    – b3m2a1
    Commented May 28, 2021 at 17:50

3 Answers 3

4
$\begingroup$

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}]}]

enter image description here

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

enter image description here

Original answer:

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]

enter image description here

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

enter image description here

$\endgroup$
1
$\begingroup$

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

enter image description here

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

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

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

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

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.