def plot_time_series(df, target, tag='eda', directory=None):
r"""Plot time series data.
Parameters
----------
df : pandas.DataFrame
The dataframe containing the ``target`` feature.
target : str
The target variable for the time series plot.
tag : str
Unique identifier for the plot.
directory : str, optional
The full specification of the plot location.
Returns
-------
None : None.
References
----------
http://seaborn.pydata.org/generated/seaborn.tsplot.html
"""
logger.info("Generating Time Series Plot")
# Generate the time series plot
ts_plot = sns.tsplot(data=df[target])
ts_fig = ts_plot.get_figure()
# Save the plot
write_plot('seaborn', ts_fig, 'time_series_plot', tag, directory)
#
# Function plot_candlestick
#
python类tsplot()的实例源码
def plot_reward_by_episode(self, ax=None):
self.make_palette()
full_data = pd.DataFrame()
for idx, (benchmark_data, name) in enumerate(self.benchmarks):
plot_data = to_timeseries(benchmark_data, x_label="Episode", y_label="Average Episode Reward",
target=rewards_by_episode, cut_x=benchmark_data.min_x('episodes'), smooth=10)
plot_data['Benchmark'] = name
full_data = full_data.append(plot_data)
plot = sns.tsplot(data=full_data, time="Episode", value="Average Episode Reward", unit="experiment",
condition='Benchmark', ax=ax, ci=[68, 95], color=self.palette)
return plot
def plot_reward_by_timestep(self, ax=None):
self.make_palette()
full_data = pd.DataFrame()
for idx, (benchmark_data, name) in enumerate(self.benchmarks):
plot_data = to_timeseries(benchmark_data, x_label="Time step", y_label="Average Episode Reward",
target=rewards_by_timestep, cut_x=benchmark_data.min_x('timesteps'), smooth=10)
plot_data['Benchmark'] = name
full_data = full_data.append(plot_data)
plot = sns.tsplot(data=full_data, time="Time step", value="Average Episode Reward", unit="experiment",
condition='Benchmark', ax=ax, ci=[68, 95], color=self.palette)
return plot
def plot_reward_by_second(self, ax=None):
self.make_palette()
full_data = pd.DataFrame()
for idx, (benchmark_data, name) in enumerate(self.benchmarks):
plot_data = to_timeseries(benchmark_data, x_label="Second", y_label="Average Episode Reward",
target=rewards_by_second, cut_x=benchmark_data.min_x('seconds'), smooth=10)
plot_data['Benchmark'] = name
full_data = full_data.append(plot_data)
plot = sns.tsplot(data=full_data, time="Second", value="Average Episode Reward", unit="experiment",
condition='Benchmark', ax=ax, ci=[68, 95], color=self.palette)
return plot
def phase1_plot_update(f, ax1, ax2, data, passed, results, fails, failure_criteria, progress):
ax1.cla()
ax2.cla()
sns.tsplot(data.ch1, time=data.time, color="g" if passed else "r", ax=ax1, interpolate=True)
if len(results) > 1:
try:
sns.distplot(results, norm_hist=False, rug=True, ax=ax2)
except Exception:
pass
ax1.set(title='Transition Waveform', xlabel='Time (sec)', ylabel='Amplitude (V)')
ax2.set(title="Risetime Histogram", ylabel="Density", xlabel="Risetime (sec)")
ax2.annotate('Outside Spec: %d / %d\nCompleted %d%%' % (len(fails), len(results), progress), xy=(0.75,0.90), xycoords='axes fraction', fontsize=14)
xlims = ax2.get_xlim()
ax2.axvspan(failure_criteria,xlims[1] - 0.001*(xlims[1] - xlims[0]), alpha=0.1, color='red')
plt.pause(0.01)
def phase2_plot_update(f, ax1, data, passed, peak, hf1, hf2, progress):
# Update the plot with latest measurement
ax1.cla()
freq_mhz = map(lambda x: x/1e6, data.frequency)
sns.tsplot(data.ch1, time=freq_mhz, ax=ax1, interpolate=True)
ax1.plot(peak[0]/1e6, peak[1], 'v')
ax1.set(title='Beatnote Spectrum', xlabel='Frequency (MHz)', ylabel='Power (dBm)')
ax1.annotate('Peak (%.2f MHz)\nLinewidth (%.2f kHz)\nCompleted %d%%' % (peak[0]/1e6, (hf2[0]-hf1[0])/1e3, progress), xy=(0.80,0.90), xycoords='axes fraction', fontsize=14)
ax1.axvspan(hf1[0]/1e6,hf2[0]/1e6, alpha=0.1, color='green' if passed else 'red')
plt.pause(0.01)
def show_km(y,
n=4,
c=['b', 'g', 'r', 'k'],
title='KMeans Clustering'):
km = cluster.KMeans(n)
yi = km.fit_predict(y)
#c = ['b', 'g', 'r', 'k']
for i in range(n):
sns.tsplot(y[yi==i], color=c[i])
plt.title(title)
def show_cluster(y,
yi,
c=['b', 'g', 'r', 'k'],
title='Clustering Resutls'):
#km = cluster.KMeans(n)
#yi = km.fit_predict(y)
#c = ['b', 'g', 'r', 'k']
set_yi = list(set(yi))
n = len(set_yi)
for i in set_yi:
sns.tsplot(y[yi==i], color=c[set_yi.index(i)])
plt.title(title)
def plot( self):
sns.tsplot(data=self.pdo, time="alpha", unit="unit", condition="Method", value="r2")
plt.xscale('log')
plt.ylabel( r'$r^2$')
def plot( self):
sns.tsplot(data=self.pdo, time="alpha", unit="unit", condition="Method", value="r2")
plt.xscale('log')
plt.ylabel( r'$r^2$')
def show_both_cell( self, c, cell_id):
X1part = self.X1part
X2part = self.X2part
y = self.y
cell = self.cell
X1_ylim = self.X1_ylim
X2_ylim = self.X2_ylim
cmethod = self.cmethod
X3_int = X2part[ np.where(y==c)[0],:]
X3_vel = X1part[ np.where(y==c)[0],:]
cell3 = cell[ np.where(y==c)[0]]
km = getattr(cluster, cmethod)(**cparam_d)
y3 = km.fit_predict( X3_int)
# redefine based on cell_id
X3_int = X3_int[ np.where(cell3==cell_id)[0],:]
X3_vel = X3_vel[ np.where(cell3==cell_id)[0],:]
y3 = y3[np.where(cell3==cell_id)[0]]
n_0 = X3_int[ np.where( y3==0)[0]].shape[0]
n_1 = X3_int[ np.where( y3==1)[0]].shape[0]
plt.figure(figsize=(9,4))
plt.subplot(1,2,1)
if n_0 > 0: sns.tsplot( X3_int[ np.where( y3==0)[0],:], color="blue")
if n_1 > 0: sns.tsplot( X3_int[ np.where( y3==1)[0],:], color="green")
plt.ylim(X2_ylim)
plt.title("Cluster{0}:Intensity {1}:{2}".format(c, n_0, n_1))
#plt.show()
plt.subplot(1,2,2)
#print("Velocity")
if n_0 > 0: sns.tsplot( X3_vel[ np.where( y3==0)[0],:], color="blue")
if n_1 > 0: sns.tsplot( X3_vel[ np.where( y3==1)[0],:], color="green")
plt.ylim(X1_ylim)
plt.title("Cluster{0}:Velocity {1}:{2}".format(c, n_0, n_1))
plt.show()
def np_tsplot( N):
sns.tsplot( get_ts_df( N), time='time', unit='unit', value='value')
def tsplot_clusters( X, y):
"""
X, 2d array with y, cluster index
"""
for yit in list(set(y)):
sns.tsplot( X[y==yit,:], color=plt.cm.rainbow(yit/max(y)))
def tsplot(df, add_plot=None, figsize=None, xlim=None, ylim=None, xlabel=None, ylabel=None,
label_size=None, tick_size=None, title=None, title_size=None, err=0, **kwargs):
"""
:param df:
:param figsize:
:param xlim:
:param ylim:
:param xlabel:
:param ylabel:
:param label_size:
:param tick_size:
:param title:
:param title_size:
:param err: 0 = standard deviation, 1 = standard error
:param kwargs:
:return:
"""
if not add_plot:
fig, axes = plt.subplots(1,1,figsize=figsize)
else:
fig, axes = add_plot
fig.patch.set_facecolor('white')
axes.spines['top'].set_visible(False)
axes.spines['right'].set_visible(False)
if xlim:
axes.set_xlim(xlim)
if ylim:
axes.set_ylim(ylim)
if title:
axes.set_title(title, size=title_size)
if xlabel:
axes.set_xlabel(xlabel, size=label_size)
else:
axes.set_xlabel('Time (s)', size=label_size)
if ylabel:
axes.set_ylabel(ylabel, size=label_size)
else:
axes.set_ylabel('Responses', size=label_size)
axes.tick_params(labelsize=tick_size, direction='out', top='off', right='off')
if err:
sns.tsplot(df.T.values, err_style='sterr_band', ax=axes, **kwargs)
else:
sns.tsplot(df.T.values, err_style='std_band', ax=axes, **kwargs)
return fig, axes
def plotForcingSubplots(tsdata, filename=None, ci=95, show_figure=False, save_fig_kwargs=None):
sns.set_context('paper')
expList = tsdata['expName'].unique()
nrows = 1
ncols = len(expList)
width = 2 * ncols
height = 2
fig, axes = plt.subplots(nrows=nrows, ncols=ncols, sharey=True, figsize=(width, height))
def dataForExp(expName):
df = tsdata.query("expName == '%s'" % expName).copy()
df.drop(['expName'], axis=1, inplace=True)
df = pd.melt(df, id_vars=['runId'], var_name='year')
return df
for ax, expName in zip(axes, expList):
df = dataForExp(expName)
pos = expName.find('-')
title = expName[:pos] if pos >= 0 else expName
ax.set_title(title.capitalize())
tsm.tsplot(df, time='year', unit='runId', value='value', ci=ci, ax=ax)
ylabel = 'W m$^{-2}$' if ax == axes[0] else ''
ax.set_ylabel(ylabel)
ax.set_xlabel('') # no need to say "year"
ax.axhline(0, color='navy', linewidth=0.5, linestyle='-')
plt.setp(ax.get_xticklabels(), rotation=270)
plt.tight_layout()
# Save the file
if filename:
if isinstance(save_fig_kwargs, dict):
fig.savefig(filename, **save_fig_kwargs)
else:
fig.savefig(filename)
# Display the figure
if show_figure:
plt.show()
return fig
def show_both( self, c):
X1part = self.X1part
X2part = self.X2part
y = self.y
cell = self.cell
X1_ylim = self.X1_ylim
X2_ylim = self.X2_ylim
cmethod = self.cmethod
cparam_d = self.cparam_d
#print("Cluster:", c)
X3_int = X2part[ np.where(y==c)[0],:]
X3_vel = X1part[ np.where(y==c)[0],:]
#km = cluster.KMeans(2)
#km = getattr(cluster, cmethod)(2)
km = getattr(cluster, cmethod)(**cparam_d)
y3 = km.fit_predict( X3_int)
plt.figure(figsize=(9,4))
plt.subplot(1,2,1)
#print("Intensity")
n_0 = X3_int[ np.where( y3==0)[0]].shape[0]
n_1 = X3_int[ np.where( y3==1)[0]].shape[0]
sns.tsplot( X3_int[ np.where( y3==0)[0],:], color="blue")
sns.tsplot( X3_int[ np.where( y3==1)[0],:], color="green")
plt.ylim(X2_ylim)
plt.title("Cluster{0}:X2 {1}:{2}".format(c, n_0, n_1))
#plt.show()
plt.subplot(1,2,2)
#print("Velocity")
sns.tsplot( X3_vel[ np.where( y3==0)[0],:], color="blue")
sns.tsplot( X3_vel[ np.where( y3==1)[0],:], color="green")
plt.ylim(X1_ylim)
plt.title("Cluster{0}:X1 {1}:{2}".format(c, n_0, n_1))
plt.show()
cell3 = cell[ np.where(y==c)[0]]
plt.subplot(1,2,1)
plt.stem( cell3[np.where( y3==0)[0]], linefmt='b-', markerfmt='bo')
plt.title("Cell Index - Subcluster 1")
plt.subplot(1,2,2)
plt.stem( cell3[np.where( y3==1)[0]], linefmt='g-', markerfmt='go')
plt.title("Cell Index - Subcluster 2")
plt.show()
return y3
def show_both( self, c):
X1part = self.X1part
X2part = self.X2part
y = self.y
cell = self.cell
X1_ylim = self.X1_ylim
X2_ylim = self.X2_ylim
cmethod = self.cmethod
cparam_d = self.cparam_d
#print("Cluster:", c)
X3_int = X2part[ np.where(y==c)[0],:]
X3_vel = X1part[ np.where(y==c)[0],:]
#km = cluster.KMeans(2)
#km = getattr(cluster, cmethod)(2)
km = getattr(cluster, cmethod)(**cparam_d)
y3 = km.fit_predict( X3_int)
plt.figure(figsize=(9,4))
plt.subplot(1,2,1)
#print("Intensity")
n_0 = X3_int[ np.where( y3==0)[0]].shape[0]
n_1 = X3_int[ np.where( y3==1)[0]].shape[0]
sns.tsplot( X3_int[ np.where( y3==0)[0],:], color="blue")
sns.tsplot( X3_int[ np.where( y3==1)[0],:], color="green")
plt.ylim(X2_ylim)
plt.title("Cluster{0}:X2 {1}:{2}".format(c, n_0, n_1))
#plt.show()
plt.subplot(1,2,2)
#print("Velocity")
sns.tsplot( X3_vel[ np.where( y3==0)[0],:], color="blue")
sns.tsplot( X3_vel[ np.where( y3==1)[0],:], color="green")
plt.ylim(X1_ylim)
plt.title("Cluster{0}:X1 {1}:{2}".format(c, n_0, n_1))
plt.show()
cell3 = cell[ np.where(y==c)[0]]
plt.subplot(1,2,1)
plt.stem( cell3[np.where( y3==0)[0]], linefmt='b-', markerfmt='bo')
plt.title("Cell Index - Subcluster 1")
plt.subplot(1,2,2)
plt.stem( cell3[np.where( y3==1)[0]], linefmt='g-', markerfmt='go')
plt.title("Cell Index - Subcluster 2")
plt.show()
return y3
def show_both_kmeans( self, c):
X1part = self.X1part
X2part = self.X2part
y = self.y
cell = self.cell
X1_ylim = self.X1_ylim
X2_ylim = self.X2_ylim
cmethod = self.cmethod
cparam_d = self.cparam_d
nc = cparam_d["n_clusters"]
#print("Cluster:", c)
X3_int = X2part[ y==c,:]
X3_vel = X1part[ y==c,:]
#km = cluster.KMeans(2)
#km = getattr(cluster, cmethod)(2)
assert cmethod == "KMeans"
km = cluster.KMeans( nc)
y3 = km.fit_predict( X3_int)
plt.figure(figsize=(9,4))
plt.subplot(1,2,1)
#print("Intensity")
n_l = [ X3_int[ y3==i].shape[0] for i in range(nc)]
for i in range(nc):
sns.tsplot( X3_int[ y3==i,:], color=plt.cm.rainbow(i/nc))
plt.ylim(X2_ylim)
plt.title("Cluster{0}:X2 {1}".format(c, n_l))
#plt.show()
plt.subplot(1,2,2)
#print("Velocity")
for i in range(nc):
sns.tsplot( X3_vel[ y3==i,:], color=plt.cm.rainbow(i/nc))
plt.ylim(X1_ylim)
plt.title("Cluster{0}:X1 {1}".format(c, n_l))
plt.show()
return y3
def plotScalabilityResultsForTest(dfs, test, colName, title, xlabel, savePath,
ax=None, logxscale=False, logyscale=True):
dfs = filter(lambda df: df['test'][0] == test, dfs)
if ax is None:
plt.figure()
ax = plt.gca()
lines = []
labels = []
for df in dfs:
x = df[colName]
y = df[TIME_COL] / 60. # convert to minutes
algo = df[ALGORITHM_COL][0]
p = ALGO_2_LINE_PARAMS[algo]
line, = ax.plot(x, y, label=algo, color=p.color, lw=p.width, ls=p.style)
ax.set_xlim([np.min(x), np.max(x)])
lines.append(line)
labels.append(algo)
# seaborn tsplot version; confidence intervals like invisibly thin
# around the lines, so not much point (although verify this once
# all experiments run TODO)
# df = pd.concat(dfs, ignore_index=True)
# colors = dict([algo, p.color] for algo, p in ALGO_2_LINE_PARAMS.items()])
# sb.tsplot(time=colName, value=TIME_COL, unit=SEED_COL,
# condition=ALGORITHM_COL, data=df, ax=ax)
# lines, labels = ax.get_legend_handles_labels()
if logxscale:
ax.set_xscale('log')
if logyscale:
ax.set_yscale('log')
# x = df[colName]
# ax.set_xlim([np.min(x), np.max(x)])
ax.set_title(title)
ax.set_xlabel(xlabel)
ax.set_ylabel("Runtime (min)")
if savePath:
plt.savefig(savePath)
return lines, labels