【问题标题】:seaborn barplot with labels for x values (and no hue)seaborn barplot 带有 x 值标签(并且没有色调)
【发布时间】:2020-01-29 21:59:06
【问题描述】:

我的数据框包含两列,我想在条形图中绘制它们的值。像这样:

import seaborn as sns

# load sample data and drop all but two columns
tips = sns.load_dataset("tips")
tips= tips[["day", "total_bill"]]

sns.set(style="whitegrid")
ax = sns.barplot(x="day", y="total_bill", data=tips)

在此条形图之上,我还想为每个 x 值添加一个带有标签的图例。 Seaborn 支持这一点,但据我所知,它仅在您指定色调参数时才有效。然后图例中的每个标签对应一个色调值。

我可以创建一个说明 x 值的图例吗? 这可能是一个令人困惑的问题。我不想重命名轴的标签或沿轴的刻度。相反,我想有一个单独的图例和额外的解释。我的条形图为我提供了一些很好的空间来放置这个图例,而解释太长了,无法将它们作为记号。

【问题讨论】:

    标签: python pandas matplotlib bar-chart seaborn


    【解决方案1】:

    这是你想要的吗:

    sns.set(style="whitegrid")
    ax = sns.barplot(x="day", y="total_bill", data=tips)
    
    ax.legend(ax.patches, ['1','2','3','Something that I can\'t say'], loc=[1.01,0.5])
    

    输出:

    【讨论】:

      猜你喜欢
      • 2020-06-27
      • 1970-01-01
      • 1970-01-01
      • 2020-08-05
      • 2015-10-16
      • 1970-01-01
      • 2020-02-04
      • 1970-01-01
      • 2021-09-03
      相关资源
      最近更新 更多