如何从matplotlib图中排除San和Sat?

时间:2013-09-19 11:41:57

标签: python matplotlib pandas

我有像这样的pandas DataFrame:

2013-09-13 20:51:00
2013-09-13 20:52:00
2013-09-13 20:53:00
2013-09-13 20:54:00
2013-09-13 20:55:00
2013-09-13 20:56:00
2013-09-13 20:57:00
2013-09-13 20:58:00
2013-09-13 20:59:00
2013-09-16 00:00:00
2013-09-16 00:01:00
2013-09-16 00:02:00
2013-09-16 00:03:00
2013-09-16 00:04:00
2013-09-16 00:05:00
2013-09-16 00:06:00
2013-09-16 00:07:00
2013-09-16 00:08:00

没有太阳和星期六约会。 Matplotlib pyplot得出如此:

enter image description here

是否可以裁剪除日期之外的图像?像这样: enter image description here

1 个答案:

答案 0 :(得分:0)

如果在matplotlib中绘制一个具有空值(或掩码数组)的系列,则该行将在空值的位置处被破坏。

例如:

import matplotlib.pyplot as plt
import numpy as np

plt.plot([1, 2, 3, np.nan, np.nan, 6, 7, 8, 9])
plt.show()

enter image description here

因此,您要做的是为周末日期添加NaNpandas提供了一些不同的方法来执行此操作。有an overview here,但基本上您想要查看asfreqresample方法。

以日期为例:

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

dates = """2013-09-13 20:51:00
2013-09-13 20:52:00
2013-09-13 20:53:00
2013-09-13 20:54:00
2013-09-13 20:55:00
2013-09-13 20:56:00
2013-09-13 20:57:00
2013-09-13 20:58:00
2013-09-13 20:59:00
2013-09-16 00:00:00
2013-09-16 00:01:00
2013-09-16 00:02:00
2013-09-16 00:03:00
2013-09-16 00:04:00
2013-09-16 00:05:00
2013-09-16 00:06:00
2013-09-16 00:07:00
2013-09-16 00:08:00"""
dates = pd.to_datetime(dates.split('\n'))

# Generate some random y-data...
y = (np.random.random(dates.size) - 0.5).cumsum()

# Resample to a regular 1-minute interval series
# By default, this will put NaNs in where there isn't any data...
data = pd.Series(y, index=dates).asfreq('1Min')

data.plot().set(title='Resampled data')
plt.show()