24107

Patches in seaborn

Question:

My intention is to add a patch at a specific coordinate in seaborn's lmplot :

<a href="https://i.stack.imgur.com/OCxEk.jpg" rel="nofollow"><img alt="Intended seaborn plot with patch" class="b-lazy" data-src="https://i.stack.imgur.com/OCxEk.jpg" data-original="https://i.stack.imgur.com/OCxEk.jpg" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" /></a>

Is there anyway to add a rectangular/square patch to lmplot?

I was able to get the plot printed out through sns.lmplot(). But when I try to add the rectangular patch using ax.add_patch() statement with relevant coordinate's, it error's out.

#Sample code to generate lmplot and add patch ax= sns.lmplot('A', 'B', hue="group", data=res_me,fit_reg=False, \ palette="Set1",size=10, aspect=1, scatter_kws={"s": 100,"linewidths":2,"edgecolor":"black"}) ax.add_patch(patches.Rectangle((0.912, 0.72), 1.02, .802,fill=False,edgecolor='green',lw=3))

I get the following error.

AttributeError: 'FacetGrid' object has no attribute 'add_patch'

So can we add patches to FacetGrid ?

Answer1:

lmplot as you've learned, returns a FacetGrid, which stores all of its axes in an axes property as a 2D numpy array.

So you just need to do something like:

fg = sns.lmplot('A', 'B', hue="group", data=res_me,fit_reg=False, palette="Set1",size=10, aspect=1, scatter_kws={"s": 100,"linewidths":2,"edgecolor":"black"}) fg.axes[0, 0].add_patch(patches.Rectangle((0.912, 0.72), 1.02, 0.802,fill=False,edgecolor='green',lw=3))

Note that if your FacetGrid only has one Axes object in it, you can access it directly with fg.ax

Recommend

  • XML - XSLT - Using two XML files - Additions to XML file consulting another XML file
  • I want to perform a multi-set intersection using C++
  • Calculate Median Image in Matlab
  • how to fill triangle in swift using Core graphics
  • Order of columns and rows with ggplot2 tile
  • shortcut to out-of-box of bracket
  • How to use factorplot to annotate bars with categorical values or to plot 4 variables?
  • How to assign same color to factors across plots in a nested loop for ggplot?
  • Python ValueError: non-broadcastable output operand with shape (124,1) doesn't match the broadc
  • Inconsistency between sequences and seqiplot
  • Setting the color label and varying the color palette in qplot
  • Compare two files and write to a new file but only output a few lines?
  • Why must we declare a variable name when adding a method to a struct in Golang?
  • Python ImageIO Gif Set Delay Between Frames
  • SetWindowsHookEx does not react on media keys
  • Reading a file into a multidimensional array
  • Android application: how to use the camera and grab the image bytes?
  • Textfile Structure (tables)
  • How to clear text inside text field when radio button is select
  • Change multiple background-images with jQuery
  • Scrapy recursive link crawler
  • Linq Objects Group By & Sum
  • Android screen density dpi vs ppi
  • Read text file and split every line in MSBuild
  • req.body is undefined - nodejs
  • How to add a column to a Pandas dataframe made of arrays of the n-preceding values of another column
  • DirectX11 ClearRenderTargetViewback with transparent buffer?
  • Change an a tag attribute in JavaScript based on screen width
  • Unit Testing MVC Web Application in Visual Studio and Problem with QTAgent
  • Acquiring multiple attributes from .xml file in c#
  • Hits per day in Google Big Query
  • Why joiner is not used after Sequence generator or Update statergy
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • embed rChart in Markdown
  • Linking SubReports Without LinkChild/LinkMaster
  • How can I remove ASP.NET Designer.cs files?
  • XCode 8, some methods disappeared ? ex: layoutAttributesClass() -> AnyClass
  • Android Heatmap on canvas or ImageView
  • java string with new operator and a literal