# Wolfram Function Repository

Instant-use add-on functions for the Wolfram Language

Function Repository Resource:

Extract the underlying graphical primitives from plots and graphics

Contributed by:
Jaroslav Kysela

ResourceFunction["ExtractGraphicsPrimitives"][ extracts the geometric primitives from the graphics object | |

ResourceFunction["ExtractGraphicsPrimitives"][ extracts the primitives together with | |

ResourceFunction["ExtractGraphicsPrimitives"][ removes all styling directives and options. |

Graphical objects characterized by the heads Graphics, Graphics3D, and GeoGraphics are internally stored as expressions with a hierarchical structure that contains information about the graphics primitives as well as their styling. Usually the primitives form the last level of the hierarchy and styling options and directives reside in higher levels. ExtractGraphicsPrimitives takes the entire structure and returns only those parts of it that correspond to graphics primitives. Since some styling information might be lost this way, the second argument *lev* allows one to include also levels above the primitives.

Sometimes a plotting function returns a GraphicsComplex expression, which represents a standalone unit that contains both the primitives and their styling. For the purposes of ResourceFunction["ExtractGraphicsPrimitives"], this unit is understood as a single primitive, even though it might contain actual primitives such as Polygon or Line. Analogously, an Input expression is also considered a single primitive.

The default value of the second argument *lev* is 0. This argument can be any non-negative integer.

The default value of the third argument is True.

Form a Graphics object:

In[1]:= |

Out[1]= |

Extract the underlying primitives:

In[2]:= |

Out[2]= |

Apart from the primitives, also include their accompanying directives one level up:

In[3]:= |

Out[3]= |

Keep the primitives but remove the directives:

In[4]:= |

Out[4]= |

Extract primitives from a plot:

In[5]:= |

Out[5]= |

In[6]:= |

Out[6]= |

Display the primitives:

In[7]:= |

Out[3]= |

Apply a different styling to the extracted primitive:

In[8]:= |

Out[8]= |

Extract primitives from Graphics objects generated by 2D plotting functions:

In[9]:= |

Out[9]= |

In[10]:= |

Out[3]= |

Show the primitive:

In[11]:= |

Out[11]= |

Extract primitives from Graphics3D objects generated by 3D plotting functions:

In[12]:= |

Out[13]= |

In[14]:= |

Out[3]= |

Show the primitive:

In[15]:= |

Out[16]= |

Extract primitives from GeoGraphics objects generated by geo plotting functions:

In[17]:= |

Out[18]= |

In[19]:= |

Out[3]= |

Show the primitives:

In[20]:= |

Out[20]= |

The input object can be more general:

In[21]:= |

Out[21]= |

In[22]:= |

Out[22]= |

By default, only the primitives themselves are extracted, which might lead to the loss of styling:

In[23]:= |

Out[23]= |

In[24]:= |

Out[3]= |

Include also the styling by increasing the level parameter:

In[25]:= |

Out[26]= |

Usually only a certain range of level values gives useful results:

In[27]:= |

Out[27]= |

In[28]:= |

Out[28]= |

By default, the styling is preserved (provided that the level specification is correct):

In[29]:= |

Out[29]= |

In[30]:= |

Out[30]= |

Remove the styling:

In[31]:= |

Out[31]= |

The internal structure of graphics objects can be examined using InputForm:

In[32]:= |

Out[32]= |

In[33]:= |

Out[33]= |

Relevant primitives can be retrieved from this expression using Cases or Position:

In[34]:= |

Out[25]= |

In[35]:= |

Out[36]= |

Removing the styling of a graphical object via the third argument in ExtractGraphicsPrimitives gives similar results to menu option Format ▶ Clear Formatting:

In[37]:= |

Out[38]= |

In[39]:= |

Out[35]= |

Compare with the approach when the output of Plot3D above is selected and then shortcut + + (for Windows) is pressed:

Charts are by default interactive, which may cause problems when extracting the primitives:

In[40]:= |

Out[40]= |

In[41]:= |

Out[41]= |

Effectively turn off interactivity by setting PerformanceGoal to "Speed":

In[42]:= |

Out[43]= |

Some charts and plots store styling information in the higher levels of the hierarchy:

In[44]:= |

Out[44]= |

In[45]:= |

Out[45]= |

Increment the level parameter to include also the styling information:

In[46]:= |

Out[46]= |

Some plotting functions adjust the aspect ratio to accommodate the plot. Extracting the primitives resets this ratio:

In[47]:= |

Out[47]= |

In[48]:= |

Out[46]= |

The line appears flat due to disproportionate values in the horizontal and vertical axes. Set the aspect ratio appropriately:

In[49]:= |

Out[49]= |

Similar problems arise when plotting against dates:

In[50]:= |

Out[50]= |

In[51]:= |

Out[51]= |

The output of geographics functions contains more primitives than are directly visible:

In[52]:= |

Out[49]= |

In[53]:= |

Out[54]= |

Plotting all these primitives may lead to unexpected results:

In[55]:= |

Out[55]= |

Choose only the relevant primitives:

In[56]:= |

Out[56]= |

Additional primitives might be stored inside the value for the option Method. Remove it to obtain better results:

In[57]:= |

Out[46]= |

The output of some plotting functions contains more graphics wrapped in Inset, which may lead to unexpected results:

In[58]:= |

Out[58]= |

In[59]:= |

Out[59]= |

In[60]:= |

Out[60]= |

Adjust manually the position of Inset objects or fix the PlotRange:

In[61]:= |

Out[61]= |

Create a still life with a glass containing green liquid:

In[62]:= |

In[63]:= |

In[64]:= |

Out[64]= |

In[65]:= |

Out[62]= |

- 1.0.0 – 01 March 2023

This work is licensed under a Creative Commons Attribution 4.0 International License