0

I have a dataframe like this. I drawed Seaborn Line plot but the graph is not that i want.

This is my dataframe and code.

import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
import datetime as dt

data = {'year': ['2001', '2001', '2001', '2001', '2001', '2001','2001', '2001', '2001', '2001', '2001', '2001','2001', '2001', '2001', '2001', '2001', '2001','2001', '2001', '2001', '2001', '2001', '2001','2001', '2001', '2001', '2001', '2001', '2001','2001', '2001', '2001', '2001', '2001', '2001','2001', '2001', '2001', '2001', '2001', '2001','2001', '2001', '2001', '2001', '2001', '2001','2001', '2001', '2001', '2001', '2001', '2001','2001', '2001', '2001', '2001', '2001', '2001','2001', '2001', '2001', '2001', '2001', '2001','2001', '2001', '2001', '2001', '2001', '2001'],
        'month': ['1','1','1','1','1','1','2', '2', '2', '2', '2','2',  '3', '3', '3', '3', '3', '3', '4', '4', '4', '4', '4', '4', '5', '5', '5', '5', '5', '5', '6', '6', '6', '6', '6', '6','7','7','7','7','7','7', '8', '8', '8', '8', '8', '8', '9', '9', '9', '9', '9', '9', '10', '10', '10', '10', '10', '10', '11', '11', '11', '11', '11', '11', '12', '12', '12', '12', '12', '12'],
        'unemployment': [0, 0, 0, 0, 0,1,0, 0, 0, 0, 0,1,0, 0, 0, 0, 0,1,0, 1, 1, 0, 0,1,0, 0, 0, 0, 0,1,0, 1, 1, 1, 0,1,0, 0, 0, 1, 0,1,0, 0, 1, 0, 0,1,0,1 , 1, 0, 0,1,0, 1, 1, 1, 1,1,0, 0,1, 1, 1,1,0, 0, 1, 1, 1,1 ]
        }

df = pd.DataFrame(data)

print (df)

date=pd.to_datetime(df['year'].astype(str)  + df['month'].astype(str), format='%Y%m')

df['date']=date

# set figure size
plt.figure( figsize = ( 12, 5))

# plot a simple time series plot
# using seaborn.lineplot()
sns.lineplot( x = 'date',
            y = 'unemployment',
            data = df,
            color="blue")

Output is this.

enter image description here

However, I want to fit smooth line plot like this.

enter image description here

I tried to use "scipy.interpolate.spline" but my code didn't work.Any Idea?Thanks in advance.

  • 1
    what would be the logic of the smoothing? – mozway Mar 15 '22 at 10:07
  • 1
    Smoothing procedures have an underlying mathematical model. What is yours that you want to implement? – Mr. T Mar 15 '22 at 10:08
  • I tried to use "scipy.interpolate.spline" commend to smooth out my data. Does this work? – DAEHYUN KIM Mar 15 '22 at 10:34
  • @JohanC do you have any idea? – DAEHYUN KIM Mar 15 '22 at 13:26
  • [scipy.interpolate.c](https://docs.scipy.org/doc/scipy/reference/generated/scipy.interpolate.splprep.html) is the most-frequently used. See also [Plot smooth line](https://stackoverflow.com/a/53472966/12046409) or [How to smooth a curve in the right way?](https://stackoverflow.com/questions/20618804/how-to-smooth-a-curve-in-the-right-way). It depends a lot on the specific situation. – JohanC Mar 15 '22 at 13:58

0 Answers0