def draw_bar_chart(dictionary, title, x_label, y_label): bar_chart = plt.bar(range(len(dictionary)), dictionary.values(), align='center', color=cm.OrRd(np.linspace(.4, .8, 10)), zorder=3) value_list = list(dictionary.values()) high = max(value_list) plt.ylim([0, high + 10]) plt.title(title) plt.ylabel(y_label) plt.xlabel(x_label) ax = plt.gca() ax.grid() ax.spines['top'].set_visible(False) ax.spines['right'].set_visible(False) # ax.spines['bottom'].set_visible(False) ax.spines['left'].set_visible(False) for tick in ax.xaxis.get_major_ticks() + ax.yaxis.get_major_ticks(): tick.label.set_fontsize(8) plt.tick_params(top=False, bottom=False, left=False, right=False, labelleft=True, labelbottom=True) plt.xticks(range(len(dictionary)), list(dictionary.keys()), rotation=90) plt.tight_layout() # plt.subplots_adjust(left=0.1, right=0.95, top=0.9, bottom=0.25) auto_label(bar_chart) plt.savefig(ml_utils.get_dir_path() + title + '.png') plt.clf()
def wscmap(): # This function returns the colormap and bins for the PM spatial plots # this is designed to have a vmin =0 and vmax = 140 # return cmap,bins colors1 = cm.viridis(linspace(0, 1, 128)) colors2 = cm.OrRd(linspace(.2, 1, 128)) colors = vstack((colors1, colors2)) return mcolors.LinearSegmentedColormap.from_list('wscmap', colors), arange( 0, 40.2, .2)
def graph_color(data, maximum, svg_file): tree = ET.parse(svg_file) root = tree.getroot() gselect = r'{http://www.w3.org/2000/svg}g' paths = [] for p in root.iter(gselect): paths.append(p) for path in paths: try: scale = data[path.attrib['id']] / maximum s = clrs.rgb2hex(cm.OrRd(scale)) path.attrib['style'] = 'fill:' + s except KeyError: continue tree.write('out1.svg')
def get_cmaps_biasCNN(): """ Create color maps """ nsteps = 8 colors_all_1 = np.moveaxis( np.expand_dims(cm.Greys(np.linspace(0, 1, nsteps)), axis=2), [0, 1, 2], [0, 2, 1]) colors_all_2 = np.moveaxis( np.expand_dims(cm.GnBu(np.linspace(0, 1, nsteps)), axis=2), [0, 1, 2], [0, 2, 1]) colors_all_3 = np.moveaxis( np.expand_dims(cm.YlGn(np.linspace(0, 1, nsteps)), axis=2), [0, 1, 2], [0, 2, 1]) colors_all_4 = np.moveaxis( np.expand_dims(cm.OrRd(np.linspace(0, 1, nsteps)), axis=2), [0, 1, 2], [0, 2, 1]) colors_all = np.concatenate((colors_all_1[np.arange(2, nsteps, 1), :, :], colors_all_2[np.arange(2, nsteps, 1), :, :], colors_all_3[np.arange(2, nsteps, 1), :, :], colors_all_4[np.arange(2, nsteps, 1), :, :], colors_all_2[np.arange(2, nsteps, 1), :, :]), axis=1) int_inds = [3, 3, 3, 3] colors_main = np.asarray( [colors_all[int_inds[ii], ii, :] for ii in range(np.size(int_inds))]) colors_main = np.concatenate((colors_main, colors_all[5, 1:2, :]), axis=0) # plot the color map #plt.figure();plt.imshow(np.expand_dims(colors_main,axis=0)) colors_sf = np.moveaxis( np.expand_dims(cm.GnBu(np.linspace(0, 1, 8)), axis=2), [0, 1, 2], [0, 2, 1]) colors_sf = colors_sf[np.arange(2, 8, 1), :, :] return colors_main, colors_sf
def draw_hoods(boroughs, dataset): if dataset != "pickups" and dataset != "drops": raise Exception( "Error: the two datasets are 'pickups' and " "'drops', not ", str(dataset)) datafile = 'neighborhoods/nyc-pediacities-neighborhoods-v3-polygon.geojson' hoodlist = parse_neighborhood_file(datafile) xlim = len(boroughs) == 5 f1 = 15 if len(boroughs) == 5 else 10 fig = pyplot.figure(1, figsize=(f1, 10)) myplot = fig.add_subplot(111) if xlim: myplot.set_xlim(-74.35, -73.6) myplot.set_ylim(40.45, 40.95) hood_count = load_precomputed_count(boroughs) maxmin = load_precomputed_maxmin(boroughs) norm = colors.LogNorm(maxmin["min"][dataset]["count"], maxmin["max"][dataset]["count"]) for n in hoodlist.keys(): # keep it to requested boroughs if hoodlist[n]["boroughCode"] not in boroughs: continue count = hood_count[n][dataset] color = colors.rgb2hex(cm.OrRd(norm(count))) if hoodlist[n]["location"]["type"] == "MultiPolygon": for c in hoodlist[n]["location"]["coordinates"]: patch = PolygonPatch({ "type": "Polygon", "coordinates": c }, fc=color, ec='#000000', alpha=1.0, zorder=2) myplot.add_patch(patch) else: patch = PolygonPatch(hoodlist[n]["location"], fc=color, ec="#000000", alpha=1.0, zorder=2) myplot.add_patch(patch) if not xlim: myplot.relim() myplot.autoscale_view() pyplot.xlabel("longitude") pyplot.ylabel("latitude") filename = ''.join(str(b) for b in boroughs) pyplot.savefig(filename + dataset + '.png', bbox_inches='tight', dpi=100)
def draw_hoods3D(boroughs, dataset): if dataset != "pickups" and dataset != "drops": raise Exception( "Error: the two datasets are 'pickups' " "and 'drops', not ", str(dataset)) datafile = 'neighborhoods/nyc-pediacities-neighborhoods-v3-polygon.geojson' hoodlist = parse_neighborhood_file(datafile) manhattan = len(boroughs) == 1 and boroughs[0] == 1 fig = pyplot.figure(1, figsize=(7, 6)) ax = fig.gca(projection='3d') ax.set_zlabel("# of rides") ax.set_xlabel("longitude") ax.set_ylabel("latitude") if manhattan: ax.set_xlim3d(-74.07, -73.85) ax.set_ylim3d(40.65, 40.90) else: ax.set_xlim3d(-74.35, -73.6) ax.set_ylim3d(40.45, 40.95) hood_count = load_precomputed_count(boroughs) maxmin = load_precomputed_maxmin(boroughs) norm = colors.LogNorm(maxmin["min"][dataset]["count"], maxmin["max"][dataset]["count"]) for n in hoodlist.keys(): # keep it to requested boroughs if hoodlist[n]["boroughCode"] not in boroughs: continue count = hood_count[n][dataset] if norm(count) < 0: count = 0 rgba = cm.OrRd(norm(count)) color = colors.rgb2hex(rgba) tint = colors.rgb2hex( (rgba[0] * 0.9, rgba[1] * 0.9, rgba[2] * 0.9, rgba[3] * 0.9)) if hoodlist[n]["location"]["type"] == "MultiPolygon": for c in hoodlist[n]["location"]["coordinates"]: patch = PolygonPatch({ "type": "Polygon", "coordinates": c }, fc=color, ec=tint, alpha=1.0, zorder=2) ax.add_patch(patch) art3d.pathpatch_2d_to_3d(patch, z=norm(count), zdir='z') num_coords = len(c[0]) verts = [] for i in range(num_coords): P = c[0][i % num_coords] Q = c[0][(i + 1) % num_coords] x = [P[0], Q[0], Q[0], P[0]] y = [P[1], Q[1], Q[1], P[1]] z = [0, 0, norm(count), norm(count)] verts.append(zip(x, y, z)) coll = Poly3DCollection(verts) coll.set_facecolors(color) coll.set_edgecolors(tint) ax.add_collection3d(coll) else: patch = PolygonPatch(hoodlist[n]["location"], fc=color, ec=tint, alpha=1.0, zorder=2) ax.add_patch(patch) art3d.pathpatch_2d_to_3d(patch, z=norm(count), zdir='z') num_coords = len(hoodlist[n]["location"]["coordinates"][0]) verts = [] for i in range(num_coords): P = hoodlist[n]["location"]["coordinates"][0][i % num_coords] Q = hoodlist[n]["location"]["coordinates"][0][(i + 1) % num_coords] x = [P[0], Q[0], Q[0], P[0]] y = [P[1], Q[1], Q[1], P[1]] z = [0, 0, norm(count), norm(count)] verts.append(zip(x, y, z)) coll = Poly3DCollection(verts) coll.set_facecolors(color) coll.set_edgecolors(tint) ax.add_collection3d(coll) filename = ''.join(str(b) for b in boroughs) pyplot.savefig(filename + dataset + '.png', bbox_inches='tight', dpi=100)