def plot_response(data, plate_name, save_folder = 'Figures/'): """ """ if not os.path.isdir(save_folder): os.makedirs(save_folder) for block in data: # group = group_similar(data[block].keys()) names = data[block].keys() names.sort() # plt.figure(figsize=(16, 4 + len(names)/8), dpi=300) # for i, name in enumerate(names): a, b, c = get_index(group, name) color, pattern = color_shade_pattern(a, b, c, group) mean = data[block][name]['mean'][0] std = data[block][name]['std'][0] plt.barh([i], [mean], height=1.0, color=color, hatch=pattern) plt.errorbar([mean], [i+0.5], xerr=[std], ecolor = [0,0,0], linestyle = '') plt.yticks([i+0.5 for i in xrange(len(names))], names, size = 8) plt.title(plate_name) plt.ylim(0, len(names)) plt.xlabel('change') plt.tight_layout() plt.savefig(save_folder + 'response_' + str(block + 1)) # return None
def _plot(self): (binsX, binsY) = (self.bins, self.bins) if isinstance(self.bins, int) else self.bins X, Y = self.data H, ex, ey = numpy.histogram2d(X, Y, bins=(binsX, binsY)) x_center = numpy.diff(ex) / 2 + ex[0:-1] x_digit = numpy.digitize(X, ex) y_center = numpy.empty(binsY) y_std = numpy.empty(binsY) for i in range(binsX): y_pop = Y[numpy.where(x_digit == i + 1)[0]] y_center[i] = numpy.mean(y_pop) y_std[i] = numpy.std(y_pop) plt.errorbar(x_center, y_center, y_std) plt.xlim(ex[0], ex[-1])
def _plot(self): for name, val in self.errors.items(): x, y, yerr, xerr = val yerr_mod = yerr if self.log: y_mod = numpy.log(y) if yerr is not None: yerr_mod = numpy.log(y + yerr) - y_mod else: y_mod = y err_bar = plt.errorbar(x, y_mod, yerr=yerr_mod, xerr=xerr, label=name, fmt='o', ms=self.size) err_bar[0].set_label('_nolegend_')
def mkplots(self): # run to make plots of the resulting posteriors. Modified from marginal_plots.py # from pymultinest. Produces basename+marg.pdf and basename+marge.png files prefix = self.basename parameters = json.load(file(prefix + 'params.json')) n_params = len(parameters) a = pymultinest.Analyzer(n_params=n_params, outputfiles_basename=prefix) s = a.get_stats() p = pymultinest.PlotMarginal(a) try: values = a.get_equal_weighted_posterior() except IOError as e: print 'Unable to open: %s' % e return assert n_params == len(s['marginals']) modes = s['modes'] dim2 = os.environ.get('D', '1' if n_params > 20 else '2') == '2' nbins = 100 if n_params < 3 else 20 if dim2: plt.figure(figsize=(5.1 * n_params, 5 * n_params)) for i in range(n_params): plt.subplot(n_params, n_params, i + 1) plt.xlabel(parameters[i]) m = s['marginals'][i] plt.xlim(m['5sigma']) oldax = plt.gca() x, w, patches = oldax.hist(values[:, i], bins=nbins, edgecolor='grey', color='grey', histtype='stepfilled', alpha=0.2) oldax.set_ylim(0, x.max()) newax = plt.gcf().add_axes(oldax.get_position(), sharex=oldax, frameon=False) p.plot_marginal(i, ls='-', color='blue', linewidth=3) newax.set_ylim(0, 1) ylim = newax.get_ylim() y = ylim[0] + 0.05 * (ylim[1] - ylim[0]) center = m['median'] low1, high1 = m['1sigma'] print center, low1, high1 newax.errorbar(x=center, y=y, xerr=np.transpose( [[center - low1, high1 - center]]), color='blue', linewidth=2, marker='s') oldax.set_yticks([]) #newax.set_yticks([]) newax.set_ylabel("Probability") ylim = oldax.get_ylim() newax.set_xlim(m['5sigma']) oldax.set_xlim(m['5sigma']) #plt.close() for j in range(i): plt.subplot(n_params, n_params, n_params * (j + 1) + i + 1) p.plot_conditional(i, j, bins=20, cmap=plt.cm.gray_r) for m in modes: plt.errorbar(x=m['mean'][i], y=m['mean'][j], xerr=m['sigma'][i], yerr=m['sigma'][j]) ax = plt.gca() if j == i - 1: plt.xlabel(parameters[i]) plt.ylabel(parameters[j]) [l.set_rotation(45) for l in ax.get_xticklabels()] else: ax.set_xticklabels([]) ax.set_yticklabels([]) plt.xlim([ m['mean'][i] - 5 * m['sigma'][i], m['mean'][i] + 5 * m['sigma'][i] ]) plt.ylim([ m['mean'][j] - 5 * m['sigma'][j], m['mean'][j] + 5 * m['sigma'][j] ]) #plt.savefig('cond_%s_%s.pdf' % (params[i], params[j]), bbox_tight=True) #plt.close() plt.tight_layout() plt.savefig(prefix + 'marg.pdf') plt.savefig(prefix + 'marg.png') plt.close() else: from matplotlib.backends.backend_pdf import PdfPages print '1dimensional only. Set the D environment variable D=2 to force' print '2d marginal plots.' pp = PdfPages(prefix + 'marg1d.pdf') for i in range(n_params): plt.figure(figsize=(5, 5)) plt.xlabel(parameters[i]) m = s['marginals'][i] plt.xlim(m['5sigma']) oldax = plt.gca() x, w, patches = oldax.hist(values[:, i], bins=20, edgecolor='grey', color='grey', histtype='stepfilled', alpha=0.2) oldax.set_ylim(0, x.max()) newax = plt.gcf().add_axes(oldax.get_position(), sharex=oldax, frameon=False) p.plot_marginal(i, ls='-', color='blue', linewidth=3) newax.set_ylim(0, 1) ylim = newax.get_ylim() y = ylim[0] + 0.05 * (ylim[1] - ylim[0]) center = m['median'] low1, high1 = m['1sigma'] print center, low1, high1 newax.errorbar(x=center, y=y, xerr=np.transpose( [[center - low1, high1 - center]]), color='blue', linewidth=2, marker='s') oldax.set_yticks([]) newax.set_ylabel("Probability") ylim = oldax.get_ylim() newax.set_xlim(m['5sigma']) oldax.set_xlim(m['5sigma']) plt.savefig(pp, format='pdf', bbox_inches='tight') plt.close() pp.close()
N = np.load('./results/Node_contrib_' + mode + '_' + direction + '_70128.npy') error = abs(S - N) / N * 100 weightedError = abs(S - N) / N * quantiles / np.mean(quantiles) * 100 means = np.mean(error, axis=1) weightedMeans = np.mean(weightedError, axis=1) stds = np.std(error, axis=1) nodeMean = np.mean(means) weightedNodeMean = np.mean(weightedMeans) x = np.linspace(.5, 29.5, 30) if mode == 'linear': title = 'localised' if mode == 'square': title = 'synchronised' plt.figure() ax = plt.subplot() plt.errorbar(x, means[loadOrder], yerr=stds * 0, marker='s', lw=0, elinewidth=1) plt.plot([0, 30], [nodeMean, nodeMean], '--k', lw=2) plt.title(title + ' ' + direction + ', sum of colors vs. total network usage') plt.ylabel('Mean link deviation in %') ax.set_xticks(np.linspace(1, 30, 30)) ax.set_xticklabels(loadNames, rotation=60, ha="right", va="top", fontsize=9) plt.axis([0, 30, min(means) - (.1 * min(means)), max(means) + (.1 * max(means))]) plt.legend(('individual country', 'mean of countries'), loc=2, ncol=2) plt.savefig(figPath + 'error/' + title + '_' + direction + '.pdf', bbox_inches='tight') plt.figure() ax = plt.subplot() plt.errorbar(x, weightedMeans[loadOrder], yerr=stds * 0, marker='s', lw=0, elinewidth=1) plt.plot([0, 30], [weightedNodeMean, weightedNodeMean], '--k', lw=2) plt.title(title + ' ' + direction + ', sum of colors vs. total network usage') plt.ylabel(r'Weighed mean link deviation in % normalised to $\left\langle \mathcal{K}^T \right\rangle$')
def mkplots(self): # run to make plots of the resulting posteriors. Modified from marginal_plots.py # from pymultinest. Produces basename+marg.pdf and basename+marge.png files prefix = self.basename parameters = json.load(file(prefix + 'params.json')) n_params = len(parameters) a = pymultinest.Analyzer(n_params = n_params, outputfiles_basename = prefix) s = a.get_stats() p = pymultinest.PlotMarginal(a) try: values = a.get_equal_weighted_posterior() except IOError as e: print 'Unable to open: %s' % e return assert n_params == len(s['marginals']) modes = s['modes'] dim2 = os.environ.get('D', '1' if n_params > 20 else '2') == '2' nbins = 100 if n_params < 3 else 20 if dim2: plt.figure(figsize=(5.1*n_params, 5*n_params)) for i in range(n_params): plt.subplot(n_params, n_params, i + 1) plt.xlabel(parameters[i]) m = s['marginals'][i] plt.xlim(m['5sigma']) oldax = plt.gca() x,w,patches = oldax.hist(values[:,i], bins=nbins, edgecolor='grey', color='grey', histtype='stepfilled', alpha=0.2) oldax.set_ylim(0, x.max()) newax = plt.gcf().add_axes(oldax.get_position(), sharex=oldax, frameon=False) p.plot_marginal(i, ls='-', color='blue', linewidth=3) newax.set_ylim(0, 1) ylim = newax.get_ylim() y = ylim[0] + 0.05*(ylim[1] - ylim[0]) center = m['median'] low1, high1 = m['1sigma'] print center, low1, high1 newax.errorbar(x=center, y=y, xerr=np.transpose([[center - low1, high1 - center]]), color='blue', linewidth=2, marker='s') oldax.set_yticks([]) #newax.set_yticks([]) newax.set_ylabel("Probability") ylim = oldax.get_ylim() newax.set_xlim(m['5sigma']) oldax.set_xlim(m['5sigma']) #plt.close() for j in range(i): plt.subplot(n_params, n_params, n_params * (j + 1) + i + 1) p.plot_conditional(i, j, bins=20, cmap = plt.cm.gray_r) for m in modes: plt.errorbar(x=m['mean'][i], y=m['mean'][j], xerr=m['sigma'][i], yerr=m['sigma'][j]) ax = plt.gca() if j == i-1: plt.xlabel(parameters[i]) plt.ylabel(parameters[j]) [l.set_rotation(45) for l in ax.get_xticklabels()] else: ax.set_xticklabels([]) ax.set_yticklabels([]) plt.xlim([m['mean'][i]-5*m['sigma'][i],m['mean'][i]+5*m['sigma'][i]]) plt.ylim([m['mean'][j]-5*m['sigma'][j],m['mean'][j]+5*m['sigma'][j]]) #plt.savefig('cond_%s_%s.pdf' % (params[i], params[j]), bbox_tight=True) #plt.close() plt.tight_layout() plt.savefig(prefix + 'marg.pdf') plt.savefig(prefix + 'marg.png') plt.close() else: from matplotlib.backends.backend_pdf import PdfPages print '1dimensional only. Set the D environment variable D=2 to force' print '2d marginal plots.' pp = PdfPages(prefix + 'marg1d.pdf') for i in range(n_params): plt.figure(figsize=(5, 5)) plt.xlabel(parameters[i]) m = s['marginals'][i] plt.xlim(m['5sigma']) oldax = plt.gca() x,w,patches = oldax.hist(values[:,i], bins=20, edgecolor='grey', color='grey', histtype='stepfilled', alpha=0.2) oldax.set_ylim(0, x.max()) newax = plt.gcf().add_axes(oldax.get_position(), sharex=oldax, frameon=False) p.plot_marginal(i, ls='-', color='blue', linewidth=3) newax.set_ylim(0, 1) ylim = newax.get_ylim() y = ylim[0] + 0.05*(ylim[1] - ylim[0]) center = m['median'] low1, high1 = m['1sigma'] print center, low1, high1 newax.errorbar(x=center, y=y, xerr=np.transpose([[center - low1, high1 - center]]), color='blue', linewidth=2, marker='s') oldax.set_yticks([]) newax.set_ylabel("Probability") ylim = oldax.get_ylim() newax.set_xlim(m['5sigma']) oldax.set_xlim(m['5sigma']) plt.savefig(pp, format='pdf', bbox_inches='tight') plt.close() pp.close()