Mathematica:3D图形中的栅格

jml*_*pez 17 wolfram-mathematica

有时候导出到pdf图像简直很麻烦.如果您绘制的数据包含许多点,那么您的数字将会很大,您选择的pdf查看器将花费大部分时间来渲染这个高质量的图像.因此,我们可以将此图像导出为jpeg,png或tiff.从某个视图来看图片会很好但是当你放大时它看起来会变形.对于我们正在绘制的图形,这在某种程度上是很好的,但如果您的图像包含文本,那么此文本将看起来像素化.

为了尝试两全其美,我们可以将这个图分成两部分:带标签的轴和3D图片.因此,轴可以导出为pdf或eps,3D图形可以导出为栅格.我希望我知道后来如何将这两者结合在Mathematica中,所以目前我们可以使用矢量图形编辑器(如Inkscape或Illustrator)来组合这两者.

我设法实现了这个我在出版物中制作的情节,但这促使我在Mathematica中创建例程以自动化这个过程.这是我到目前为止:

SetDirectory[NotebookDirectory[]];
SetOptions[$FrontEnd, PrintingStyleEnvironment -> "Working"];
Run Code Online (Sandbox Code Playgroud)

我喜欢通过将工作目录设置为notebook目录来启动笔记本.由于我希望我的图像具有我指定的大小,因此我将打印样式环境设置为工作,请查看信息以获取更多信息.

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}
 ]
Run Code Online (Sandbox Code Playgroud)

在这里,我们设置了我们想要制作的情节的视图.现在让我们创建我们的情节

g = Show[
  Plot3D[Sin[x y], {x, 0, Pi}, {y, 0, Pi}, 
   Mesh -> None,
   AxesLabel -> {"x", "y", "z"}
   ], 
  Options[G3D]
 ]
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

现在我们需要找到一种分离方式.让我们从绘制轴开始.

axes = Graphics3D[{}, AbsoluteOptions[g]]
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

fig = Show[g, 
  AxesStyle -> Directive[Opacity[0]],
  FaceGrids -> {{-1, 0, 0}, {0, 1, 0}}
 ]
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

我包含了facegrids,以便我们可以在后期编辑过程中将图形与轴匹配.现在我们导出两个图像.

Export["Axes.pdf", axes];
Export["Fig.pdf", Rasterize[fig, ImageResolution -> 300]];
Run Code Online (Sandbox Code Playgroud)

您将获得两个pdf文件,您可以编辑这些文件并将它们组合成pdf或eps.我希望它很简单,但事实并非如此.如果你真的这样做了,你会得到这个:

在此输入图像描述

这两个数字是不同的大小.我知道axes.pdf是正确的,因为当我在Inkspace中打开它时,数字大小是我之前指定的5英寸.

我之前提到过,我设法通过我的一个情节得到了这个.我将清理文件并更改图表,以使任何想要看到这实际上是真实的人更容易访问.无论如何,有谁知道为什么我不能让这两个pdf文件大小相同?另外,请记住,我们想要为Rasterized图获得漂亮的图.感谢您的时间.

PS.作为奖励,我们可以避免后期编辑并简单地将这两个数字合并到mathematica中吗?光栅化版本和矢量图形版本.


编辑:

感谢rcollyer的评论.我发布了他评论的结果.

有一点需要注意的是,当我们输出轴时,我们需要设置Background,None以便我们可以有一个透明的图片.

Export["Axes.pdf", axes, Background -> None];
Export["Fig.pdf", Rasterize[fig, ImageResolution -> 300]];
a = Import["Axes.pdf"];
b = Import["Fig.pdf"];
Show[b, a]
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

然后,导出图形会产生预期的效果

Export["FinalFig.pdf", Show[b, a]]
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

轴保留了矢量图形的漂亮组件,而图形现在是我们绘制的光栅化版本.但主要问题仍然存在.你如何使这两个数字匹配?

更新:

Alexey Popkov回答了我的问题.我想感谢他花时间研究我的问题.以下代码是您想要使用我之前提到的技术的示例.请参阅Alexey Popkov在其代码中提供有用评论的答案.他设法使它在Mathematica 7中工作,它在Mathematica 8中的效果更好.结果如下:

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}]
Run Code Online (Sandbox Code Playgroud)

此时你应该看到这个:

在此输入图像描述

放大倍率可以处理图像的分辨率.你应该尝试不同的值来看看它如何改变你的画面.

fig = Magnify[fig, 5];
fig = Rasterize[fig, Background -> None];
Run Code Online (Sandbox Code Playgroud)

结合图形

axes = First@ImportString[ExportString[axes, "PDF"], "PDF"];
result = Show[axes, Epilog -> Inset[fig, {0, 0}, {0, 0}, ImageDimensions[axes]]];
Run Code Online (Sandbox Code Playgroud)

导出它们

Export["Result.pdf", result];
Export["Result.eps", result];
Run Code Online (Sandbox Code Playgroud)

我使用上面的代码在M7和M8之间找到的唯一区别是M7不能正确导出eps文件.除此之外现在一切正常.:)

在此输入图像描述

第一列显示从M7获得的输出.Top是eps版本,文件大小为614 kb,底部是pdf版本,文件大小为455 kb.第二列显示从M8获得的输出.Top是eps版本,文件大小为643 kb,底部是pdf版本,文件大小为463 kb.

希望这个对你有帮助.请检查Alexey的答案,看看他的代码中的注释,它们将帮助您避免Mathematica的陷阱.

Ale*_*kov 7

Mathematica 7.0.1的完整解决方案:修复错误

带注释的代码:

(*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]
Run Code Online (Sandbox Code Playgroud)

这是我在笔记本中看到的内容:

截图

这是我在PDF中得到的:

截图