in scripts/plot.py [0:0]
def plot_data(data, xaxis='Epoch', value="AverageEpRet",
condition="Condition1", smooth=1, paper=False,
hidelegend=False, title=None, savedir=None,
clear_xticks=False, **kwargs):
# special handling for plotting a horizontal line
splits = value.split(',')
value = splits[0]
#if len(splits) > 1:
# y_horiz = float(splits[1])
#else:
# y_horiz = None
y_horiz, ymin, ymax = None, None, None
if len(splits) > 1:
for split in splits[1:]:
if split[0]=='h':
y_horiz = float(split[1:])
elif split[0]=='l':
ymin = float(split[1:])
elif split[0]=='u':
ymax = float(split[1:])
if isinstance(data, list):
# Seive data so only data with value column is present
data = [x for x in data if value in x.columns]
if smooth > 1:
"""
smooth data with moving window average.
that is,
smoothed_y[t] = average(y[t-k], y[t-k+1], ..., y[t+k-1], y[t+k])
where the "smooth" param is width of that window (2k+1)
"""
y = np.ones(smooth)
for datum in data:
x = np.asarray(datum[value])
z = np.ones(len(x))
smoothed_x = np.convolve(x,y,'same') / np.convolve(z,y,'same')
datum[value] = smoothed_x
if isinstance(data, list):
data = pd.concat(data, ignore_index=True)
font_scale = 1. if paper else 1.5
sns.set(style="darkgrid", font_scale=font_scale)
"""
#sns.set_palette(sns.color_palette('muted'))
sns.set_palette([(0.12156862745098039, 0.4666666666666667, 0.7058823529411765),
#(1.0, 0.4980392156862745, 0.054901960784313725),
(0.17254901960784313, 0.6274509803921569, 0.17254901960784313),
(0.5803921568627451, 0.403921568627451, 0.7411764705882353),
(0.8392156862745098, 0.15294117647058825, 0.1568627450980392),
#(0.5490196078431373, 0.33725490196078434, 0.29411764705882354),
#(0.8901960784313725, 0.4666666666666667, 0.7607843137254902),
#(0.4980392156862745, 0.4980392156862745, 0.4980392156862745),
(0.7372549019607844, 0.7411764705882353, 0.13333333333333333),
(0.09019607843137255, 0.7450980392156863, 0.8117647058823529)]
)
#"""
sns.tsplot(data=data, time=xaxis, value=value, unit="Unit", condition=condition, ci='sd', **kwargs)
"""
If you upgrade to any version of Seaborn greater than 0.8.1, switch from
tsplot to lineplot replacing L29 with:
sns.lineplot(data=data, x=xaxis, y=value, hue=condition, ci='sd', **kwargs)
Changes the colorscheme and the default legend style, though.
"""
plt.legend(loc='best')#.draggable()
"""
For the version of the legend used in the Spinning Up benchmarking page,
swap L38 with:
plt.legend(loc='upper center', ncol=6, handlelength=1,
mode="expand", borderaxespad=0., prop={'size': 13})
"""
xmax = np.max(np.asarray(data[xaxis]))
xscale = xmax > 5e3
if xscale:
# Just some formatting niceness: x-axis scale in scientific notation if max x is large
plt.ticklabel_format(style='sci', axis='x', scilimits=(0,0))
old_ymin, old_ymax = plt.ylim()
if ymin:
plt.ylim(bottom=min(ymin, old_ymin))
if ymax:
plt.ylim(top=max(ymax, old_ymax))
#if title:
# plt.title(title)
if paper:
plt.gcf().set_size_inches(3.85,2.75)
plt.tight_layout(pad=0.5)
else:
plt.tight_layout(pad=0.5)
if y_horiz:
# y, xmin, xmax, colors='k', linestyles='solid', label='',
plt.hlines(y_horiz, 0, xmax, colors='red', linestyles='dashed', label='limit')
fname = osp.join(savedir, title+'_'+value).lower()
if clear_xticks:
x, _ = plt.xticks()
plt.xticks(x, [])
plt.xlabel('')
fname += '_nox'
if savedir is not '':
os.makedirs(savedir, exist_ok=True)
plt.savefig(fname+'.pdf', format='pdf')
if hidelegend:
plt.legend().remove()
if savedir is not '':
plt.savefig(fname + '_nolegend.pdf', format='pdf')
if savedir is not '':
# Separately save legend
h, l = plt.axes().get_legend_handles_labels()
legfig, legax = plt.subplots(figsize=(7.5,0.75))
legax.set_facecolor('white')
leg = legax.legend(h, l, loc='center', ncol=5, handlelength=1.5,
mode="expand", borderaxespad=0., prop={'size': 13})
legax.xaxis.set_visible(False)
legax.yaxis.set_visible(False)
for line in leg.get_lines():
line.set_linewidth(4.0)
plt.tight_layout(pad=0.5)
plt.savefig(osp.join(savedir, title+'_legend.pdf'), format='pdf')