gra*_*age 53 python matplotlib
我有一组数据,我想绘制为线图.对于每个系列,缺少一些数据(但每个系列都不同).目前,matplotlib不会绘制跳过缺失数据的行:例如
import matplotlib.pyplot as plt
xs = range(8)
series1 = [1, 3, 3, None, None, 5, 8, 9]
series2 = [2, None, 5, None, 4, None, 3, 2]
plt.plot(xs, series1, linestyle='-', marker='o')
plt.plot(xs, series2, linestyle='-', marker='o')
plt.show()
导致线条中有间隙的图.如何告诉matplotlib在间隙中绘制线条?(我宁愿不必插入数据).
Tho*_*anz 74
您可以通过以下方式屏蔽NaN值:
import numpy as np
import matplotlib.pyplot as plt
xs = np.arange(8)
series1 = np.array([1, 3, 3, None, None, 5, 8, 9]).astype(np.double)
s1mask = np.isfinite(series1)
series2 = np.array([2, None, 5, None, 4, None, 3, 2]).astype(np.double)
s2mask = np.isfinite(series2)
plt.plot(xs[s1mask], series1[s1mask], linestyle='-', marker='o')
plt.plot(xs[s2mask], series2[s2mask], linestyle='-', marker='o')
plt.show()
这导致

熊猫的解决方案:
import matplotlib.pyplot as plt
import pandas as pd
def splitSerToArr(ser):
    return [ser.index, ser.as_matrix()]
xs = range(8)
series1 = [1, 3, 3, None, None, 5, 8, 9]
series2 = [2, None, 5, None, 4, None, 3, 2]
s1 = pd.Series(series1, index=xs)
s2 = pd.Series(series2, index=xs)
plt.plot( *splitSerToArr(s1.dropna()), linestyle='-', marker='o')
plt.plot( *splitSerToArr(s2.dropna()), linestyle='-', marker='o')
plt.show()
splitSerToArr在 Pandas 中绘图时,该功能非常方便。这是输出:
Qouting @罗格·卡西斯(链接):
Matplotlib仅在连续的(有效)数据点之间绘制一条线,并在NaN值之间留出间隙。
如果您使用的是熊猫的解决方案,:
#pd.Series 
s.dropna().plot() #masking (as @Thorsten Kranz suggestion)
#pd.DataFrame
df['a_col_ffill'] = df['a_col'].ffill(method='ffill')
df['b_col_ffill'] = df['b_col'].ffill(method='ffill')  # changed from a to b
df[['a_col_ffill','b_col_ffill']].plot()