def plot_states_and_var(data, hidden_states, cmap=None, columns=None, by='Activity'):
"""
Make a plot of the data and the states
Parameters
----------
data : pandas DataFrame
Data to plot
hidden_states: iteretable
the hidden states corresponding to the timesteps
columns : list, optional
Which columns to plot
by : str
The column to group on
"""
fig, ax = plt.subplots(figsize=(15, 5))
if columns is None:
columns = data.columns
df = data[columns].copy()
stateseq = np.array(hidden_states)
stateseq_norep, durations = rle(stateseq)
datamin, datamax = np.array(df).min(), np.array(df).max()
y = np.array(
[datamin, datamax])
maxstate = stateseq.max() + 1
x = np.hstack(([0], durations.cumsum()[:-1], [len(df.index) - 1]))
C = np.array(
[[float(state) / maxstate] for state in stateseq_norep]).transpose()
ax.set_xlim((min(x), max(x)))
if cmap is None:
num_states = max(hidden_states) + 1
colormap, cmap = get_color_map(num_states)
pc = ax.pcolorfast(x, y, C, vmin=0, vmax=1, alpha=0.3, cmap=cmap)
plt.plot(df.as_matrix())
locator = AutoDateLocator()
locator.create_dummy_axis()
num_index = pd.Index(df.index.map(date2num))
ticks_num = locator.tick_values(min(df.index), max(df.index))
ticks = [num_index.get_loc(t) for t in ticks_num]
plt.xticks(ticks, df.index.strftime('%H:%M')[ticks], rotation='vertical')
cb = plt.colorbar(pc)
cb.set_ticks(np.arange(1./(2*cmap.N), 1, 1./cmap.N))
cb.set_ticklabels(np.arange(0, cmap.N))
# Plot the activities
if by is not None:
actseq = np.array(data[by])
sca = ax.scatter(
np.arange(len(hidden_states)), #data.index,
np.ones_like(hidden_states) * datamax,
c=actseq,
edgecolors='none'
)
plt.show()
return fig, ax
评论列表
文章目录