19
$\begingroup$

I like Mathematica, but it's syntax baffles me.

I am trying to figure out how to minimize the whitespace around a graphic.

For example,

ParametricPlot3D[{r Cos[t], r Sin[t], r^2}, {r, 0, 1}, {t, 0, 2 \[Pi]}, 
 Boxed -> True, Axes -> False]

3d bounding box on

Puts the 3d bounding box at the limits of the view. But if I don't show the 3d bounding box,

3d bounding box on

ParametricPlot3D[{r Cos[t], r Sin[t], r^2}, {r, 0, 1}, {t, 0, 2 \[Pi]}, 
 Boxed -> False, Axes -> False]

3d bounding box off

there is all this white space around the actual object.

Is there some way (syntax) that can put the view just around the visible objects?

Edit in response to answers

Ok, from the below answers, I have two solutions; 1) use ImageCrop, or 2) use Method->{"ShrinkWrap" -> True}. However both of these options do a little something strange to the plot I want (maybe it is just a problem with the plot itself).

So the actual plot I am after is,

Module[{r = 1, \[Theta] = \[Pi]/2, \[CurlyPhi] = \[Pi]/6, \[Psi] = \[Pi]/12},
Framed@Show[
 Graphics3D[
  {
   {Arrowheads[.025],
    Arrow[{{0, 0, 0}, {1.1, 0, 0}}], Text["x", {1.2, 0, 0}],
    Arrow[{{0, 0, 0}, {0, 1.1, 0}}], Text["y", {0, 1.2, 0}],
    Arrow[{{0, 0, 0}, {0, 0, 1.1}}], Text["z", {0, 0, 1.2}],
    Arrow[{{0, 0, 0}, r {Cos[\[Theta]] Sin[\[CurlyPhi]], 
      Sin[\[Theta]] Sin[\[CurlyPhi]], Cos[\[CurlyPhi]]}}]},
   {Specularity[White, 50], Opacity[.1], Sphere[{0, 0, 0}, r]}
   },
  Boxed -> False,
  ImageSize -> 600,
  PlotRange -> 1.1 {{-r, r}, {-r, r}, {0, r}}
  ]]
 ]

enter image description here

Which has too much whitespace. If I replace Framed@Show[ with Framed@ImageCrop@Show[ I get, enter image description here

which actually crops some of the (hemi)sphere. If just use Method -> {"ShrinkWrap" -> True}, in the Show options, I get,

Mathematica graphics

which looks almost correct, but the x and z textboxes have now not included. Seems like I can't win!

$\endgroup$
11
  • 4
    $\begingroup$ You've tried PlotRangePadding -> None and Method -> {"ShrinkWrap" -> True}? $\endgroup$ Sep 11, 2012 at 2:36
  • $\begingroup$ Hi @J.M., PLotRangePadding-> None didn't seem to do much, but the Method option did (so thanks!). I'm curious though, how was I ever supposed to find this in the help? This is what I mean by not understanding Mathematica syntax. $\endgroup$
    – ShaunH
    Sep 11, 2012 at 3:00
  • $\begingroup$ @ShaunH You can find it here mathematica.stackexchange.com/q/809/193 $\endgroup$ Sep 11, 2012 at 3:21
  • $\begingroup$ @J.M. I think that one is good enough for an answer $\endgroup$ Sep 11, 2012 at 3:24
  • 3
    $\begingroup$ I believe I've found a small hack to get the ShrinkWrap option approach working: I placed white points with Opacity[0.01] just past each of my axis labels (or other text that's being cut-off). I'm running Mathematica 10.2. P.S. I don't have the 50 reputation points needed to post a response in the appropriate place above, but solving this was very useful to me and perhaps will be to others, so I'm posting it here. $\endgroup$
    – jmc
    Aug 3, 2015 at 20:57

3 Answers 3

10
$\begingroup$

Actually, there isn't white space at all:

Show[RegionPlot3D[True, {x, -1, 1}, {y, -1, 1}, {z, 0, 1}, 
                  PlotStyle -> Directive[Yellow, Opacity[0.5]], Mesh -> None, 
                  Boxed -> False, Axes -> False, PlotRangePadding -> 0], 
     ParametricPlot3D[{r Cos[t], r Sin[t], r^2}, {r, 0, 1}, {t, 0, 2 Pi} ]]

Mathematica graphics

Edit

If you want to crop the image in 2D:

p = ParametricPlot3D[{r Cos[t], r Sin[t], r^2}, {r, 0, 1}, {t, 0, 2 Pi}, 
                     Boxed -> False, Axes -> False, PlotRangePadding -> 0];
Framed@ImageCrop@p

Mathematica graphics

Edit

For your plot. Use .2 as Opacity. It has been reported elsewhere in this site that lowering the opacity too much makes other functions unable to detect the object.

Module[{r = 
   1, \[Theta] = \[Pi]/2, \[CurlyPhi] = \[Pi]/6, \[Psi] = \[Pi]/12},
 Framed@ImageCrop@Show[
    Graphics3D[
     {{Specularity[White, 50], Opacity[.2], 
       Sphere[{0, 0, 0}, r]}, {Arrowheads[.025], 
       Arrow[{{0, 0, 0}, {1.1, 0, 0}}], Text["x", {1.2, 0, 0}], 
       Arrow[{{0, 0, 0}, {0, 1.1, 0}}], Text["y", {0, 1.2, 0}], 
       Arrow[{{0, 0, 0}, {0, 0, 1.1}}], Text["z", {0, 0, 1.2}], 
       Arrow[{{0, 0, 0}, 
         r {Cos[\[Theta]] Sin[\[CurlyPhi]], 
           Sin[\[Theta]] Sin[\[CurlyPhi]], Cos[\[CurlyPhi]]}}]}}, 
     Boxed -> False, ImageSize -> 600, 
     PlotRange -> 1.1 {{-r, r}, {-r, r}, {0, r}}]]]

Mathematica graphics

$\endgroup$
7
  • 1
    $\begingroup$ Hi @belisarius, fine, it is not whitespace. But this doesn't answer the question of how to make a tight crop around just the object(s). $\endgroup$
    – ShaunH
    Sep 11, 2012 at 2:58
  • $\begingroup$ @ShaunH The crop IS tight, but in 3D. $\endgroup$ Sep 11, 2012 at 3:01
  • $\begingroup$ that definitely helps, but in the plot that I am after, it crops some of the object (see my edit to the question). $\endgroup$
    – ShaunH
    Sep 11, 2012 at 4:39
  • $\begingroup$ @ShaunH See edit $\endgroup$ Sep 11, 2012 at 4:47
  • 1
    $\begingroup$ The cropped half-sphere image still seems to be cut off by a few pixels on the lower border... $\endgroup$
    – Yves Klett
    Sep 11, 2012 at 7:51
12
$\begingroup$

I think what you are looking for is ViewAngle option. The graph below compares default Automatic versus custom setting for ViewAngle. The image are framed intentionally to see clearly the removal of surrounding white space.

Framed[#, FrameMargins -> 0] & /@ 
     (ParametricPlot3D[{r Cos[t], r Sin[t], r^2}, {r, 0, 1}, {t, 0, 2 \[Pi]}, 
        Boxed -> False, Axes -> False, PlotRangePadding -> 0, 
        ImageSize -> 350 {1, 1}, ViewAngle -> #] & /@ {Automatic, .31}) 

enter image description here

Mathematica has rich set of options for so called "simulated camera" that "views" all 3D objects. To learn in detail about this i suggest downlading notebbok and watching free video of the @Yu-SungChang course Lights, Camera, Graphics! . Here is a illustrious diagram from that course explaining how ViewAngle option (measured in your example in 0.31 radians) sets up the view of a 3D objects.

enter image description here

You actually can change the ViewAngle setting interactively by holding CTRL or ALT key and dragging graphics with mouse - this effectively zooms in or out. See this tutorial for details.

$\endgroup$
1
  • $\begingroup$ You should point out, that this does not work automatically and that you have to adjust this for different viewing positions. But when I think about it: maybe the OP wasn't really interested in a Cropping->All solution and is fine to adjust it manually. 1+ $\endgroup$
    – halirutan
    Sep 11, 2012 at 3:26
2
$\begingroup$

ImageCrop seems to be a bit buggy (at least right here in Version 8.04, Win 64). It tends to crop lightly coloured areas rather agressively. You could try the following work-around, which works more reliably:

imcrop[img_] := ImagePad[img, -BorderDimensions[img, 0]]

g = Graphics3D[{Specularity[White, 50], Opacity[.1], 
    Sphere[{0, 0, 0}, 1]}, Boxed -> False, 
   PlotRange -> 1.1 {{-1, 1}, {-1, 1}, {0, 1}}
   ];
Column[Framed /@ {g, ImageCrop[g], imcrop[g]}]

Mathematica graphics

For your graphics it seems to work without additional changes to Opacity or similar:

g = Module[{r = 
     1, \[Theta] = Pi/2, \[CurlyPhi] = Pi/6, \[Psi] = Pi/12},
    Show[Graphics3D[{{Arrowheads[.025], 
       Arrow[{{0, 0, 0}, {1.1, 0, 0}}], Text["x", {1.2, 0, 0}], 
       Arrow[{{0, 0, 0}, {0, 1.1, 0}}], Text["y", {0, 1.2, 0}], 
       Arrow[{{0, 0, 0}, {0, 0, 1.1}}], Text["z", {0, 0, 1.2}], 
       Arrow[{{0, 0, 0}, 
         r {Cos[\[Theta]] Sin[\[CurlyPhi]], 
           Sin[\[Theta]] Sin[\[CurlyPhi]], 
           Cos[\[CurlyPhi]]}}]}, {Specularity[White, 50], Opacity[.1],
        Sphere[{0, 0, 0}, r]}}, Boxed -> False, ImageSize -> 600, 
     PlotRange -> 1.1 {{-r, r}, {-r, r}, {0, r}}]]];    

Framed@imcrop[g]

Mathematica graphics

$\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.