【问题标题】:how to annotate for every subplot in seaborn facetgrid?如何为seaborn facetgrid中的每个子图注释?
【发布时间】:2022-10-12 22:41:12
【问题描述】:

我创建了一个 facetgrid,其中列是我的年份,x 轴是月份,Y 轴是消费水平。我已经放置了一条水平线,显示每个子图的平均消费水平。我还想用注释说明每一列的平均数字是多少,这里是年度数据。任何想法如何做到这一点?

My facetgrid

【问题讨论】:

    标签: matplotlib seaborn


    【解决方案1】:

    由于您没有提供任何数据,因此我使用参考中的数据应用了您的代码。 我创建了一个函数来注释字符串并添加一条水平线,因为绘制水平线的函数是未知的。

    import seaborn as sns
    
    flights = sns.load_dataset('flights')
    
    import matplotlib.pyplot as plt
    
    def annotate(data, **kws):
        n = data.passengers.mean()
        ax = plt.gca()
        ax.text(.1, .8, "Mean = {:.1f}".format(n), transform=ax.transAxes)
        ax.axhline(y=n, color='red')
        
    g2 = sns.FacetGrid(flights, col='year', col_wrap=4)
    g2.map(sns.barplot, 'month', 'passengers', alpha=0.7)
    g2.map_dataframe(annotate)
    

    【讨论】:

      猜你喜欢
      • 2021-05-15
      • 1970-01-01
      • 2022-01-12
      • 2018-12-09
      • 1970-01-01
      • 2019-07-30
      • 1970-01-01
      • 2019-01-24
      相关资源
      最近更新 更多