Mathematica:获取图形基元和指令 [英] Mathematica: Obtaining graphics primitives and directives
问题描述
如何从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
现在是一个图形对象.如果查看g2
的InputForm
,您将看到此图形对象由Polygon
和JoinedCurve
组成.我想做的是能够遍历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 Polygon
s and JoinedCurve
s. 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]}]
请注意,左侧图像绘制不正确.该图像仅使用JoinedCurve
和RGBColor
生成.它以某种方式设法错过了一种颜色,这就是为什么我们有一条黑色的线,而其余的行却具有另一种颜色.另一幅图像绘制正确,我们所做的只是删除其中出现的所有Polygons
和Thickness
.我在这里做了什么不同的事情?我们不应该获得相同的地块吗?
Notice that the image on the left is drawn incorrectly. This image was generated using only JoinedCurve
s and RGBColor
s. 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 Line
s are wrapped with Style
:
tmp3 = Cases[g2,
Style[{lines__Line}, ___, color_RGBColor, ___] :> {color, lines},
Infinity];
Graphics[tmp3]
这篇关于Mathematica:获取图形基元和指令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!