Matplotlib - 平滑一条线 [英] Matplotlib - smooth a line

查看:75
本文介绍了Matplotlib - 平滑一条线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找有关如何平滑趋势线的建议.

这是代码:

将pandas导入为pd从numpy导入随机#生成数据框df = pd.DataFrame(data = random.randn(5,4), index = ['A','B','C','D','E'],栏= ['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'])打印(df.head())#绘制高温图无花果,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')

这是我得到的:

我想要一条流畅的线,像这样:

解决方案

您可以使用 df.resample 方法和 df.interpolate 来完成您想要的事情./p>

首先, df.resample 计算我们进行插值的日期时间.在此之后,我们可以继续进行插值.

 将熊猫作为pd导入从numpy导入随机导入matplotlib.pyplot作为plt#生成数据框df = pd.DataFrame(data = random.randn(5,4),index = ['A','B','C','D','E'],列 = ['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)#绘图无花果,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)

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:

解决方案

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)

这篇关于Matplotlib - 平滑一条线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆