Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zorder specification in matplotlib patch collections?

I am trying to plot a series of rectangles and circles, with the circles in the foreground.

According to the following post, I have to set the zorder argument: Patches I add to my graph are not opaque with alpha=1. Why?

This works fine when I plot all the circles individually, but not when I try to place a series of circles into a collection and add the collection, i.e.

fig,ax=plt.subplots(1)
p_fancy = FancyBboxPatch((1,1),
                         0.5, 0.5,
                         boxstyle="round,pad=0.1",
                         fc='beige',
                         ec='None', zorder=1)
ax.add_patch(p_fancy)
ax.set_xlim([0,2])
ax.set_ylim([0,2])
circ=patches.Circle ((1,1), 0.2, zorder=10)
ax.add_patch(circ)

works fine: enter image description here while

fig,ax=plt.subplots(1)
p_fancy = FancyBboxPatch((1,1),
                         0.5, 0.5,
                         boxstyle="round,pad=0.1",
                         fc='beige',
                         ec='None', zorder=1)
ax.add_patch(p_fancy)
ax.set_xlim([0.,2])
ax.set_ylim([0.,2])
circ=[]
circ.append(patches.Circle ((1,1), 0.2, zorder=10))
coll=PatchCollection(circ)
ax.add_collection(coll)

does not:

enter image description here Is there a reason, or does zorder work differently with patch collections in ways that I don't understand?

like image 584
mzzx Avatar asked Jun 05 '17 04:06

mzzx


People also ask

What is MatPlotLib Zorder?

The Zorder attribute of the Matplotlib Module helps us to improve the overall representation of our plot. This property determines how close the points or plot is to the observer. The higher the value of Zorder closer the plot or points to the viewer.

What are MatPlotLib patches?

A patch is a 2D artist with a face color and an edge color. If any of edgecolor, facecolor, linewidth, or antialiased are None, they default to their rc params setting. The following kwarg properties are supported. Property.

What is MatPlotLib collection?

collections. Classes for the efficient drawing of large collections of objects that share most properties, e.g. a large number of line segments or polygons.

What is Alpha in scatter plot?

Matplotlib allows you to regulate the transparency of a graph plot using the alpha attribute. By default, alpha=1. If you would like to form the graph plot more transparent, then you'll make alpha but 1, such as 0.5 or 0.25.


1 Answers

In the second case you want the PatchCollection to have a defined zorder, not the members of the PatchCollection. Thus, you need to specify zorder for the collection.

circ=[]
circ.append(Circle ((1,1), 0.2))
coll=PatchCollection(circ, zorder=10)
ax.add_collection(coll)
like image 88
ImportanceOfBeingErnest Avatar answered Oct 03 '22 23:10

ImportanceOfBeingErnest