def plot_per_row_legend_n_save(a, x, y, namefield, xlab, ylab, outdir, baseout=False, xlim=False, ylim=False, size_field=False, colors=False, shapes=False, names=False): """Plot two columns of a numpy array against each other. Give each point a different color and include a legend.""" # Default values for colors and shapes if not colors: colors = ['b', 'g', 'r', 'c', 'm', 'y', 'k', 'w'] if not shapes: shapes = 8 * ['.'] # arbitrary length # Create color dict if not names: names = np.unique(a[namefield]).tolist() assert len(names) <= len(colors) colord = dict(zip(names, colors[:len(names)])) # Create shape dict assert len(names) <= len(shapes) shaped = dict(zip(names, shapes[:len(names)])) # Plot with equal size unless size_field is given ax = plt.subplot(1, 1, 1) if not size_field: for i in range(len(a)): ax.plot(a[x][i], a[y][i], shaped[a[namefield][i]], color=colord[a[namefield][i]], label=a[namefield][i], markersize=20) else: for i in range(len(a)): ax.plot(a[x][i], a[y][i], shaped[a[namefield][i]], color=colord[a[namefield][i]], label=a[namefield][i], markersize=int(a[size_field][i] / float(max(a[size_field])) * 20)) # Set the legend handles, labels = ax.get_legend_handles_labels() labelsu = list(set(labels)) handlesu = [handles[labels.index(l)] for l in labelsu] ax.legend(handlesu, labelsu, numpoints=1, loc=0) # Set x and y limits if xlim: plt.xlim(xmin=xlim[0], xmax=xlim[1]) else: # X and Y are plotted on the direct ends increase plot size xmin, xmax = plt.xlim() xs = (xmax - xmin) / 10 assert xs > 0 plt.xlim(xmin - xs, xmax + xs) if ylim: plt.ylim(ymin=ylim[0], ymax=ylim[1]) # Set axes labels plt.xlabel(xlab) plt.ylabel(ylab) # Save if not baseout: baseout = "{x}_vs_{y}".format(x=x, y=y) savefig_multiple_ext(outdir + "/" + baseout) # Return png name of outputted file return baseout + ".png"
def main(outputbase, assemblies, names, colors, shapes, linewidths, cut_off=100, roc=False): # Change figure size plt.rcParams['figure.figsize'] = 8, 6 plt.rcParams['figure.dpi'] = 100 plt.rcParams['savefig.dpi'] = 100 if not roc: plot_length_cum(assemblies, names, colors, shapes, linewidths, cut_off) else: plot_length_roc_style(assemblies, names, colors, shapes, linewidths, cut_off) savefig_multiple_ext(outputbase) # Return png name of outputted file return outputbase + ".png"