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

I would like to make a 3D bubble chart with an aspect ratio of 0.3. But when I give BubbleChart3D the option AspectRatio -> 0.3, the bubbles aspect ratio also changes, squashing the bubbles. I have wasted much time and I didn't get the desired result. I tried to apply functions like Scale and set BoxRatios but didn't get a positive result.

How can I cope with this problem?

BubbleChart3D[
{Join[mean[[;; , ;; 3]], Partition[σ, 1], 2]}~Flatten~1
, Axes -> True
, AxesEdge -> {{-1, -1}, {-1, -1}, {-1, -1}}
, AxesLabel -> {"X", "Y", "Z"}
, BubbleScale -> "Diameter"
, ViewVertical -> {0, 1, 0}
, ViewPoint -> {-1, 0., 0.}
, FaceGrids -> {{0, 0, -1}, {0, -1, 0}}
, FaceGridsStyle -> Directive[Gray, Dashed]
, Boxed -> True
, ColorFunction -> (GrayLevel[0.7 - 0.7 #1, 0.7] &)
, Lighting ->
{{"Ambient", LightGray}, {"Directional", Gray, {20000, 15000, 100000}}}
, AspectRatio -> 0.3
, BoxRatios -> Automatic
, ImageSize -> {600, 200}
]


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},
ImageSize -> 500]]


• Huge help, thank you. I am going to be that person who points out the nearly-insignificant typo. You have "With[{zAspect = .3, λ = .2}}," and it needs to be "With[{zAspect = .3, λ = .2}," . – elscan Jun 16 at 12:01
• Ah, something which is a little more worth mentioning, looks like if you use "ChartElementFunction -> (Ellipsoid[#2[[;; 3]], 0.3 #2[[4]] {1, 1, 1}] &)," at the very end (say, after "BoxRatios -> {1, 1, zAspect},") then the shapes are perfect spheres again. – elscan Jun 16 at 12:05

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