Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add individual vlines to every subplot of seaborn FacetGrid

I want to add a vertical line to every subplot to mark the individual launch date of each product. Every vertical line should display the date. But i am too beginner to figure this out. I tried .axvline as an example:

Facet Grid of Products

Here is the code:

g = sns.FacetGrid(df2, col='Product', hue='Vendor', col_wrap=4, height=3.5)
g = g.map(plt.plot, 'Date', 'Volumes')
g = g.map(plt.fill_between, 'Date', 'Volumes', alpha=0.2).set_titles("{col_name} Product")
g = g.set_titles("{col_name}")
g = g.set(xticks=[0, 12, 23])
g = g.set(xlabel='')

plt.axvline(x='Apr 19', color='r', linestyle=':')

I found the following apporaches, but i can't really make sense of it, or apply it to my own purposes:

Marking specific dates when visualizing a time series

Add vertical lines to Seaborn Facet Grid plots based on the occurrence of an event

I've created two lists with product names and corresponding launch dates:

product_names = ['Product A', 'Product B','Product C', 'Product D','Product E', 'Product F',
                 'Product G', 'Product H','Product I', 'Product J',]

launch_dates = ['2019-02-01', '2019-09-01', '2019-12-01', '2019-12-01',
                '2020-02-01', '2020-05-01', '2020-07-01', '2020-07-01',
                '2020-08-01', '2020-07-15']

launch_dates = [datetime.strptime(d, "%Y-%m-%d") for d in launch_dates]

So, how can I iterate through all facets to add the mentioned vertical line?

like image 817
Zeno Avatar asked Feb 11 '21 12:02

Zeno


People also ask

How do you use FacetGrid in Seaborn?

Plotting Small Multiples of Data Subsets FacetGrid object takes a dataframe as input and the names of the variables that will form the row, column, or hue dimensions of the grid. The variables should be categorical and the data at each level of the variable will be used for a facet along that axis.

What is the primary goal when using FacetGrid in Seaborn?

FacetGrid() : FacetGrid class helps in visualizing distribution of one variable as well as the relationship between multiple variables separately within subsets of your dataset using multiple panels.

How do you insert a horizontal 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 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.


1 Answers

You could access the axes objects of the FacetGrid with g.axes.flat and add a line to each of them either at a given x-position or collected from a list for each individual facet:

import matplotlib.pyplot as plt
import seaborn as sns

tips = sns.load_dataset("tips")

g = sns.FacetGrid(tips, col="time", row="sex")
g.map_dataframe(sns.histplot, x="total_bill", binwidth=2)
g.set_axis_labels("Total bill", "Count")

line_position = [14, 23, 11, 39]

for ax, pos in zip(g.axes.flat, line_position):
    ax.axvline(x=pos, color='r', linestyle=':')

plt.tight_layout()
plt.show()

Sample output:

enter image description here

like image 52
Mr. T Avatar answered Oct 14 '22 00:10

Mr. T