I have a time-sampled data set with essentially a two-column index (timestamp, ID). However, some timestamps do not have a sample point for a given index.
How can I make a stackplot with Matplotlib for this kind of data?
import pandas as pd
import numpy as np
import io
import matplotlib.pyplot as plt
df = pd.read_csv(io.StringIO('''
A,B,C
1,1,0
1,2,0
1,3,0
1,4,0
2,1,.5
2,2,.2
2,4,.15
3,1,.7
3,3,.1
3,4,.2
'''.strip()))
b = np.unique(df.B)
plt.stackplot(np.unique(df.A),
[df[df.B==_b].C for _b in b],
labels=['B:{0}'.format(_b) for _b in b],
)
plt.xlabel('A')
plt.ylabel('C')
plt.legend(loc='upper left')
plt.show()
When I try this program, Python replies:
TypeError: ufunc 'isfinite' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''
When I manually fill in the missing data points (see blank lines in string literal), the plot works fine.
Is there a straightforward way to "insert" zero records for missing sample data (like this question, but I have two columns functioning as indices, and I don't know how to adapt the solution to my problem) or have Matplotlib plot with holes?