问题:我有几天的时间序列数据,我使用Seaborn Python库的sns.FacetGrid函数以分面形式绘制这些数据。但在某些情况下,我发现该Seaborn函数在两个读数之间连续地绘制了缺失值(nan
值),而Matplotlib则将缺失值显示为一个间隙,这更符合实际。下面是一个演示例子:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
# create timeseries data for 3 days such that day two contains NaN values
time_duration1 = pd.date_range('1/1/2018', periods=24,freq='H')
data1 = np.random.randn(len(time_duration1))
ds1 = pd.Series(data=data1,index=time_duration1)
time_duration2 = pd.date_range('1/2/2018',periods=24,freq='H')
data2 = [float('nan')]*len(time_duration2)
ds2 = pd.Series(data=data2,index=time_duration2)
time_duration3 = pd.date_range('1/3/2018', periods=24,freq='H')
data3 = np.random.randn(len(time_duration3))
ds3 = pd.Series(data=data3,index=time_duration3)
# combine all three days series and then convert series into pandas dataframe
DS = pd.concat([ds1,ds2,ds3])
DF = DS.to_frame()
DF.plot()
它的结果如下图所示:
![enter image description here](https://istack.dev59.com/Q2nmE.webp)
DF['col'] = np.ones(DF.shape[0])# dummy column but required for facets
DF['timestamp'] = DF.index
DF.columns = ['data_val','col','timestamp']
g = sns.FacetGrid(DF,col='col',col_wrap=1,size=2.5)
g.map_dataframe(plt.plot,'timestamp','data_val')
请看,seaborn绘图展示了缺失数据的情况。我如何强制seaborn不使用这样的线来绘制NaN
值?
注:这是一个虚拟示例,而且我无论如何需要facet grid来绘制我的数据。