删除缺少值的插值时间序列图 [英] Remove interpolation Time series plot for missing values

查看:86
本文介绍了删除缺少值的插值时间序列图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试绘制时间序列数据,但是我遇到了一些问题.

I'm trying to plot a time series data but I have some problems.

我正在使用以下代码:

from matplotlib import pyplot as plt
plt.figure('Fig')
plt.plot(data.index,data.Colum,'g', linewidth=2.0,label='Data')

我得到这个:

但是我不希望在缺失值之间进行插值!

But I dont want the interpolation between missing values!

我该如何实现?

推荐答案

由于您使用的是熊猫,因此您可以执行以下操作:

Since you are using pandas you could do something like this:

import pandas as pd
import matplotlib.pyplot as plt

pd.np.random.seed(1234)
idx = pd.date_range(end=datetime.today().date(), periods=10, freq='D')
vals = pd.Series(pd.np.random.randint(1, 10, size=idx.size), index=idx)
vals.iloc[4:8] = pd.np.nan
print vals

这是带有DatetimeIndex

2016-03-29    4.0
2016-03-30    7.0
2016-03-31    6.0
2016-04-01    5.0
2016-04-02    NaN
2016-04-03    NaN
2016-04-04    NaN
2016-04-05    NaN
2016-04-06    9.0
2016-04-07    1.0
Freq: D, dtype: float64

要绘制没有日期为NaN的日期,可以执行以下操作:

To plot it without dates where data is NaN you could do something like this:

fig, ax = plt.subplots()
ax.plot(range(vals.dropna().size), vals.dropna())
ax.set_xticklabels(vals.dropna().index.date.tolist());
fig.autofmt_xdate()

应该产生这样的情节:

这里的窍门是用调用.plot方法时不会触发matplotlib内部日期处理的某些值范围替换日期.

The trick here is to replace the dates with some range of values that do not trigger matplotlib's internal date processing when you call .plot method.

稍后,绘制完成后,将勾号标签替换为实际日期. (可选)调用.autofmt_xdate()以使标签可读.

Later, when the plotting is done, replace the ticklabels with actual dates. Optionally, call .autofmt_xdate() to make labels readable.

这篇关于删除缺少值的插值时间序列图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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