How can I specify aspect ratio in BubbleChart3D so as to keep the bubbles spherical?

A somewhat hacky fix is to inspect what's inside and change it:

postprocessBubbleChart3D[in_] := 
  With[{br = Quiet[BoxRatios /. AbsoluteOptions[in, BoxRatios]]},
    (
      in /. GeometricTransformation3DBox[{s_SphereBox}, {m_?MatrixQ, v_}] :> 
        GeometricTransformation3DBox[s, {m/br, v}]

    ) /; VectorQ[br]
  ]

postprocessBubbleChart3D[in_] := in

Some fake data:

chart = BubbleChart3D[Select[RandomReal[1, {50, 4}], Min[#[[2]], #[[3]]] > 0.5 &], BoxRatios -> Automatic];

Test:

{chart, postprocessBubbleChart3D[chart]}

enter image description here


Here is another way to do it. I define a chart element function that makes the bubbles ellipsoids having principal axes calculated to reverse the distortion introduced by changing the z-axis box ratio.

coords = Flatten[CoordinateBoundingBoxArray[{{0, 0, 0}, {3, 3, 2}}], 2];
SeedRandom[42]; vals = RandomReal[1, Length@coords];
bubbles = MapThread[{Sequence @@ #1, #2} &, {coords, vals}];

With[{zAspect = .3, λ = .2}},
  BubbleChart3D[bubbles,
    ChartElementFunction -> 
      (Ellipsoid[#2[[;; 3]], λ #2[[4]] {1, 1, 1/zAspect/GoldenRatio}] &),
    Axes -> True,
    AxesEdge -> {{-1, -1}, {-1, -1}, {-1, -1}},
    AxesLabel -> {"X", "Y", "Z"},
    BubbleScale -> "Diameter",
    FaceGridsStyle -> Directive[Gray, Dashed],
    Boxed -> True,
    ColorFunction -> (GrayLevel[.3 + .7 (1 - #1), 0.5] &),
    Lighting -> "Neutral",
    BoxRatios -> {1, 1, zAspect},
    PlotRangePadding -> Scaled[.04],
    ImageSize -> 500]]

bubbles

Tags:

Graphics