Mathematica:获取图形基元和指令 [英] Mathematica: Obtaining graphics primitives and directives

查看:66
本文介绍了Mathematica:获取图形基元和指令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从Graphics对象获取图形基元和指令? Leonid Shifrin在帖子 Mathematica:删除图形基元中展示了如何删除它们.我尝试应用类似的东西,但是我无法获得想要的东西.考虑以下示例:

How do you obtain graphic primitives and directives from a Graphics object? Leonid Shifrin showed how to remove them in the post Mathematica: Removing graphics primitives. I tried applying something similar but I can't get what I want. Consider this example:

 g1 = ListPlot3D[
    {{0, -1, 0}, {0, 1, 0}, {-1, 0, 1}, {1, 0, 1}, {-1, 1, 1}},
    Mesh -> {2, 2},
    Boxed -> False,
    Axes -> False,
    ViewPoint -> {2, -2, 1},
    ViewVertical -> {0, 0, 1},
    MeshStyle -> RGBColor[0, 0.5, 0],
    BoundaryStyle -> RGBColor[1, 0.5, 0]
 ];
 g2 = ImportString[ExportString[g1, "PDF", Background -> None], "PDF"][[1]]

g2现在是一个图形对象.如果查看g2InputForm,您将看到此图形对象由PolygonJoinedCurve组成.我想做的是能够遍历g2的所有原始对象.如果我们尝试如下迭代

g2 is now a graphics object. If you look at the InputForm of g2 you will see that this graphics object is composed of Polygons and JoinedCurves. What I would like to do is able to iterate through all of the primitive objects of g2. If we try to iterate as follows

 objs = First[g2];
 Table[Head[objs[[i]]], {i, 1, Length@objs}]

我们获得

 {Thickness, Polygon, Polygon, Polygon, Polygon, Style, Style, Style, Style, 
  Style, Style, Style, Style, Style, Style, Style, Style, Style, Style, Style, 
  Style, Style, Style, Style, Style, Style, Style, Style, Style, Style, Style, 
  Style, Style, Style, Style, Style, Style, Style, Style, Style, Style, Style, 
  Style, Style, Style}

我想获取的是简单基元的列表,我不希望它们包含在Styles中.这是仅获取线条和颜色的一种尝试:

What I would like to obtain instead is a list of simple primitives, I do not want them inside Styles. Here is one attempt obtaining only the lines and colors:

 tmp1 = Cases[objs, (_JoinedCurve | _RGBColor), Infinity];
 tmp2 = DeleteCases[objs, (_Polygon | _Thickness), Infinity];
 GraphicsRow[{Graphics[tmp1], Graphics[tmp2]}]

请注意,左侧图像绘制不正确.该图像仅使用JoinedCurveRGBColor生成.它以某种方式设法错过了一种颜色,这就是为什么我们有一条黑色的线,而其余的行却具有另一种颜色.另一幅图像绘制正确,我们所做的只是删除其中出现的所有PolygonsThickness.我在这里做了什么不同的事情?我们不应该获得相同的地块吗?

Notice that the image on the left is drawn incorrectly. This image was generated using only JoinedCurves and RGBColors. It somehow managed to miss one color, that is why we have a black line and then the rest of lines have the other color. The other image is drawn correctly, all we did was delete all the Polygons and Thickness that appeared in there. What am I doing differently here? Shouldn't we obtain the same plots?

推荐答案

我读到:

我想获得的是 一组简单的原语,我不 希望将它们放入Styles.

What I would like to obtain instead is a list of simple primitives, I do not want them inside Styles.

您可以通过简单的替换就可以得到它:

You can get it just by simple replacement:

First[ g2 /. Style[expr_, opts___] :> {opts, expr} ]


现在您写:


Now you write:

这是仅获取以下内容的一种尝试 线条和颜色

Here is one attempt obtaining only the lines and colors

了解g2的内部结构,仅提取具有颜色的Line对象很简单.这甚至更简单,因为所有Line都用Style包裹:

Knowing the internal structure of g2 it is simple to extract only Line objects with its colors. It is even simpler because all Lines are wrapped with Style:

tmp3 = Cases[g2, 
   Style[{lines__Line}, ___, color_RGBColor, ___] :> {color, lines}, 
   Infinity];
Graphics[tmp3]

这篇关于Mathematica:获取图形基元和指令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆