Matplotlib - smooth a line

Deadly 提交于 2021-02-11 15:45:51

问题


I'm looking for an advice on how to smoothen a trend line.

This is the code:

import pandas as pd
from numpy import random

#Generating the data frame
df = pd.DataFrame(data = random.randn(5,4), index = ['A','B','C','D','E'],
columns = ['W','X','Y','Z'])

df['W'] = ['10/01/2018 12:00:00','10/03/2018 13:00:00',
           '10/03/2018 12:30:00','10/04/2018 12:05:00',
           '10/08/2018 12:00:15']

pd.to_datetime(df['W'])

print(df.head()) 

#Plotting hte graph
fig, ax = plt.subplots()
df.plot(x="W", y="X", ax=ax, color='salmon', alpha=0.5, marker='o')
df.plot(x="W", y="Y", ax=ax, color='royalblue', alpha=0.4, marker='o')

This is what I get:

I would like to get a smooth line, something like this:


回答1:


You can use the df.resample method and df.interpolate to do what you desire.

First, df.resample computes the datetimes at which we will interpolate. After this we can go ahead and interpolate.

import pandas as pd
from numpy import random
import matplotlib.pyplot as plt

#Generating the data frame
df = pd.DataFrame(data = random.randn(5,4), index = ['A','B','C','D','E'], 
                                          columns = ['W','X','Y','Z'])

df['W'] = pd.to_datetime(['10/01/2018 12:00:00','10/03/2018 13:00:00',
                           '10/03/2018 12:30:00','10/04/2018 12:05:00',
                           '10/08/2018 12:00:15'], infer_datetime_format=True)

#Plotting
fig, ax = plt.subplots(1, 1)
df.plot(x="W", y="X", ax=ax, color='salmon', alpha=0.5, marker='o')
df.plot(x="W", y="Y", ax=ax, color='royalblue', alpha=0.4, marker='o')

df = df.resample('T', on='W').mean()

df.interpolate(method='spline', order=3, inplace=True)

df.plot(y='X', alpha=0.5, ax=ax, legend=False)
df.plot(y='Y',  alpha=0.4, ax=ax, legend=False)


来源:https://stackoverflow.com/questions/54902425/matplotlib-smooth-a-line

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!