Python Matplotlib - Smooth plot line for x-axis with date values -


im trying smooth graph line out since x-axis values dates im having great trouble doing this. have dataframe follows

import matplotlib.pyplot plt import numpy np import pandas pd %matplotlib inline  startdate = '2015-05-15' enddate = '2015-12-5' index = pd.date_range(startdate, enddate) data = np.random.normal(0, 1, size=len(index)) cols = ['value']  df = pd.dataframe(data, index=index, columns=cols) 

then plot data

fig, axs = plt.subplots(1,1, figsize=(18,5)) x = df.index y = df.value axs.plot(x, y) fig.show() 

we get

enter image description here

now smooth line there usefull staekoverflow questions allready like:

but cant seem code working example, suggestions?

you can use interpolation functionality shipped pandas. because dataframe has value every index already, can populate index more sparse, , fill every non-existent indices nan values. then, after choosing 1 of many interpolation methods available, interpolate , plot data:

index_hourly = pd.date_range(startdate, enddate, freq='1h') df_smooth = df.reindex(index=index_hourly).interpolate('cubic') df_smooth = df_smooth.rename(columns={'value':'smooth'})  df_smooth.plot(ax=axs, alpha=0.7) df.plot(ax=axs, alpha=0.7) fig.show() 

enter image description here


Comments

Popular posts from this blog

sql - can we replace full join with union of left and right join? why not? -

javascript - Parallax scrolling and fixed footer code causing width issues -

iOS: Performance of reloading UIImage(name:...) -