【问题标题】:seaborn plot from total来自总的seaborn地块
【发布时间】:2017-07-18 10:02:42
【问题描述】:

我有以下数据框:

df = pd.DataFrame({'group': ['Red', 'Red', 'Red', 'Blue', 'Blue', 'Blue'],
                   'valueA_found': [10, 40, 50, 20, 50, 70],
                   'valueA_total': [100,200, 210, 100, 200, 210],
                  'date': ['2017-01-01', '2017-02-01', '2017-03-01', '2017-01-01', '2017-02-01', '2017-03-01']})

并且可以创建一个情节:

fig, ax = plt.subplots(figsize=(15,8))
sns.set_style("whitegrid")
g = sns.barplot(x="date", y="valueA_found", hue="group", data=df)
# g.set_yscale('log')
g.set_xticklabels(df.date, rotation=45)
g.set(xlabel='date', ylabel='value from total')

但是,我希望在每个时间点看到以下内容: 正如您所看到的,每个模型 valueA_found 被绘制为条形,而总数被绘制为单个条形。

最初建议,也可以将总数绘制为一条线 - 但正如 cmets 中所述,最好也生成一个条形图。 valueA_total 即每个组每月的总数应该相同。

【问题讨论】:

  • 什么是ValueB?目的是在条形顶部绘制一条粉色线吗?
  • 它不需要是粉红色的。上下文:这些值是来自分类机器学习问题的绝对值。组构成不同的模型。我想随着时间的推移评估这些模型,并希望显示从每个模型和每个月的 valueB 总数中找到了多少 valueA。
  • 我的评论是为了鼓励你解释你想要的问题。例如。 “线”这个词甚至没有出现在其中。另外,那条线的要求是什么?这一点尤其重要,因为分类图上的连接点可能看起来不是很直观,如果要连接独立组(如果这是你想要的?),则更不直观;你解释得越好,获得满意答案的机会就越大。
  • 我希望现在更清楚了。
  • 由于图片中您现在只有一组,我现在不知道如何处理第二组。我可以想象你想要this image 中显示的东西。如果是这种情况,我可以提供答案。

标签: python matplotlib visualization seaborn


【解决方案1】:

一个选项可能是在第一个数据集后面的不饱和/更透明的条形图中绘制总值。

import matplotlib.pyplot as plt
import pandas as pd
import seaborn.apionly as sns

df = pd.DataFrame({'group': ['Red', 'Red', 'Red', 'Blue', 'Blue', 'Blue'],
                   'valueA': [10, 40, 50, 20, 50, 70],
                   'valueB': [100,200, 210, 100, 200, 210],
                  'date': ['2017-01-01', '2017-02-01', '2017-03-01', 
                           '2017-01-01', '2017-02-01', '2017-03-01']})

fig, ax = plt.subplots(figsize=(6,4))

sns.barplot(x="date", y="valueB", hue="group", data=df, 
            ax=ax, palette={"Red":"#f3c4c4","Blue":"#c5d6f2" }, alpha=0.6)
sns.barplot(x="date", y="valueA", hue="group", data=df, 
            ax=ax, palette={"Red":"#d40000","Blue":"#0044aa" })


ax.set_xticklabels(df.date, rotation=45)
ax.set(xlabel='date', ylabel='value from total')

plt.show()

或者只是在后台放一个条形图,假设每组的总数总是相同的:

import matplotlib.pyplot as plt
import pandas as pd
import seaborn.apionly as sns

df = pd.DataFrame({'group': ['Red', 'Red', 'Red', 'Blue', 'Blue', 'Blue'],
                   'valueA': [10, 40, 50, 20, 50, 70],
                   'valueB': [100,200, 210, 100, 200, 210],
                  'date': ['2017-01-01', '2017-02-01', '2017-03-01', 
                           '2017-01-01', '2017-02-01', '2017-03-01']})

fig, ax = plt.subplots(figsize=(6,4))

sns.barplot(x="date", y="valueB", data=df[df.group=="Red"], 
            ax=ax,  color="#e7e2e8", label="total")
sns.barplot(x="date", y="valueA", hue="group", data=df, 
            ax=ax, palette={"Red":"#d40000","Blue":"#0044aa" })


ax.set_xticklabels(df.date, rotation=45)
ax.set(xlabel='date', ylabel='value from total')

plt.show()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-23
    • 2019-12-23
    • 2021-09-30
    • 2018-08-03
    • 2018-12-15
    相关资源
    最近更新 更多