Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Draw a line at specific position/annotate a Facetgrid in seaborn

A have produced a boxplot with Facetgrid in seaborn the following way

# Import the dataset tips = sns.load_dataset("tips")  # Plot using FacetGrid, separated by smoke plt.style.use('ggplot') g = sns.FacetGrid(tips, col="smoker", size=5, aspect=1.5) g.map(sns.boxplot, "sex", "total_bill", palette='viridis', order=['Male', 'Female']) plt.show() 

I now want to draw to distinct horizontal lines in each of the plot. For instance one horizontal line (with coordinated (0,10)) only in the left-hand side plot and another horizontal line (with coordinates (0,30)) only in the right-hand side plot.

How can I go about doing that?

like image 608
BCArg Avatar asked Aug 17 '18 08:08

BCArg


People also ask

How do you draw a straight line in Seaborn?

Python program to add a horizontal line in a Seaborn plot A barplot will be used in this tutorial and we will put a horizontal line on this bar plot using the axhline() function. First, we import the seaborn and matplotlib. pyplot libraries using aliases 'sns' and 'plt' respectively. Next, we use the sns.

How do you use FacetGrid in Seaborn?

Plotting Small Multiples of Data SubsetsA FacetGrid can be drawn with up to three dimensions − row, col, and hue. The first two have obvious correspondence with the resulting array of axes; think of the hue variable as a third dimension along a depth axis, where different levels are plotted with different colors.

How do you add a line in Seaborn?

Seaborn's refline() function to add horizontal/vertical lines in subplots. To add a horizontal and vertical line we can use Seaborn's refline() function with x and y y co-ordinates for the locations of the horizontal and vertical lines.

How do you plot two graphs on the same plot in Seaborn?

In Seaborn, we will plot multiple graphs in a single window in two ways. First with the help of Facetgrid() function and other by implicit with the help of matplotlib. data: Tidy dataframe where each column is a variable and each row is an observation.


Video Answer


1 Answers

You can get a list of axes used in the FacetGrid using FacetGrid.axes which returns the axes used. You can then do all of the normal matplotlib operations using these axes, such as axhline for horizontal lines, or plt.text for putting text on the axes:

import seaborn as sns import matplotlib.pyplot as plt tips = sns.load_dataset("tips")  # Plot using Facegrid, separated by smoke plt.style.use('ggplot') g = sns.FacetGrid(tips, col="smoker", size=5, aspect=1.5) g.map(sns.boxplot, "sex", "total_bill", palette='viridis', order=['Male', 'Female'])  ax1, ax2 = g.axes[0]  ax1.axhline(10, ls='--') ax2.axhline(30, ls='--')  ax1.text(0.5,25, "Some text") ax2.text(0.5,25, "Some text")  plt.show() 

enter image description here

like image 179
DavidG Avatar answered Sep 21 '22 18:09

DavidG