Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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!

share|improve this question
1  
You've tried PlotRangePadding -> None and Method -> {"ShrinkWrap" -> True}? –  J. M. Sep 11 '12 at 2:36
    
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. –  ShaunH Sep 11 '12 at 3:00
    
@ShaunH You can find it here mathematica.stackexchange.com/q/809/193 –  belisarius Sep 11 '12 at 3:21
    
@J.M. I think that one is good enough for an answer –  belisarius Sep 11 '12 at 3:24
    
@J.M. I should stop answering questions. Where did you dig this up? –  halirutan Sep 11 '12 at 3:46
show 5 more comments

3 Answers 3

up vote 5 down vote accepted

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

share|improve this answer
    
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). –  ShaunH Sep 11 '12 at 2:58
    
@ShaunH The crop IS tight, but in 3D. –  belisarius Sep 11 '12 at 3:01
    
that definitely helps, but in the plot that I am after, it crops some of the object (see my edit to the question). –  ShaunH Sep 11 '12 at 4:39
    
@ShaunH See edit –  belisarius Sep 11 '12 at 4:47
1  
The cropped half-sphere image still seems to be cut off by a few pixels on the lower border... –  Yves Klett Sep 11 '12 at 7:51
show 1 more comment

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

share|improve this answer
add comment

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.

share|improve this answer
    
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+ –  halirutan Sep 11 '12 at 3:26
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.