Remove Interpolation Time Series Graph for Missing Values

I am trying to build time series data, but I have some problems.

I am 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 understand: enter image description here

But I do not need interpolation between missing values!

How can i achieve this?

+4
source share
1 answer

Since you are using pandas, you can 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 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 build it without dates where the data is NaN, you can 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()

What should produce such a plot:

enter image description here

, matplotlib, .plot.

, , . .autofmt_xdate(), .

0

All Articles