简体   繁体   English

如何向 seaborn FacetGrid 的每个子图添加单独的 vlines

[英]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:.axvline为例:

产品分面网格

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 根据事件的发生向 Seaborn 面网格图添加垂直线

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?那么,如何遍历所有方面以添加提到的垂直线?

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:您可以使用g.axes.flat访问FacetGridaxes对象,并在给定的 x 位置为每个对象添加一条线,或者从每个单独方面的列表中收集一条线:

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:样品 output:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM