Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove interpolation Time series plot for missing values

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

I'm using this code:

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

And I get this: enter image description here

But I dont want the interpolation between missing values!

How can I achieve this?

like image 348
Lorenzo Bottaccioli Avatar asked Apr 07 '16 09:04

Lorenzo Bottaccioli


1 Answers

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

Here is an example of a column from a DataFrame with 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

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()

Which should produce a plot like this:

enter image description here

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.

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

like image 197
Primer Avatar answered Oct 06 '22 08:10

Primer