Python 绘制分组数据

Python Plotting Grouped Data

分组数据看起来像

我的方法屈服于

我想要这样的结果

有没有简单的方法可以实现?

作为我使用的测试DataFrame:

         MAPPING CREATED_DTM  counts
0  Beschaedigung  2020-04-30   22738
1  Beschaedigung  2020-05-31   21523
2  Beschaedigung  2020-06-30   18516
3  Beschaedigung  2020-07-31   21436
4  Beschaedigung  2020-08-31   22325
5        Verlust  2020-04-30   20000
6        Verlust  2020-05-31   19500
7        Verlust  2020-06-30   22400
8        Verlust  2020-07-31   19100
9        Verlust  2020-08-31   21100

(CREATED_DTM datetime64[ns] 类型的列)。

创建所需情节的一个优雅解决方案是使用 seaborn

从必要的进口开始:

import seaborn as sns
import matplotlib.pyplot as plt
import matplotlib.dates as md

然后,运行:

sns.lineplot(data=reaktiv_mapping, x='CREATED_DTM', y='counts', hue='MAPPING')
ax = plt.gca()
x = ax.xaxis
x.set_major_locator(md.MonthLocator())
x.set_major_formatter(md.DateFormatter('%Y-%m'))
plt.xticks(rotation = 45)
ax.legend(loc='upper left', bbox_to_anchor=(1.05, 1.0));

对于上面的源数据,我得到如下图:

要获得网格,就像您期望的图片一样,您可以从:

sns.set_style('darkgrid')