How to plot each year as a line with months on the x-axis

Viewed 1888

I have a question, I have the following dataframe containing multiple years and months with an total sum:

import pandas as pd

data = {'Bedrag': [210406.49, 191369.87, 118458.65, 81682.95, 90571.61, 196374.53, 223619.85, 144773.64, 240221.67, 110666.73, 108633.49, 194808.85, 103302.85, 186419.17, 96297.53, 81404.79, 94874.21, 209520.46, 270694.15, 107448.21, 188290.77, 163761.78, 168799.28, 190937.74, 127930.28, 262299.96, 48658.0, 48027.57, 220501.67, 234570.63, 89188.45, 233270.46, 179647.23, 272358.86],
        'Jaar': [2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021],
        'maand': ['April', 'August', 'December', 'February', 'January', 'July', 'June', 'March', 'May', 'November', 'October', 'September', 'April', 'August', 'December', 'February', 'January', 'July', 'June', 'March', 'May', 'November', 'October', 'September', 'April', 'August', 'February', 'January', 'July', 'June', 'March', 'May', 'October', 'September']}

correct_omzet = pd.DataFrame(data)

# display(correct_omzet.head())

      Bedrag  Jaar     maand
0  210406.49  2019     April
1  191369.87  2019    August
2  118458.65  2019  December
3   81682.95  2019  February
4   90571.61  2019   January

Now I would like to plot this with seaborn in a line plot to see a season sales pattern. But when I try to plot as below, the outcome only shows the 12 months.

plt.figure(figsize=(25,6))
sns.set_style("darkgrid")
sns.lineplot(data=correct_omzet, x="maand", y="Bedrag", ci=None, color="green", marker='o')

Line plot 2019

I've also tried to plot it without seaborn:

correct_omzet.plot.line(x='maand', rot=90, y='Bedrag', title='Omzetpatroon 2019 - 2021', colormap='Spectral', figsize=(20,8))
plt.minorticks_on()

plot without seaborn

But then I can't figure out to get all the months as x-axis titles.

1 Answers
  • The clearest way to make seasonal observations is to plot each year as a separate line, with the x-axis as the months
  • Set the month column as ordered categorical with pd.Categorical, which will ensure the months are plotted in order. The built-in calendar package is used for the ordered month_name list, or create a list manually.
  • Use the figure-level seaborn.relplot with kind='line' to plot the data and separate 'Jaar' with the hue= parameter.
    • The figure-level plot is used because it has height and aspect for sizing, removing the need to create fig, ax = plt.subplots(figsize=(5, 10)). Otherwise use the axes-level seaborn.lineplot.
  • Also see Weather Visualization for Portland, OR: 1940 - 2020
  • Tested in python 3.8.12, pandas 1.3.4, matplotlib 3.4.3, seaborn 0.11.2
import pandas as pd
import seaborn as sns
from calendar import month_name as mn

# month list
months = mn[1:]

# convert the column to categorical and ordered
correct_omzet.maand = pd.Categorical(correct_omzet.maand, categories=months, ordered=True)

# plot the data
p = sns.relplot(kind='line', data=correct_omzet, x='maand', y='Bedrag', hue='Jaar', aspect=2.5, marker='o')

enter image description here

Related