Introduction to Artists#
Almost all objects you interact with on a Matplotlib plot are called "Artist"(and are subclasses of theArtist class).FigureandAxes are Artists, and generally containAxis Artists and Artists that contain data or annotation information.
Creating Artists#
Usually we do not instantiate Artists directly, but rather use a plottingmethod onAxes. Some examples of plotting methods and the Artistobject they create is given below:
Axes helper method | Artist |
|---|---|
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
As an example, we can save the Line2D Artist returned fromaxes.Axes.plot:
In [209]:importmatplotlib.pyplotaspltIn [210]:importmatplotlib.artistasmartistIn [211]:importnumpyasnpIn [212]:fig,ax=plt.subplots()In [213]:x,y=np.random.rand(2,100)In [214]:lines=ax.plot(x,y,'-',label='example')In [215]:print(lines)[<matplotlib.lines.Line2D at 0xd378b0c>]
Note thatplot returns a _list_ of lines because you can pass in multiple x,y pairs to plot. The line has been added to the Axes, and we can retrieve theArtist viaget_lines():
In [216]:print(ax.get_lines())<a list of 1 Line2D objects>In [217]:print(ax.get_lines()[0])Line2D(example)
Changing Artist properties#
Getting thelines object gives us access to all the properties of theLine2D object. So if we want to change thelinewidth after the fact, we can do so usingArtist.set.
fig,ax=plt.subplots(figsize=(4,2.5))x=np.arange(0,13,0.2)y=np.sin(x)lines=ax.plot(x,y,'-',label='example',linewidth=0.2,color='blue')lines[0].set(color='green',linewidth=2)

We can interrogate the full list of settable properties withmatplotlib.artist.getp:
In [218]:martist.getp(lines[0])agg_filter = Nonealpha = Noneanimated = Falseantialiased or aa = Truebbox = Bbox(x0=0.004013842290585101, y0=0.013914221641967...children = []clip_box = TransformedBbox( Bbox(x0=0.0, y0=0.0, x1=1.0, ...clip_on = Trueclip_path = Nonecolor or c = bluedash_capstyle = buttdash_joinstyle = rounddata = (array([0.91377845, 0.58456834, 0.36492019, 0.0379...drawstyle or ds = defaultfigure = Figure(550x450)fillstyle = fullgapcolor = Nonegid = Nonein_layout = Truelabel = examplelinestyle or ls = -linewidth or lw = 2.0marker = Nonemarkeredgecolor or mec = bluemarkeredgewidth or mew = 1.0markerfacecolor or mfc = bluemarkerfacecoloralt or mfcalt = nonemarkersize or ms = 6.0markevery = Nonemouseover = Falsepath = Path(array([[0.91377845, 0.51224793], [0.58...path_effects = []picker = Nonepickradius = 5rasterized = Falsesketch_params = Nonesnap = Nonesolid_capstyle = projectingsolid_joinstyle = roundtightbbox = Bbox(x0=70.4609002763619, y0=54.321277798941786, x...transform = CompositeGenericTransform( TransformWrapper( ...transformed_clip_path_and_affine = (None, None)url = Nonevisible = Truewindow_extent = Bbox(x0=70.4609002763619, y0=54.321277798941786, x...xdata = [0.91377845 0.58456834 0.36492019 0.03796664 0.884...xydata = [[0.91377845 0.51224793] [0.58456834 0.9820474 ] ...ydata = [0.51224793 0.9820474 0.24469912 0.61647032 0.483...zorder = 2
Note most Artists also have a distinct list of setters; e.g.Line2D.set_color orLine2D.set_linewidth.
Changing Artist data#
In addition to styling properties likecolor andlinewidth, the Line2Dobject has adata property. You can set the data after the line has beencreated usingLine2D.set_data. This is often used for Animations, where thesame line is shown evolving over time (seeAnimations using Matplotlib)
fig,ax=plt.subplots(figsize=(4,2.5))x=np.arange(0,13,0.2)y=np.sin(x)lines=ax.plot(x,y,'-',label='example')lines[0].set_data([x,np.cos(x)])

Manually adding Artists#
Not all Artists have helper methods, or you may want to use a low-level methodfor some reason. For example thepatches.Circle Artist does not have ahelper, but we can still create and add to an Axes using theaxes.Axes.add_artist method:
importmatplotlib.patchesasmpatchesfig,ax=plt.subplots(figsize=(4,2.5))circle=mpatches.Circle((0.5,0.5),0.25,ec="none")ax.add_artist(circle)clipped_circle=mpatches.Circle((1,0.5),0.125,ec="none",facecolor='C1')ax.add_artist(clipped_circle)ax.set_aspect(1)

The Circle takes the center and radius of the Circle as arguments to itsconstructor; optional arguments are passed as keyword arguments.
Note that when we add an Artist manually like this, it doesn't necessarilyadjust the axis limits like most of the helper methods do, so the Artists canbe clipped, as is the case above for theclipped_circle patch.
SeeReference for Matplotlib artists for other patches.
Removing Artists#
Sometimes we want to remove an Artist from a figure without re-specifying thewhole figure from scratch. Most Artists have a usableremove method thatwill remove the Artist from its Axes list. For instancelines[0].remove()would remove theLine2D artist created in the example above.