Home > Software engineering >  Python Plotting Grouped Data
Python Plotting Grouped Data

Time:03-11

The grouped data looks like

The Grouped Data looks like

My approach yields to

enter image description here

I'd want a result like

enter image description here

Is there an easy way to achieve that?

CodePudding user response:

As the test DataFrame I used:

         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 column of datetime64[ns] type).

An elegant solution to create the plot you want, is to use seaborn.

Start from necessary imports:

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

Then, run:

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));

For the above source data, I got the following plot:

enter image description here

To get the grid, like in your expected picture, you can start with:

sns.set_style('darkgrid')
  • Related