def draw_place(target): fig = plt.figure() ax = fig.add_subplot(111) dims = target.bounds polys = poly_to_coords(target) for p in polys: patch = pgn(p[0], edgecolor='black', facecolor='white', alpha=1, zorder=0) ax.add_patch(patch) #draw interior for ip in p[1]: patch = pgn(ip, edgecolor='black', facecolor='white', alpha=1, zorder=1) ax.add_patch(patch) ax.set_xlim(dims[0], dims[2]) ax.set_ylim(dims[1], dims[3]) plt.axis("off") plt.show() plt.close()
if len(poly) > 2: ##finish off last polygon poly_list_group.append(poly) pl = Polygon(poly) poly_group.append(pl) print(len(edge_copy)) # should be 1; ##Plot the polygons fig, ax = plt.subplots() for pl in poly_group: pols, ipols = process_poly(pl) county = pgn(pols, fc='w', ec=(0, 0, 0, 1), lw=1, alpha=1, fill=None, zorder=100) ax.add_artist(county) ##drawing in the edges edge_map = [] for i in range(mi): tmp = [] for j in range(mj): if (i, j) in edges: tmp.append(1) else: tmp.append(0) edge_map.append(tmp) ##contour plot
def draw_map(infilename, outfigname, dims, target2, size=30, county=None, gadm=None, place='none', edited=False, nodealphafile=""): node_alphas = {} if len(nodealphafile) > 0: with open(nodealphafile, 'r') as infile: for line in infile: words = line.split() node_alphas[words[0]] = float(words[1]) nmax = node_alphas[max(node_alphas, key=node_alphas.get)] nmin = node_alphas[min(node_alphas, key=node_alphas.get)] for n in node_alphas: node_alphas[n] = (node_alphas[n] - nmin) / (nmax - nmin) best_partition = read_partition(infilename, edited) vmax = float(len(set(best_partition["data"].values()))) vmin = 0 partition_alpha = {"data": 1.0, "extrap": 0.75, "swap": 0.75} partition_edge = {"data": "none", "extrap": 'k', "swap": 'k'} partition_z = {"data": 0, "extrap": 5, "swap": 5} ############ ##plotting## ############ fig = plt.figure() ax = fig.add_subplot(111) setup_figure(ax, dims, target2, zorder=1, place=place, gadm=gadm) plt.axis("off") ############## ##rectangles## ############## cols = [ 'r', 'g', 'b', 'yellow', 'm', 'c', 'lightpink', 'saddlebrown', 'orange', 'grey', 'k' ] #, 'olive', 'moccasin', 'chartreuse', 'violet', 'chocolate', 'teal', ] partition_sizes = Counter() for node in best_partition["data"]: partition_sizes[best_partition["data"][node]] += 1 colour_map = {} for i, comm in enumerate(partition_sizes.most_common()): if False: #comm[1] < 20: colour_map[comm[0]] = 'k' else: if i < len(cols): colour_map[comm[0]] = cols[i] else: colour_map[comm[0]] = get_random_color(pastel_factor=1) print(partition_sizes.most_common()) print(colour_map) if not county: for box_id, box_number, mp in generate_land(dims, target2, size, contains=False): xmin, ymin, xmax, ymax = box_to_coords(mp) for k in best_partition: if str(box_id) in best_partition[k]: pn = best_partition[k][str(box_id)] col = colour_map[pn] al = partition_alpha[k] if len(node_alphas) > 0: al = node_alphas[str(box_id)] ##draw costal edges if target2.contains(mp): patch = Rectangle((xmin, ymin), (xmax - xmin), (ymax - ymin), edgecolor=partition_edge[k], facecolor=col, alpha=al, zorder=0 + partition_z[k]) ax.add_patch(patch) else: poly = target2.intersection(mp) polys = poly_to_coords(poly) for p in polys: patch = pgn(p[0], edgecolor=partition_edge[k], facecolor=col, alpha=al, zorder=2 + partition_z[k]) ax.add_patch(patch) else: for k in best_partition: for place in best_partition[k]: pn = best_partition[k][place] if pn < len(cols): col = cols[pn] else: col = get_random_color() mp = MultiPolygon(county.lookup(place)) polys = poly_to_coords(mp) for p in polys: patch = pgn(p[0], edgecolor=partition_edge[k], facecolor=col, alpha=partition_alpha[k], zorder=2 + partition_z[k]) ax.add_patch(patch) #draw interior for ip in p[1]: patch = pgn(ip, edgecolor=partition_edge[k], facecolor='white', alpha=partition_alpha[k], zorder=1 + partition_z[k]) ax.add_patch(patch) plt.savefig(outfigname) plt.close()
for place in county.county_dict: mp = MultiPolygon(county.county_dict[place]) if mp.bounds[0] < min_lat: min_lat = mp.bounds[0] if mp.bounds[1] < min_lon: min_lon = mp.bounds[1] if mp.bounds[2] > max_lat: max_lat = mp.bounds[2] if mp.bounds[3] > max_lon: max_lon = mp.bounds[3] pols2 = poly_to_coords(mp) for p in pols2: patch = pgn(p[0], edgecolor='black', facecolor='white', alpha=1, zorder=0) ax.add_patch(patch) #draw interior for ip in p[1]: patch = pgn(ip, edgecolor='black', facecolor='white', alpha=1, zorder=10) ax.add_patch(patch) border = 0 ax.set_xlim(min_lat - border, max_lat + border)