【问题标题】:Seaborn FacetGrid lineplot: set specific line color in single subplotSeaborn FacetGrid lineplot:在单个子图中设置特定的线条颜色
【发布时间】:2020-09-10 14:59:34
【问题描述】:

在 Seaborn 中使用 FacetGrid 构建小倍数线图(或任何图表类型)时,如何手动覆盖单个特定子图的线条颜色,以便突出显示某些内容并将其与其他子图进行对比?

在 Matplotlib 中,这是一项相对简单的任务,但我无法找到一种方法来访问特定方面,然后自定义其中的样式,而让所有其他方面保持不变..

以下代码生成一个 2x2 图表,所有子图具有统一的样式:

tips = sns.load_dataset("tips")
tips = sns.load_dataset("tips")
g = sns.FacetGrid(tips, row="sex", col="smoker", margin_titles=True)
g.map(sns.lineplot, "total_bill", 'tip')

....如果我的目标是增加线宽并更改第一个位置的子图中的颜色,同时保持其他子图不变,我将如何实现?

【问题讨论】:

    标签: python-3.x colors seaborn subplot facet-grid


    【解决方案1】:

    您必须访问由FacetGrid 创建的轴之一,然后访问您要自定义的艺术家,并更改其属性。

    tips = sns.load_dataset("tips")
    g = sns.FacetGrid(tips, row="sex", col="smoker", margin_titles=True)
    g.map(sns.lineplot, "total_bill", 'tip')
    
    # customize first subplot
    ax = g.facet_axis(0,0) # could also do ax=g.axes[0,0]
    l = ax.get_lines()[0] # get the relevant Line2D object (in this case there is only one, but there could be more if using hues)
    l.set_linewidth(3)
    l.set_color('C1')
    

    【讨论】:

      猜你喜欢
      • 2018-09-27
      • 2021-08-18
      • 1970-01-01
      • 2019-01-24
      • 2020-07-29
      • 2016-05-15
      • 1970-01-01
      • 2015-04-06
      相关资源
      最近更新 更多