我具有以下格式的数据
+-------------+------------+
| date | ThisOrThat |
+-------------+------------+
| 2011-03-31 | this |
| 2011-03-31 | that |
| 2011-03-31 | this |
| 2011-04-30 | this |
| 2011-04-30 | that |
| ... | ... |
| 2018-01-31 | this |
| 2018-01-31 | that |
+-------------+------------+
我需要在几个月内绘制一个多条形图,这样才能显示“此”与“该”计数的分布。
这是我需要的条形图格式的示例
我试图按日期对数据进行分组,但是我在如何访问和绘制分组的值计数方面陷入困境
答案 0 :(得分:0)
下面的代码从示例数据框中绘制了count()
和mean()
。
导入库
import pandas as pd
import numpy as np
import datetime
from matplotlib import pyplot as plt
import seaborn as sns
%matplotlib inline
创建示例数据框
df = pd.DataFrame({'Date': ['2018-01-23', '2018-01-23','2018-02-11', '2018-01-03','2018-02-01', '2018-04-04','2018-04-01',
'2018-02-26', '2018-02-21','2018-01-07', '2018-01-04','2018-02-28', '2018-04-03','2018-04-02',
],
'ThisOrThat': ['this', 'this', 'that', 'that', 'this', 'this','this',
'this', 'this', 'that', 'that', 'this', 'that','that',
],
'value': [212,333,433,232,454,232,56,454,676,454,677,454, 676,455]
})
df['Date'] = pd.to_datetime(df['Date'])
df['month_yr'] = df['Date'].apply(lambda x: x.strftime('%b-%Y'))
df.head(2)
地块计数
sns.countplot(x="month_yr", hue="ThisOrThat", data=df)
plt.xlabel('Month_Year')
plt.ylabel('Count')
绘制带有误差线的平均值
sns.barplot(x="month_yr", y="value", hue="ThisOrThat", data=df)
plt.xlabel('Month_Year')
plt.ylabel('Mean value')