How to create 2D (3D) animation in Wolfram Mathematica with the camera following the object? - animation

I have a graphical object which is moving along a trajectory. How can I make the camera follow the object?

Let's draw a planet and its satellite, with the camera following the moon from a view directed toward the Earth. For example:
a = {-3.5, 3.5};
Animate[
Show[
Graphics3D[
Sphere[3 {Cos#t, Sin#t, 0}, .5],
ViewPoint -> 3.5 {Cos#t, Sin#t, 0},
SphericalRegion -> True,
PlotRange -> {a, a, a}, Axes -> False, Boxed -> False],
myEarth],
{t, 0, 2 Pi}]
Where myEarth is another 3D Graphics (for reference).
Static vertical view:
a = {-3.5, 3.5};
Animate[
Show[
Graphics3D[
Sphere[3 {Cos#t, Sin#t, 0}, .5],
ViewPoint -> 3.5 {0,0,1},
SphericalRegion -> True,
PlotRange -> {a, a, a}, Axes -> False, Boxed -> False],
myEarth],
{t, 0, 2 Pi}]
The trick is SphericalRegion -> True, without it the image perspective "moves" from frame to frame.
Edit
With two static objects:

Since the question asks about 2D, here's how you can emulate a camera in 2D Graphics.
First, let's get the stackoverflow favicon.ico:
so = First#Import["http://sstatic.net/stackoverflow/img/favicon.ico"]
Well put this on top of some overlapping circles and make the "camera" follow the icon around by adjusting the PlotRange
Manipulate[Graphics[{
Table[Circle[{j, 0}, i], {i, 0, 1, .1}, {j, {-.5, .5}}],
Inset[so, pos, {0, 0}, .2]},
PlotRange -> {{-.5, .5}, {-.5, .5}} + pos],
{{pos, {0, 0}, ""}, {-1.4, -1}, {1.4, 1}, ControlPlacement -> Left}]
To show how it works (with out putting the above into Mathematica), we need to animate it.
Originally I chose a variable step random walk drunk = Accumulate[RandomReal[{-.1, .1}, {200, 2}]] but it was a unpredictable! So instead, we'll make the icon follow the ABC logo
drunk = Table[{1.5 Sin[t], Cos[3 t]}, {t, 0, 2 Pi, .1}];
Animate[Graphics[{
Table[Circle[{j, 0}, i], {i, 0, 1, .1}, {j, {-.5, .5}}],
Inset[so, drunk[[pos]], {0, 0}, .2]},
PlotRange -> {{-.5, .5}, {-.5, .5}} + drunk[[pos]]],
{pos, 1, Length[drunk], 1}]

Related

Could not combine the graphics objects in Show[

I get this error for the Show method, why? :/
sol = First#
NDSolve[{eq1ad, eq2ad, eqrad} U CondizioniIniziali, {q1, q2,
qr}, {t, 0, T}]
p1 = ParametricPlot3D[
{xE, yE, zE} /. sol,
{t, 0, T},
AxesLabel -> {"x[t]", "y[t]", "z[t]"},
BoxRatios -> {1, 1, 1},
PlotStyle -> Red
]
Manipulate[
Show[
p1,
ListLinePlot[
{{0, 0, 0}, {xB, yB, zB}, {xE, yE, zE}} /. sol /. t -> time,
PlotStyle -> {Thick, Red}
]
],
{time, 0, T}
]
Is it maybe because I can't combine a ParametricPlot3d with Show?
I think you are trying to combine a 2D ListLinePlot with a 3D ParametricPlot3D. Reading the documentation for ListLinePlot seems to show that it only accepts 2D points, not 3D points.
You might be able to adapt something like this
T=2;
p1 = ParametricPlot3D[{Sin[t],Cos[t],t^2}, {t,0,T}];
Show[p1, Graphics3D[ Line[{{0, 0, 0}, {1/2,1/2,2}, {1/3, 1/3,3}}]]]
which can turn a list of 3D points into a Line into a Graphics3D and then combine that your ParametricPlot3D

How can I select one out of several Graphics3D objects and change its coordinates in Mathematica?

In the accepted answer of question " Mathematica and MouseListener - developing interactive graphics with Mma " Sjoerd C de Vries demonstrates that it is possible to select an object in a 3D graphic and change its color.
I would like to know if it is possible (in a similar fashion as above) in a Graphics3D with two or more objects (e.g. two cuboids) to select one and change its coordinates (by moving or otherwise)?
I'm partly reusing Sjoerd's code here, but maybe something like this
DynamicModule[{pos10, pos11 = {0, 0, 0},
pos12 = {0, 0, 0}, pos20, pos21 = {0, 0, 0}, pos22 = {0, 0, 0}},
Graphics3D[{EventHandler[
Dynamic[{Translate[Cuboid[], pos11]}, ImageSize -> Tiny],
{"MouseDown" :> (pos10 = Mean#MousePosition["Graphics3DBoxIntercepts"]),
"MouseDragged" :> (pos11 =
pos12 + Mean#MousePosition["Graphics3DBoxIntercepts"] - pos10),
"MouseUp" :> (pos12 = pos11)}],
EventHandler[
Dynamic[{Translate[Cuboid[{1, 1, 1}], pos21]}, ImageSize -> Tiny],
{"MouseDown" :> (pos20 = Mean#MousePosition["Graphics3DBoxIntercepts"]),
"MouseDragged" :> (pos21 =
pos22 + Mean#MousePosition["Graphics3DBoxIntercepts"] - pos20),
"MouseUp" :> (pos22 = pos21)}]},
PlotRange -> {{-3, 3}, {-3, 3}, {-3, 3}}]]
Note that this just moves the cuboids in a plane so you would have to rotate the bounding box to move them perpendicular to that plane, but it shouldn't be too hard to introduce a third dimensions by adding modifier keys.
Edit
Thanks for the comments. Here's an updated version of the code above. In this version the cubes jump back to within the bounding box if they happen to move outside so that should solve the problem of the disappearing cubes.
DynamicModule[{init, cube, bb, restrict, generate},
init = {{0, 0, 0}, {2, 1, 0}};
bb = {{-3, 3}, {-3, 3}, {-3, 3}};
cube[pt_, scale_] :=
Translate[Scale[Cuboid[{-1/2, -1/2, -1/2}, {1/2, 1/2, 1/2}], scale], pt];
restrict[pt_] := MapThread[Min[Max[#1[[1]], #2], #1[[2]]] &, {bb, pt}];
generate[pos_, scale_] := Module[{mp, pos0, pos1, pos2},
mp := MousePosition["Graphics3DBoxIntercepts"];
pos1 = pos;
EventHandler[
Dynamic[{cube[pos1, scale]}, ImageSize -> Tiny],
{"MouseDown" :> (pos0 = LeastSquares[Transpose[mp], pos1].mp),
"MouseDragged" :>
((pos1 = #[[2]] + Projection[pos0 - #[[2]], #[[1]] - #[[2]]]) &#mp),
"MouseUp" :> (pos1 = restrict[pos1])}]];
Graphics3D[generate[#, 1] & /# init, PlotRange -> bb, PlotRangePadding -> .5]
]

Effect of change in Viewpoint->{x,y,z} on the size of graphic objects is not what I expected. How to fix?

If you run the following code snippet:
Manipulate[
Graphics3D[
{Cuboid[{{-1, -1, -1}, {1, 1, 1}}], Sphere[{5, 5, 5}, 1]},
ViewPoint -> {1, 1, a}, AxesOrigin -> {0,0,0}
],
{a, 1, 100}
]
and move the viewpoint from (1,1,1) to (1,1,100) with the slider you will see that after a while the objects remain fixed in size.
Questions.
1. When I move the viewpoint further away from the scene I want the objects to become smaller. How should this be done in Mathematica?
( EDIT: )
2. What is the position of the 'camera' in relation to Viewpoint?
See ViewAngle. Under "More Information", note that the default setting ViewAngle -> Automatic is effectively equivalent to ViewAngle -> All when you zoom far enough out.
You just need to add an explicit setting for ViewAngle:
Manipulate[
Graphics3D[{Cuboid[{{-1, -1, -1}, {1, 1, 1}}], Sphere[{5, 5, 5}, 1]},
ViewPoint -> {1, 1, a}, AxesOrigin -> {0, 0, 0},
ViewAngle -> 35 Degree], {a, 1, 100}]
As far as I know, the camera viewpoint really coincides with the position given by ViewPoint. Because Mathematica scales the result to fit in about the same image you don't see much changes but they are there. The perspective changes considerably. Try, for instance, to move away from a semi-transparant square and you'll see that the farther you go, the more the projection becomes an orthogonal projection:
If you want to scale your image according to distance you can use ImageSize. SphericalRegion is good to stabilize the image.
Manipulate[
vp = {1, 1, a};
Graphics3D[{Cuboid[{{-1, -1, -1}, {1, 1, 1}}], Sphere[{5, 5, 5}, 1]},
ViewPoint -> vp,
AxesOrigin -> {0, 0, 0},
SphericalRegion -> True,
ImageSize -> 500/Norm[vp]],
{a, 1, 100}
]
[animation made with some ImagePadding to keep object in the center. I stopped the animation at a = 10, the image gets pretty small after that]

Mathematica: Rasters in 3D graphics

There are times when exporting to a pdf image is simply troublesome. If the data you are plotting contains many points then your figure will be big in size and the pdf viewer of your choice will spend most of its time rendering this high quality image. We can thus export this image as a jpeg, png or tiff. The picture will be fine from a certain view but when you zoom in it will look all distorted. This is fine to some extent for the figure we are plotting but if your image contains text then this text will look pixelated.
In order to try to get the best of both worlds we can separate this figure into two parts: Axes with labels and the 3D picture. The axes can thus be exported as pdf or eps and the 3D figure as a raster. I wish I knew how later combine the two in Mathematica, so for the moment we can use a vector graphics editor such as Inkscape or Illustrator to combine the two.
I managed to achieve this for a plot I made in a publication but this prompt me to create routines in Mathematica in order to automatize this process. Here is what I have so far:
SetDirectory[NotebookDirectory[]];
SetOptions[$FrontEnd, PrintingStyleEnvironment -> "Working"];
I like to start my notebook by setting the working directory to the notebook directory. Since I want my images to be of the size I specify I set the printing style environment to working, check this for more info.
in = 72;
G3D = Graphics3D[
AlignmentPoint -> Center,
AspectRatio -> 0.925,
Axes -> {True, True, True},
AxesEdge -> {{-1, -1}, {1, -1}, {-1, -1}},
AxesStyle -> Directive[10, Black],
BaseStyle -> {FontFamily -> "Arial", FontSize -> 12},
Boxed -> False,
BoxRatios -> {3, 3, 1},
LabelStyle -> Directive[Black],
ImagePadding -> All,
ImageSize -> 5 in,
PlotRange -> All,
PlotRangePadding -> None,
TicksStyle -> Directive[10],
ViewPoint -> {2, -2, 2},
ViewVertical -> {0, 0, 1}
]
Here we set the view of the plot we want to make. Now lets create our plot.
g = Show[
Plot3D[Sin[x y], {x, 0, Pi}, {y, 0, Pi},
Mesh -> None,
AxesLabel -> {"x", "y", "z"}
],
Options[G3D]
]
Now we need to find a way of separating. Lets start by drawing the axes.
axes = Graphics3D[{}, AbsoluteOptions[g]]
fig = Show[g,
AxesStyle -> Directive[Opacity[0]],
FaceGrids -> {{-1, 0, 0}, {0, 1, 0}}
]
I included the facegrids so that we can match the figure with the axis in the post editing process. Now we export both images.
Export["Axes.pdf", axes];
Export["Fig.pdf", Rasterize[fig, ImageResolution -> 300]];
You will obtain two pdf files which you can edit in and put together into a pdf or eps. I wish it was that simple but it isn't. If you actually did this you will obtain this:
The two figures are different sizes. I know axes.pdf is correct because when I open it in Inkspace the figure size is 5 inches as I had previously specified.
I mentioned before that I managed to get this with one of my plots. I will clean the file and change the plots to make it more accessible for anyone who wants to see that this is in fact true. In any case, does anyone know why I can't get the two pdf files to be the same size? Also, keep in mind that we want to obtain a pretty plot for the Rasterized figure. Thank you for your time.
PS.
As a bonus, can we avoid the post editing and simply combine the two figures in mathematica? The rasterized version and the vector graphics version that is.
EDIT:
Thanks to rcollyer for his comment. I'm posting the results of his comment.
One thing to mention is that when we export the axes we need to set Background to None so that we can have a transparent picture.
Export["Axes.pdf", axes, Background -> None];
Export["Fig.pdf", Rasterize[fig, ImageResolution -> 300]];
a = Import["Axes.pdf"];
b = Import["Fig.pdf"];
Show[b, a]
And then, exporting the figure gives the desired effect
Export["FinalFig.pdf", Show[b, a]]
The axes preserve the nice components of vector graphics while the figure is now a Rasterized version of the what we plotted. But the main question still remains.
How do you make the two figures match?
UPDATE:
My question has been answered by Alexey Popkov. I would like to thank him for taking the time to look into my problem. The following code is an example for those of you want to use the technique I previously mentioned. Please see Alexey Popkov's answer for useful comments in his code. He managed to make it work in Mathematica 7 and it works even better in Mathematica 8. Here is the result:
SetDirectory[NotebookDirectory[]];
SetOptions[$FrontEnd, PrintingStyleEnvironment -> "Working"];
$HistoryLength = 0;
in = 72;
G3D = Graphics3D[
AlignmentPoint -> Center, AspectRatio -> 0.925, Axes -> {True, True, True},
AxesEdge -> {{-1, -1}, {1, -1}, {-1, -1}}, AxesStyle -> Directive[10, Black],
BaseStyle -> {FontFamily -> "Arial", FontSize -> 12}, Boxed -> False,
BoxRatios -> {3, 3, 1}, LabelStyle -> Directive[Black], ImagePadding -> 40,
ImageSize -> 5 in, PlotRange -> All, PlotRangePadding -> 0,
TicksStyle -> Directive[10], ViewPoint -> {2, -2, 2}, ViewVertical -> {0, 0, 1}
];
axesLabels = Graphics3D[{
Text[Style["x axis (units)", Black, 12], Scaled[{.5, -.1, 0}], {0, 0}, {1, -.9}],
Text[Style["y axis (units)", Black, 12], Scaled[{1.1, .5, 0}], {0, 0}, {1, .9}],
Text[Style["z axis (units)", Black, 12], Scaled[{0, -.15, .7}], {0, 0}, {-.1, 1.5}]
}];
fig = Show[
Plot3D[Sin[x y], {x, 0, Pi}, {y, 0, Pi}, Mesh -> None],
ImagePadding -> {{40, 0}, {15, 0}}, Options[G3D]
];
axes = Show[
Graphics3D[{}, FaceGrids -> {{-1, 0, 0}, {0, 1, 0}},
AbsoluteOptions[fig]], axesLabels,
Epilog -> Text[Style["Panel A", Bold, Black, 12], ImageScaled[{0.075, 0.975}]]
];
fig = Show[fig, AxesStyle -> Directive[Opacity[0]]];
Row[{fig, axes}]
At this point you should see this:
The magnification takes care of the resolution of your image. You should try different values to see how this changes your picture.
fig = Magnify[fig, 5];
fig = Rasterize[fig, Background -> None];
Combine the graphics
axes = First#ImportString[ExportString[axes, "PDF"], "PDF"];
result = Show[axes, Epilog -> Inset[fig, {0, 0}, {0, 0}, ImageDimensions[axes]]];
Export them
Export["Result.pdf", result];
Export["Result.eps", result];
The only difference I found between M7 and M8 using the above code is that M7 does not export the eps file correctly. Other than that everything is working fine now. :)
The first column shows the output obtained from M7. Top is the eps version with file size of 614 kb, bottom is the pdf version with file size of 455 kb. The second column shows the output obtained from M8. Top is the eps version with file size of 643 kb, bottom is the pdf version with file size of 463 kb.
I hope you find this useful. Please check Alexey's answer to see the comments in his code, they will help you avoid pitfalls with Mathematica.
The complete solution for Mathematica 7.0.1: fixing bugs
The code with comments:
(*controls the resolution of rasterized graphics*)
magnification = 5;
SetOptions[$FrontEnd, PrintingStyleEnvironment -> "Working"]
(*Turn off history for saving memory*)
$HistoryLength = 0;
(*Epilog will give us the bounding box of the graphics*)
g1 = Plot3D[Sin[x y], {x, 0, Pi}, {y, 0, Pi},
AlignmentPoint -> Center, AspectRatio -> 0.925,
Axes -> {True, True, True},
AxesEdge -> {{-1, -1}, {1, -1}, {-1, -1}},
BaseStyle -> {FontFamily -> "Arial", FontSize -> 12},
Boxed -> False, BoxRatios -> {3, 3, 1},
LabelStyle -> Directive[Black], ImagePadding -> All,
ImageSize -> 5*72, PlotRange -> All, PlotRangePadding -> None,
TicksStyle -> Directive[10], ViewPoint -> {2, -2, 2},
ViewVertical -> {0, 0, 1}, AxesStyle -> Directive[Opacity[0]],
FaceGrids -> {{-1, 0, 0}, {0, 1, 0}}, Mesh -> None,
ImagePadding -> 40,
Epilog -> {Red, AbsoluteThickness[1],
Line[{ImageScaled[{0, 0}], ImageScaled[{0, 1}],
ImageScaled[{1, 1}], ImageScaled[{1, 0}],
ImageScaled[{0, 0}]}]}];
(*The options list should NOT contain ImagePadding->Full.Even it is \
before ImagePadding->40 it is not replaced by the latter-another bug!*)
axes = Graphics3D[{Opacity[0],
Point[PlotRange /. AbsoluteOptions[g1] // Transpose]},
AlignmentPoint -> Center, AspectRatio -> 0.925,
Axes -> {True, True, True},
AxesEdge -> {{-1, -1}, {1, -1}, {-1, -1}},
AxesStyle -> Directive[10, Black],
BaseStyle -> {FontFamily -> "Arial", FontSize -> 12},
Boxed -> False, BoxRatios -> {3, 3, 1},
LabelStyle -> Directive[Black], ImageSize -> 5*72,
PlotRange -> All, PlotRangePadding -> None,
TicksStyle -> Directive[10], ViewPoint -> {2, -2, 2},
ViewVertical -> {0, 0, 1}, ImagePadding -> 40,
Epilog -> {Red, AbsoluteThickness[1],
Line[{ImageScaled[{0, 0}], ImageScaled[{0, 1}],
ImageScaled[{1, 1}], ImageScaled[{1, 0}],
ImageScaled[{0, 0}]}]}];
(*fixing bug with ImagePadding loosed when specifyed as option in \
Plot3D*)
g1 = AppendTo[g1, ImagePadding -> 40];
(*Increasing ImageSize without damage.Explicit setting for \
ImagePadding is important (due to a bug in behavior of \
ImagePadding->Full)!*)
g1 = Magnify[g1, magnification];
g2 = Rasterize[g1, Background -> None];
(*Fixing bug with non-working option Background->None when graphics \
is Magnifyed*)
g2 = g2 /. {255, 255, 255, 255} -> {0, 0, 0, 0};
(*Fixing bug with icorrect exporting of Ticks in PDF when Graphics3D \
and 2D Raster are combined*)
axes = First#ImportString[ExportString[axes, "PDF"], "PDF"];
(*Getting explicid ImageSize of graphics imported form PDF*)
imageSize =
Last#Transpose[{First##, Last##} & /#
Sort /# Transpose#
First#Cases[axes,
Style[{Line[x_]}, ___, RGBColor[1.`, 0.`, 0.`, 1.`], ___] :>
x, Infinity]]
(*combining Graphics3D and Graphics*)
result = Show[axes, Epilog -> Inset[g2, {0, 0}, {0, 0}, imageSize]]
Export["C:\\result.pdf", result]
Here is what I see in the Notebook:
And here is what I get in the PDF:
Just checking (Mma8):
SetOptions[$FrontEnd, PrintingStyleEnvironment -> "Working"];
in = 72;
G3D = Graphics3D[AlignmentPoint -> Center, AspectRatio -> 0.925,
Axes -> {True, True, True},
AxesEdge -> {{-1, -1}, {1, -1}, {-1, -1}},
AxesStyle -> Directive[10, Black],
BaseStyle -> {FontFamily -> "Arial", FontSize -> 12},
Boxed -> False, BoxRatios -> {3, 3, 1},
LabelStyle -> Directive[Black], ImagePadding -> All,
ImageSize -> 5 in, PlotRange -> All, PlotRangePadding -> None,
TicksStyle -> Directive[10], ViewPoint -> {2, -2, 2},
ViewVertical -> {0, 0, 1}];
g = Show[Plot3D[Sin[x y], {x, 0, Pi}, {y, 0, Pi}, Mesh -> None,
AxesLabel -> {"x", "y", "z"}], Options[G3D]];
axes = Graphics3D[{}, AbsoluteOptions[g]];
fig = Show[g, AxesStyle -> Directive[Opacity[0]],
FaceGrids -> {{-1, 0, 0}, {0, 1, 0}}];
Export["c:\\Axes.pdf", axes, Background -> None];
Export["c:\\Fig.pdf", Rasterize[fig, ImageResolution -> 300]];
a = Import["c:\\Axes.pdf"];
b = Import["c:\\Fig.pdf"];
Export["c:\\FinalFig.pdf", Show[b, a]]
In Mathematica 8 the problem may be solved even simpler using new Overlay function.
Here is the code from the UPDATE section of the question:
SetOptions[$FrontEnd, PrintingStyleEnvironment -> "Working"];
$HistoryLength = 0;
in = 72;
G3D = Graphics3D[AlignmentPoint -> Center, AspectRatio -> 0.925,
Axes -> {True, True, True},
AxesEdge -> {{-1, -1}, {1, -1}, {-1, -1}},
AxesStyle -> Directive[10, Black],
BaseStyle -> {FontFamily -> "Arial", FontSize -> 12},
Boxed -> False, BoxRatios -> {3, 3, 1},
LabelStyle -> Directive[Black], ImagePadding -> 40,
ImageSize -> 5 in, PlotRange -> All, PlotRangePadding -> 0,
TicksStyle -> Directive[10], ViewPoint -> {2, -2, 2},
ViewVertical -> {0, 0, 1}];
axesLabels =
Graphics3D[{Text[Style["x axis (units)", Black, 12],
Scaled[{.5, -.1, 0}], {0, 0}, {1, -.9}],
Text[Style["y axis (units)", Black, 12],
Scaled[{1.1, .5, 0}], {0, 0}, {1, .9}],
Text[Style["z axis (units)", Black, 12],
Scaled[{0, -.15, .7}], {0, 0}, {-.1, 1.5}]}];
fig = Show[Plot3D[Sin[x y], {x, 0, Pi}, {y, 0, Pi}, Mesh -> None],
ImagePadding -> {{40, 0}, {15, 0}}, Options[G3D]];
axes = Show[
Graphics3D[{}, FaceGrids -> {{-1, 0, 0}, {0, 1, 0}},
AbsoluteOptions[fig]], axesLabels,
Epilog ->
Text[Style["Panel A", Bold, Black, 12],
ImageScaled[{0.075, 0.975}]]];
fig = Show[fig, AxesStyle -> Directive[Opacity[0]]];
And here is the solution:
gr = Overlay[{axes,
Rasterize[fig, Background -> None, ImageResolution -> 300]}]
Export["Result.pdf", gr]
In this case we need not to convert fonts to outlines.
UPDATE
As jmlopez pointed out in the comments to this answer, the option Background -> None does not work properly under Mac OS X in Mathematica 8.0.1. One workaround is to replace white non-transparent points by transparent:
gr = Overlay[{axes,
Rasterize[fig, Background -> None,
ImageResolution -> 300] /. {255, 255, 255, 255} -> {0, 0, 0, 0}}]
Export["Result.pdf", gr]
Here I present another version of the original solution which uses the second argument of Raster instead of Inset. I think that this way is a little more straightforward.
Here is the code from the UPDATE section of the question (modified a bit):
SetOptions[$FrontEnd, PrintingStyleEnvironment -> "Working"];
$HistoryLength = 0;
in = 72;
G3D = Graphics3D[AlignmentPoint -> Center, AspectRatio -> 0.925,
Axes -> {True, True, True},
AxesEdge -> {{-1, -1}, {1, -1}, {-1, -1}},
AxesStyle -> Directive[10, Black],
BaseStyle -> {FontFamily -> "Arial", FontSize -> 12},
Boxed -> False, BoxRatios -> {3, 3, 1},
LabelStyle -> Directive[Black], ImagePadding -> 40,
ImageSize -> 5 in, PlotRange -> All, PlotRangePadding -> 0,
TicksStyle -> Directive[10], ViewPoint -> {2, -2, 2},
ViewVertical -> {0, 0, 1}];
axesLabels =
Graphics3D[{Text[Style["x axis (units)", Black, 12],
Scaled[{.5, -.1, 0}], {0, 0}, {1, -.9}],
Text[Style["y axis (units)", Black, 12],
Scaled[{1.1, .5, 0}], {0, 0}, {1, .9}],
Text[Style["z axis (units)", Black, 12],
Scaled[{0, -.15, .7}], {0, 0}, {-.1, 1.5}]}];
fig = Show[Plot3D[Sin[x y], {x, 0, Pi}, {y, 0, Pi}, Mesh -> None],
ImagePadding -> {{40, 0}, {15, 0}}, Options[G3D]];
axes = Show[
Graphics3D[{}, FaceGrids -> {{-1, 0, 0}, {0, 1, 0}},
AbsoluteOptions[fig]], axesLabels,
Prolog ->
Text[Style["Panel A", Bold, Black, 12],
ImageScaled[{0.075, 0.975}]]];
fig = Show[fig, AxesStyle -> Directive[Opacity[0]]];
fig = Magnify[fig, 5];
fig = Rasterize[fig, Background -> None];
axes2D = First#ImportString[ExportString[axes, "PDF"], "PDF"];
The rest of the answer is the new solution.
At first, we set the second argument of Raster so that it will fill the complete PlotRange of axes2D. The general way to do this is:
fig = fig /.
Raster[data_, rectangle_, opts___] :>
Raster[data, {Scaled[{0, 0}], Scaled[{1, 1}]}, opts];
Another way is to make direct assignment to the corresponding Part of the original expression:
fig[[1, 2]] = {Scaled[{0, 0}], Scaled[{1, 1}]}
Note that this last code is based on the knowledge of internal structure of the expression generated by Rasterize which is potentially version-dependent.
Now we combine two graphical objects in a very straightforward way:
result = Show[axes2D, fig]
And export the result:
Export["C:/Result.pdf", result];
Export["C:/Result.eps", result];
Both .eps and .pdf are exported perfectly with Mathematica 8.0.4 under Windows XP 32 bit and look identical to the files exported with the original code:
result = Show[axes2D,
Epilog -> Inset[fig, Center, Center, ImageScaled[{1, 1}]]]
Export["C:/Result.pdf", result];
Export["C:/Result.eps", result];
Note that we need not necessarily to convert axes to outlines at least when exporting to PDF. The code
result = Show[axes,
Epilog -> Inset[fig, Center, Center, ImageScaled[{1, 1}]]]
Export["C:/Result.pdf", result];
and the code
fig[[1, 2]] = {ImageScaled[{0, 0}], ImageScaled[{1, 1}]};
result = Show[axes, Epilog -> First#fig]
Export["C:/Result.pdf", result];
produce PDF files looking identical to both previous versions.
This looks like much ado about nothing. As I read it, the problem you want to solve is the following:
You want to export in a vector format, so that when printed the optimal resolution is used for fonts, lines and graphics
In your edit program you don't want be bothered by the slowness of rendering a complex vector drawing
These requirements can be met by exporting as .eps and using an embedded rasterized preview image.
Export["file.eps","PreviewFormat"->"TIFF"]
This will work in many applications. Unfortunately, MS Word's eps filter has been changing wildly over the last four versions or so, and whereas it once worked for me in one of the older functions it doesn't anymore in W2010. I've heard rumors that it might work in the mac version, but I can't check right now.
Mathematica 9.0.1.0 / 64-bit Linux:
In general, it seems to be very tricky to place the vectorized axes at the correct position. In most applications it will be sufficient to simply rasterize everything with a high resolution:
fig = Plot3D[Sin[x y], {x, 0, 3}, {y, 0, 3}, Mesh -> None];
Export["export.eps", fig, "AllowRasterization" -> True,
ImageResolution -> 600];
The code exports the graphic to an EPS-file using a high quality rasterization of both the 3D content and the axis. Finally, you can convert the EPS-file to a PDF using for example the Linux command epspdf:
epspdf export.eps
This is probably sufficient for most of the users and it saves you a lot of time. However, if you really want to export the text as vector graphic, you might want to try the following function:
ExportAsSemiRaster[filename_, dpi_, fig_, plotrange_,
plotrangepadding_] := (
range =
Show[fig, PlotRange -> plotrange,
PlotRangePadding -> plotrangepadding];
axes = Show[Graphics3D[{}, AbsoluteOptions[range]]];
noaxes = Show[range, AxesStyle -> Transparent];
raster =
Rasterize[noaxes, Background -> None, ImageResolution -> dpi];
result =
Show[raster,
Epilog -> Inset[axes, Center, Center, ImageDimensions[raster]]];
Export[filename, result];
);
You need to explicitly specify the PlotRange and the PlotRangePadding. Example:
fig = Graphics3D[{Opacity[0.9], Orange,
Polygon[{{0, 0, 0}, {4, 0, 4}, {4, 5, 7}, {0, 5, 5}}],
Opacity[0.05], Gray, CuboidBox[{0, 0, 0}, {4, 5, 7}]},
Axes -> True, AxesStyle -> Darker[Orange],
AxesLabel -> {"x1", "x2", "x3"}, Boxed -> False,
ViewPoint -> {-8.5, -8, 6}];
ExportAsSemiRaster["export.pdf", 600,
fig, {{0, 4}, {0, 5}, {0, 7}}, {.0, .0, .0}];
Print[Import["export.pdf"]];

Spherical co-ordinate graphics in Mathematica

Is it possible to create graphics of spherical co-ordinate system like this in mathematica or should I use photoshop? I'm asking because I want a high resolution graphic, but lot of the files on internet are grainy when zoomed.
Here is the image:
The figure is made up of simple geometric shapes and these can be easily recreated in Mathematica using equations. Here is one that is close to this plot, which IMO is less cluttered than the above, but you can always use these ideas to recreate your image exactly.
Clear[ellipsePhi, ellipseTheta, circle]
circle[x_] = {Cos[x], Sin[x]};
ellipsePhi[x_, a_: - Pi/2] = {Cos[x - a]/3, Sin[x + a]};
ellipseTheta[x_, a_: 0] = {Cos[x + a], Sin[-x - a]/2};
(*Main circle*)
ParametricPlot[circle[x], {x, 0, 2 Pi},
PlotStyle -> Black,
Epilog -> First /# {
(*Ellipses*)
ParametricPlot[{ellipsePhi[x], ellipsePhi[-x], ellipseTheta[-x],
ellipseTheta[x]}, {x, 0, Pi},
PlotStyle -> {{Black, Dashed}, Black}],
(*Co-ordinate axes*)
Graphics[
Table[GeometricTransformation[{Arrowheads[0.03],
Arrow[{{0, 0}, {1.2, 0}}]},
ReflectionMatrix[circle[x]]], {x, {Pi/2, -Pi/4, Pi/8}}]],
(*mark point, rho, phi & theta directions*)
ParametricPlot[{ellipsePhi[x, Pi/2], ellipseTheta[-x, 13 Pi/20]}, {x,
0, Pi/4},
PlotStyle -> {{Red, Thick}, {Blue, Thick}}] /.
Line[x__] :> Sequence[Arrowheads[0.03], Arrow[x]],
Graphics[{{Directive[Darker#Green, Thick], Arrowheads[0.03],
Arrow[{{0, 0}, ellipsePhi[-3 Pi/4]}]},
{Directive[Purple], Disk[ellipsePhi[-3 Pi/4], 0.02]}}],
(*text*)
Graphics[{
Text[Style["x", Italic, Larger], 1.25 circle[5 Pi/4]],
Text[Style["y", Italic, Larger], 1.25 circle[0]],
Text[Style["z", Italic, Larger], 1.25 circle[Pi/2]],
Text[Style["\[Rho]", Italic, Larger], 0.4 circle[4 Pi/11]],
Text[Style["\[CurlyPhi]", Italic, Larger],
1.1 ellipsePhi[Pi + Pi/5]],
Text[Style["\[Theta]", Italic, Larger],
1.1 ellipseTheta[13 Pi/20 - Pi/8]],
Text[Style["P", Italic, Larger], 1.2 ellipsePhi[-3 Pi/4 + Pi/24]]}]
},
Axes -> False, PlotRange -> 1.3 {{-1, 1}, {-1, 1}}
]
which gives you this
Although it is possible to set the angles & arrows precisely, in some places (e.g., 13 Pi/20), I've only roughly approximated it. You really can't tell the difference in the final figure, but if you're picky you can change them and fix the positions exactly.
This alternative solution has the advantage of being created using 3D directives. As such, it was easy to wrap inside a Manipulate and you can drag it with your mouse to change the viewpoint:
Manipulate[
Module[{x = Sin[\[Phi]] Cos[\[Theta]], y = Sin[\[Phi]] Sin[\[Theta]],
z = Cos[\[Phi]]},
Show[
ParametricPlot3D[
{{Cos[t], Sin[t], 0},
{0, Sin[t], Cos[t]},
{Sin[t], 0, Cos[t]}},
{t, 0, 2 \[Pi]}, PlotStyle -> Black, Boxed -> False,
Axes -> False, AxesLabel -> {"x", "y", "z"}],
ParametricPlot3D[0.5*{Cos[t], Sin[t], 0}, {t, 0, \[Theta]}],
ParametricPlot3D[
RotationTransform[\[Theta], {0, 0, 1}][{Sin[t]/2, 0,
Cos[t]/2}], {t, 0, \[Phi]}],
Graphics3D[{
{{Blue, Thick,
Arrow[{{0, 0, 0}, #}] & /# {{1, 0, 0}, {0, 1, 0}, {0, 0,
1}, {x, y, z}}},
{Opacity[0.1],
Red, Polygon[{{0, 0, 0}, {x, y, 0}, {x, y, z}}],
Green, Polygon[{{0, 0, 0}, {x, 0, 0}, {x, y, 0}}]}},
{Opacity[0.05], Sphere[{0, 0, 0}]},
{Text["O", {-.03, -.03, -.03}],
Text["X", {1.1, 0, 0}],
Text["Q", {x, y, 0}, {1, 1}],
Text["P", {x, y, z}, {0, -1}],
Text["Y", {0, 1.1, 0}],
Text["Z", {0, 0, 1.1}],
Text["r", {x/2, y/2, 0}, {1, 1}],
Text[
"\[Theta]", {Cos[\[Theta]/2]/2, Sin[\[Theta]/2]/2, 0}, {1,
1}],
Text["\[Phi]",
RotationTransform[\[Theta], {0, 0, 1}][{Sin[\[Phi]/2]/2, 0,
Cos[\[Phi]/2]/2}], {1, 1}]}}]]],
{{\[Phi], \[Pi]/4}, 0.01, \[Pi]/2}, {{\[Theta], \[Pi]/4}, 0.01,
2 \[Pi]}]

Resources