def bokeh_nn( s_channels, testset, errors, title="no title", swap_xy=True, x_range=None, y_range=None, radius=3.0, alpha=0.75 ): x_range, y_range = ranges(s_channels, x_range=x_range, y_range=y_range) xv, yv = zip(*testset) if swap_xy: x_range, y_range = y_range, x_range xv, yv = yv, xv scale = [0, max(errors)] colorbar = ColorBar(scale, ["#0000FF", "#FF0000"], continuous=True) colors = [colorbar.color(e) for e in errors] plotting.scatter( xv, yv, title=title, x_range=x_range, y_range=y_range, fill_color=colors, fill_alpha=alpha, line_color=None, radius=radius, radius_units="screen", ) plotting.hold(True) plotting.grid().grid_line_color = "white" plotting.hold(False)
def bokeh_nn(s_channels, testset, errors, title='no title', swap_xy=True, x_range=None, y_range=None, radius=3.0, alpha=0.75): x_range, y_range = ranges(s_channels, x_range=x_range, y_range=y_range) xv, yv = zip(*testset) if swap_xy: x_range, y_range = y_range, x_range xv, yv = yv, xv scale = [0, max(errors)] colorbar = ColorBar(scale, ['#0000FF', '#FF0000'], continuous=True) colors = [colorbar.color(e) for e in errors] plotting.scatter(xv, yv, title=title, x_range=x_range, y_range=y_range, fill_color=colors, fill_alpha=alpha, line_color=None, radius=radius, radius_units="screen") plotting.hold(True) plotting.grid().grid_line_color = 'white' plotting.hold(False)
def plotScatter(data): data_to_plot = data.dropna() pl.output_file("scatter.html") pl.figure() pl.hold() pl.scatter(data["Fare"],data["Age"], color=data["Survived"].map(colormap),alpha=0.8, xlabel="Fare") pl.show()
def bokeh_plot(data, outfile): table = np.genfromtxt(data, names=['x', 'y']) blt.output_file(outfile) blt.hold() blt.figure(tools="pan,wheel_zoom,box_zoom,reset,previewsave,select") blt.scatter(table['x'], table['y']) blt.show()
def build_plot(datalist,logx=True): # Set the output for our plot. output_file('plot.html', title='Plot')#, js="relative", css="relative") # Create some data for our plot. colors=['red','green','blue','yellow','black'] # colors=['tomato','navy'] hold() cnt=0 for name,data in datalist: cnt+=1 x,y=zip(*data) if logx: scatter([np.log(x_el) for x_el in x] ,list(y) , color=colors[cnt], legend=name) else: scatter(list(x) ,list(y) , color=colors[cnt], legend=name) # Create an HTML snippet of our plot. snippet = curplot().create_html_snippet(embed_base_url='/static/plots/', embed_save_loc=plotdir) # Return the snippet we want to place in our page. return snippet
def example_hyperlink_plot(): idx_df = pds.read_csv("example_index.txt", sep="\t", names=["url", "id"]) arc_df = pds.read_csv("example_arcs.txt", sep="\t", names=["src", "dest"]) out_deg = arc_df.groupby(["src"]).size() out_deg.sort(ascending=False) in_deg = arc_df.groupby(["dest"]).size() in_deg.sort(ascending=False) color_map_max = out_deg.max() pallette = b2m.get_map("Dark2", "Qualitative", 8).hex_colors num_colors = len(pallette) - 1 color_map = lambda x: pallette[int(num_colors*(x/color_map_max))] color_vals = out_deg[in_deg.index].fillna(0) colors = color_vals.map(color_map) plt.output_file("in_deg.html", title="In degree rankings") plt.scatter(range(in_deg.size), in_deg, color=colors, fill_alpha=0.2, size=10, name="In Degree") plt.show()
def xy_scatter_bokeh(self): from bokeh.plotting import output_server, figure, scatter, show ux, uy, pos_mean, pos_sem, neg_mean, neg_sem, colors, size = \ self.xy_plot_info() output_server("scatter.html") figure(tools="pan,wheel_zoom,box_zoom,reset,previewsave,select") scatter(ux, uy, color="#FF00FF", nonselection_fill_color="#FFFF00", nonselection_fill_alpha=1) scatter(ux, uy, color="red") scatter(ux, uy, marker="square", color="green") scatter(ux, uy, marker="square", color="blue", name="scatter_example") show()
def plot_summary(normalized_counts, chromosome): #print " - plotting " + chromosome + " summary" condition = "(file_name == '*') & (chromosome == '%s')" % chromosome chromosome_count_by_bin = collections.defaultdict(int) for row in normalized_counts.where(condition): chromosome_count_by_bin[row["bin_number"]] += row["count"] num_bins = len(chromosome_count_by_bin) if num_bins < 2: print "-- skipping plotting %s because not enough bins (only %d)" % (chromosome, num_bins) return overall = bp.scatter(chromosome_count_by_bin.keys(), chromosome_count_by_bin.values()) overall.title = chromosome + " counts per bin across all bam files"
def plot_summary(normalized_counts, chromosome): logging.debug(" - plotting %s summary", chromosome) condition = "(file_key == 0) & (chromosome == '%s')" % chromosome chromosome_count_by_bin = collections.defaultdict(int) for row in normalized_counts.where(condition): chromosome_count_by_bin[row["bin_number"]] += row["count"] num_bins = len(chromosome_count_by_bin) if num_bins < 2: logging.info( "-- skipping plotting %s because not enough bins (only %d)", chromosome, num_bins) return overall = bp.scatter(chromosome_count_by_bin.keys(), chromosome_count_by_bin.values()) overall.title = chromosome + " counts per bin across all bam files"
def plot(output_directory, normalized_counts, chromosome, cell_types): bp.output_file(output_directory + "/" + chromosome + ".html") plot_summary(normalized_counts, chromosome) for cell_type in cell_types: #print " - plotting " + cell_type bin_number = [] count = [] condition = "(file_name == '*') & (chromosome == '%s') & (cell_type == '%s')" % (chromosome, cell_type) for row in normalized_counts.where(condition): bin_number.append(row["bin_number"]) count.append(row["count"]) cell_type_plot = bp.scatter(bin_number, count) cell_type_plot.title = "%s counts per bin" % cell_type bp.save()
def plot(output_directory, normalized_counts, chromosome, cell_types): bp.output_file(output_directory + "/" + chromosome + ".html") plot_summary(normalized_counts, chromosome) for cell_type in cell_types: logging.debug(" - plotting %s", cell_type) bin_number = [] count = [] condition = "(file_key == 0) & (chromosome == '%s') & (cell_type == '%s')" % ( chromosome, cell_type) for row in normalized_counts.where(condition): bin_number.append(row["bin_number"]) count.append(row["count"]) cell_type_plot = bp.scatter(bin_number, count) cell_type_plot.title = "%s counts per bin" % cell_type bp.save()
def plot_summary(chromosome, common_clause): print "Plotting " + chromosome + " summary" cursor = db.cursor() overall_sql = ("SELECT bin, SUM(count_fraction) AS count FROM normalized_bins WHERE " + common_clause + " GROUP BY bin") cursor.execute(overall_sql) # todo: I should probably be able to do something like the following without copying: # bp.scatter(cursor.fetchall()) bin_number = [] count = [] for row in cursor.fetchall(): bin_number.append(int(row[0])) count.append(float(row[1])) overall = bp.scatter(bin_number, count) overall.title = chromosome + " counts per bin across all bam files"
def create_scatter_plot(releases): number_of_bidders = [] delta_time = [] for release in releases: start = get_start_date(release) award = get_award_date(release) delta = get_difference_in_days(start, award) delta_time.append(delta) number_of_bidders.append(get_number_of_bidders(release)) scatter_plot = scatter(delta_time, number_of_bidders, fill_alpha=0.2, size=10, name="period between start of tender period and award date vs amount of bidders") scatter_plot.title = "period vs. bidders" xaxis()[0].axis_label = "Days" yaxis()[0].axis_label = "Number of Bidders" show()
def plot(chromosome, common_clause, cell_types): bp.output_file(chromosome + ".html") plot_summary(chromosome, common_clause) for cell_type in cell_types: print "Plotting " + cell_type bin_number = [] count = [] cell_type_bin_sql = ("SELECT bin, count_fraction FROM normalized_bins WHERE " + common_clause + "AND cell_type = '%s'" % cell_type) cell_type_cursor = db.cursor() cell_type_cursor.execute(cell_type_bin_sql) for cell_type_row in cell_type_cursor.fetchall(): bin_number.append(int(cell_type_row[0])) count.append(float(cell_type_row[1])) cell_type_plot = bp.scatter(bin_number, count) cell_type_plot.title = "%s counts per bin" % cell_type bp.save()
def spread( s_channels, s_vectors=(), s_goals=(), title="no title", swap_xy=True, x_range=None, y_range=None, e_radius=1.0, e_color=E_COLOR, e_alpha=0.75, g_radius=1.0, g_color=G_COLOR, g_alpha=0.75, grid=True, radius_units="screen", font_size="11pt", **kwargs ): x_range, y_range = ranges(s_channels, x_range=x_range, y_range=y_range) # effects try: xv, yv = zip(*(s[:2] for s in s_vectors)) except ValueError: xv, yv = [], [] if swap_xy: x_range, y_range = y_range, x_range xv, yv = yv, xv plotting.scatter( xv, yv, title=title, x_range=x_range, y_range=y_range, fill_color=e_color, fill_alpha=e_alpha, line_color=None, radius=e_radius, radius_units=radius_units, **kwargs ) # title_text_font_size=font_size, **kwargs) plotting.hold(True) # goals try: xg, yg = zip(*s_goals) except ValueError: xg, yg = [], [] if swap_xy: xg, yg = yg, xg plotting.scatter( xg, yg, radius=g_radius, radius_units="screen", fill_color=g_color, fill_alpha=g_alpha, line_color=None ) plotting_axis() if not grid: plotting.grid().grid_line_color = None plotting.hold(False)
def plot1(n=200): plotting.figure(title='arm1, {} steps'.format(n)) plotting.scatter(arm1_xs[:n], arm1_ys[:n], x_range=[-1, 1], y_range=[-1, 1], fill_alpha= 0.5, line_color=None, radius=2.0, radius_units='screen')
def drawPlot(shapeFilename, zipBorough, grids, centers, gridLines): # Read the ShapeFile dat = shapefile.Reader(shapeFilename) # Creates a dictionary for zip: {lat_list: [], lng_list: []}. zipCodes = [] polygons = {'lat_list': [], 'lng_list': [], 'centerLat_list': [], 'centerLon_list': []} record_index = 0 for r in dat.iterRecords(): currentZip = r[0] # Keeps only zip codes in NY area. if currentZip in zipBorough: # was in zipBorough: # Gets shape for this zip. shape = dat.shapeRecord(record_index).shape for part in range(len(shape.parts)): zipCodes.append(currentZip) start = shape.parts[part] if part == len(shape.parts) - 1: end = len(shape.points) else: end = shape.parts[part + 1] points = shape.points[start : end] # Breaks into lists for lat/lng. lngs = [p[0] for p in points] lats = [p[1] for p in points] # Calculate centers center_lngs = min(lngs) + (max(lngs) - min(lngs))/2 center_lats = min(lats) + (max(lats) - min(lats))/2 # Store centroids for current part shape polygons['centerLat_list'].append(center_lats) polygons['centerLon_list'].append(center_lngs) # Stores lat/lng for current zip shape. polygons['lng_list'].append(lngs) polygons['lat_list'].append(lats) record_index += 1 # Creates the Plot bk.output_file("problem3.html") bk.hold() north = 40.915 east = -73.651 south = 40.496 west = -74.256 width = east - west height = north - south x_range = [west - 0.05 * width, east + 0.05 * width] y_range = [south - 0.05 * height, north + 0.05 * height] TOOLS="pan,wheel_zoom,box_zoom,reset,previewsave" fig = bk.figure(title="311 Complaints by Zip Code", \ tools=TOOLS, background_fill = "#f8f8f8", x_range = x_range, y_range = y_range) circleSizes = [item for sublist in grids['log_counts'] for item in sublist] # Creates the polygons cellWidth = gridLines['vLines'][1]-gridLines['vLines'][0] cellHeight = gridLines['hLines'][1] - gridLines['hLines'][0] bk.patches(polygons['lng_list'], polygons['lat_list'], fill_color="#fee8c8", line_color="gray") circleSizes = [0.009 * (size ** 3.7) for size in circleSizes] bk.scatter(centers['lon'], centers['lat'], size = circleSizes, alpha = 0.4, line_color = None, fill_color = 'red') bk.hold() #print type(centers['lon']) circleSizeArr = np.array(circleSizes) maxSize = np.max(circleSizeArr) circleSizeArr[circleSizeArr == 0] = 100 minSize = np.min(circleSizeArr) #print minSize, maxSize legendN = 5 legendCircles = list(np.linspace(minSize, maxSize, legendN)) legendCounts = [str(int(math.exp(((size/0.009) ** (1.0/3.7))))) + " complaints" for size in legendCircles] #print legendCircles fakeScatter = [0 for x in range(legendN)] fakeScatterX = [-74.23 for x in range(legendN)] fakeScatterXtext = [-74.23 + 0.05 for x in range(legendN)] #fakeScatterY = [40.8 + 0.028*y for y in range(legendN)] fakeScatterY = [40.8, 40.8 + 0.02, 40.8 + 0.036, 40.8 + 0.056,40.8 + 0.083] #print fakeScatterX, type(fakeScatterX) #print fakeScatterY, type(fakeScatterY) bk.scatter(fakeScatterX, fakeScatterY, size = legendCircles, fill_color = 'red', line_color = None, alpha = 0.4) bk.text(fakeScatterXtext[1:], fakeScatterY[1:], text = legendCounts[1:], angle = 0, text_align = "left", text_font_size = "7pt", text_baseline = 'middle') #for i in range(len(fakeScatter)): # bk.scatter([fakeScatter[i]], [fakeScatter[i]], size = [legendCircles[i]], legend = int(legendCircles[i]), color = 'red', fill_line = None, alpha = 0.4) # Disable background grid bk.grid().grid_line_color = None #print centers ### Legend #x = -74.25 #y = 40.8 #height = 0.003 #legend_box_y = y + 0.5 * height * len(legendColors) #bk.rect([x+0.032], [legend_box_y], width = 0.12, height = height*len(legendColors)*1.15, color = "#ffffff", line_color = "gray") ##x = -74.25 ##y = 40.9 #for color in legendColors: ## #print "Color: ", a ## #print "x:", x ## #print "y:", y ## # bk.rect([x], [y], color = color, width=0.03, height=height) # #bk.text([x], [y], text = agency, angle=0, text_font_size="7pt", text_align="center", text_baseline="middle") # y = y + height #legend_bottom_y = 40.797 #legend_top_y = legend_bottom_y + 0.92 * height * len(legendColors) #bk.text([-74.225], [legend_bottom_y], text = agency2, angle = 0, text_font_size = "7pt", text_align = "left") #bk.text([-74.225], [legend_top_y], text = agency1, angle = 0, text_font_size = "7pt", text_align = "left") #bokeh.embed.components(fig, bokeh.resources.CDN) bk.show()
# Running the Target Exploration tgt_explorations = [] for i in range(N): exploration = ex.explore() feedback = env.execute(exploration['m_signal']) tgt_ex.receive(exploration, feedback) tgt_explorations.append((exploration, feedback)) tgt_dataset = {'m_channels' : ex.m_channels, 's_channels' : ex.s_channels, 'explorations': tgt_explorations} # Graphs try: from bokeh import plotting plotting.output_file('goal_reuse.html') xs = [explo[1]['s_signal']['x'] for explo in dataset['explorations']] ys = [explo[1]['s_signal']['y'] for explo in dataset['explorations']] plotting.scatter(xs, ys, x_range=[-1, 1], y_range=[-1, 1], title='explorers library: goal reuse algorithm example', fill_alpha= 0.5, line_color=None, radius=2.0, radius_units='screen') plotting.show() except ImportError: print('exploration went fine, but you need the bokeh library (http://bokeh.pydata.org/) to display it')
feedback = env.execute(exploration['m_signal']) ex.receive(exploration, feedback) explorations.append((exploration, feedback)) dataset = {'m_channels' : ex.m_channels, 's_channels' : ex.s_channels, 'explorations': explorations} # Graph # the red dots are from motor babbling # the blue dots from goal babbling try: from bokeh import plotting plotting.output_file('goal_babbling.html') xs = [explo[1]['s_signal']['x'] for explo in dataset['explorations']] ys = [explo[1]['s_signal']['y'] for explo in dataset['explorations']] plotting.scatter(xs, ys, x_range=[-1, 1], y_range=[-1, 1], title='explorers library: exploration example', fill_alpha= 0.5, line_color=None, radius=2.0, radius_units='screen') plotting.hold(True) plotting.scatter(xs[:ex_cfg.eras[0]], ys[:ex_cfg.eras[0]], x_range=[-1, 1], y_range=[-1, 1], title='explorers library: exploration example', fill_alpha= 0.5, color='red', line_color=None, radius=2.0, radius_units='screen') plotting.show() except ImportError: print('exploration went fine, but you need the bokeh library to display the it')
def spread(s_channels, s_vectors=(), s_goals=(), title='no title', swap_xy=True, x_range=None, y_range=None, e_radius=1.0, e_color=E_COLOR, e_alpha=0.75, g_radius=1.0, g_color=G_COLOR, g_alpha=0.75, grid=True, radius_units='screen', font_size='11pt', **kwargs): x_range, y_range = ranges(s_channels, x_range=x_range, y_range=y_range) # effects try: xv, yv = zip(*(s[:2] for s in s_vectors)) except ValueError: xv, yv = [], [] if swap_xy: x_range, y_range = y_range, x_range xv, yv = yv, xv plotting.scatter(xv, yv, title=title, x_range=x_range, y_range=y_range, fill_color=e_color, fill_alpha=e_alpha, line_color=None, radius=e_radius, radius_units=radius_units, **kwargs) # title_text_font_size=font_size, **kwargs) plotting.hold(True) # goals try: xg, yg = zip(*s_goals) except ValueError: xg, yg = [], [] if swap_xy: xg, yg = yg, xg plotting.scatter(xg, yg, radius=g_radius, radius_units="screen", fill_color=g_color, fill_alpha=g_alpha, line_color=None) plotting_axis() if not grid: plotting.grid().grid_line_color = None plotting.hold(False)
def cluster_vals(nodes, values): plt.output_file("in_deg.html", title="In degree rankings") plt.scatter(nodes, values, size=10, name="Cluster Values") plt.show()
def drawPlot(shapeFilename, zipBorough, zipCount): # Read the ShapeFile dat = shapefile.Reader(shapeFilename) # Creates a dictionary for zip: {lat_list: [], lng_list: []}. zipCodes = [] polygons = {'lat_list': [], 'lng_list': [], 'centerLat_list': [], 'centerLon_list': []} record_index = 0 for r in dat.iterRecords(): currentZip = r[0] # Keeps only zip codes in NY area. if currentZip in zipBorough: # was in zipBorough: # Gets shape for this zip. shape = dat.shapeRecord(record_index).shape ### Only first polygon if non-contiguous zip codes #firstPoint = shape.parts[0] #if len(shape.parts) > 1: # lastPoint = shape.parts[1] #else: # lastPoint = len(shape.points) #zipCodes.append(currentZip) #points = shape.points[firstPoint : lastPoint] # ## Breaks into lists for lat/lng. #lngs = [p[0] for p in points] #lats = [p[1] for p in points] ## Calculate centers #center_lngs = min(lngs) + (max(lngs) - min(lngs))/2 #center_lats = min(lats) + (max(lats) - min(lats))/2 ## Store centroids for current part shape #polygons['centerLat_list'].append(center_lats) #polygons['centerLon_list'].append(center_lngs) ## Stores lat/lng for current zip shape. #polygons['lng_list'].append(lngs) #polygons['lat_list'].append(lats) ### All shapes for each zip code for part in range(len(shape.parts)): zipCodes.append(currentZip) start = shape.parts[part] if part == len(shape.parts) - 1: end = len(shape.points) else: end = shape.parts[part + 1] points = shape.points[start : end] # Breaks into lists for lat/lng. lngs = [p[0] for p in points] lats = [p[1] for p in points] # Calculate centers center_lngs = min(lngs) + (max(lngs) - min(lngs))/2 center_lats = min(lats) + (max(lats) - min(lats))/2 # Store centroids for current part shape polygons['centerLat_list'].append(center_lats) polygons['centerLon_list'].append(center_lngs) # Stores lat/lng for current zip shape. polygons['lng_list'].append(lngs) polygons['lat_list'].append(lats) record_index += 1 # Creates the Plot bk.output_file("problem4.html") bk.hold() north = 40.915 east = -73.651 south = 40.496 west = -74.256 width = east - west height = north - south x_range = [west - 0.05 * width, east + 0.05 * width] y_range = [south - 0.05 * height, north + 0.05 * height] TOOLS="pan,wheel_zoom,box_zoom,reset,previewsave" fig = bk.figure(title="311 Complaints by Zip Code", \ tools=TOOLS, background_fill = "#f8f8f8", x_range = x_range, y_range = y_range, plot_height=800, plot_width = 1200) #circleSizes = [zipCount[zipCode] for zipCode in zipCodes] circleSizes = [] for zipCode in zipCodes: if zipCode in zipCount: circleSizes.append(zipCount[zipCode]) else: circleSizes.append(1) # for taking logs #print circleSizes #print "aaaaaaaaaaaaaaaaaaaaa: ", len(polygons['lng_list']), len(circleSizes) logCircleSizes = ((np.log(np.array(circleSizes)))**3.7) * 0.009 #print logCircleSizes logCircleSizes = list(logCircleSizes) #circleSizes = [x for x in range(len(zipCodes))] # Creates the polygons bk.patches(polygons['lng_list'], polygons['lat_list'], fill_color="#fee8c8", line_color="gray") bk.scatter(polygons['centerLon_list'], polygons['centerLat_list'], size = logCircleSizes, alpha = 0.4, line_color = None, fill_color = 'red') #circleSizes = [0.009 * (size ** 3.7) for size in circleSizes] #bk.scatter(centers['lon'], centers['lat'], size = circleSizes, alpha = 0.4, line_color = None, fill_color = 'red') #circleSizeArr = np.array(circleSizes) #maxSize = np.max(circleSizeArr) #circleSizeArr[circleSizeArr == 0] = 100 #minSize = np.min(circleSizeArr) #legendN = 5 #legendCircles = list(np.linspace(minSize, maxSize, legendN)) #legendCounts = [str(int((size ** (1.0/3.7))/0.009)) + " complaints" for size in legendCircles] #fakeScatter = [0 for x in range(legendN)] #fakeScatterX = [-74.23 for x in range(legendN)] #fakeScatterXtext = [-74.23 + 0.05 for x in range(legendN)] #fakeScatterY = [40.8, 40.8 + 0.02, 40.8 + 0.036, 40.8 + 0.056,40.8 + 0.083] #bk.scatter(fakeScatterX, fakeScatterY, size = legendCircles, fill_color = 'red', line_color = None, alpha = 0.4) #bk.text(fakeScatterXtext[1:], fakeScatterY[1:], text = legendCounts[1:], angle = 0, text_align = "left", text_font_size = "7pt", text_baseline = 'middle') # Disable background grid bk.grid().grid_line_color = None bk.show()
def run(): colormap = {'setosa': 'red', 'versicolor': 'green', 'virginica': 'blue'} flowers['color'] = flowers['species'].map(lambda x: colormap[x]) return scatter(flowers["petal_length"], flowers["petal_width"], color=flowers["color"], fill_alpha=0.2, size=10, name="iris")
def drawPlot(shapeFilename, zipBorough, grids, centers, gridLines): # Read the ShapeFile dat = shapefile.Reader(shapeFilename) # Creates a dictionary for zip: {lat_list: [], lng_list: []}. zipCodes = [] polygons = { 'lat_list': [], 'lng_list': [], 'centerLat_list': [], 'centerLon_list': [] } record_index = 0 for r in dat.iterRecords(): currentZip = r[0] # Keeps only zip codes in NY area. if currentZip in zipBorough: # was in zipBorough: # Gets shape for this zip. shape = dat.shapeRecord(record_index).shape for part in range(len(shape.parts)): zipCodes.append(currentZip) start = shape.parts[part] if part == len(shape.parts) - 1: end = len(shape.points) else: end = shape.parts[part + 1] points = shape.points[start:end] # Breaks into lists for lat/lng. lngs = [p[0] for p in points] lats = [p[1] for p in points] # Calculate centers center_lngs = min(lngs) + (max(lngs) - min(lngs)) / 2 center_lats = min(lats) + (max(lats) - min(lats)) / 2 # Store centroids for current part shape polygons['centerLat_list'].append(center_lats) polygons['centerLon_list'].append(center_lngs) # Stores lat/lng for current zip shape. polygons['lng_list'].append(lngs) polygons['lat_list'].append(lats) record_index += 1 # Creates the Plot bk.output_file("problem3.html") bk.hold() north = 40.915 east = -73.651 south = 40.496 west = -74.256 width = east - west height = north - south x_range = [west - 0.05 * width, east + 0.05 * width] y_range = [south - 0.05 * height, north + 0.05 * height] TOOLS = "pan,wheel_zoom,box_zoom,reset,previewsave" fig = bk.figure(title="311 Complaints by Zip Code", \ tools=TOOLS, background_fill = "#f8f8f8", x_range = x_range, y_range = y_range) circleSizes = [item for sublist in grids['log_counts'] for item in sublist] # Creates the polygons cellWidth = gridLines['vLines'][1] - gridLines['vLines'][0] cellHeight = gridLines['hLines'][1] - gridLines['hLines'][0] bk.patches(polygons['lng_list'], polygons['lat_list'], fill_color="#fee8c8", line_color="gray") circleSizes = [0.009 * (size**3.7) for size in circleSizes] bk.scatter(centers['lon'], centers['lat'], size=circleSizes, alpha=0.4, line_color=None, fill_color='red') bk.hold() #print type(centers['lon']) circleSizeArr = np.array(circleSizes) maxSize = np.max(circleSizeArr) circleSizeArr[circleSizeArr == 0] = 100 minSize = np.min(circleSizeArr) #print minSize, maxSize legendN = 5 legendCircles = list(np.linspace(minSize, maxSize, legendN)) legendCounts = [ str(int(math.exp(((size / 0.009)**(1.0 / 3.7))))) + " complaints" for size in legendCircles ] #print legendCircles fakeScatter = [0 for x in range(legendN)] fakeScatterX = [-74.23 for x in range(legendN)] fakeScatterXtext = [-74.23 + 0.05 for x in range(legendN)] #fakeScatterY = [40.8 + 0.028*y for y in range(legendN)] fakeScatterY = [ 40.8, 40.8 + 0.02, 40.8 + 0.036, 40.8 + 0.056, 40.8 + 0.083 ] #print fakeScatterX, type(fakeScatterX) #print fakeScatterY, type(fakeScatterY) bk.scatter(fakeScatterX, fakeScatterY, size=legendCircles, fill_color='red', line_color=None, alpha=0.4) bk.text(fakeScatterXtext[1:], fakeScatterY[1:], text=legendCounts[1:], angle=0, text_align="left", text_font_size="7pt", text_baseline='middle') #for i in range(len(fakeScatter)): # bk.scatter([fakeScatter[i]], [fakeScatter[i]], size = [legendCircles[i]], legend = int(legendCircles[i]), color = 'red', fill_line = None, alpha = 0.4) # Disable background grid bk.grid().grid_line_color = None #print centers ### Legend #x = -74.25 #y = 40.8 #height = 0.003 #legend_box_y = y + 0.5 * height * len(legendColors) #bk.rect([x+0.032], [legend_box_y], width = 0.12, height = height*len(legendColors)*1.15, color = "#ffffff", line_color = "gray") ##x = -74.25 ##y = 40.9 #for color in legendColors: ## #print "Color: ", a ## #print "x:", x ## #print "y:", y ## # bk.rect([x], [y], color = color, width=0.03, height=height) # #bk.text([x], [y], text = agency, angle=0, text_font_size="7pt", text_align="center", text_baseline="middle") # y = y + height #legend_bottom_y = 40.797 #legend_top_y = legend_bottom_y + 0.92 * height * len(legendColors) #bk.text([-74.225], [legend_bottom_y], text = agency2, angle = 0, text_font_size = "7pt", text_align = "left") #bk.text([-74.225], [legend_top_y], text = agency1, angle = 0, text_font_size = "7pt", text_align = "left") #bokeh.embed.components(fig, bokeh.resources.CDN) bk.show()
tgt_explorations.append((exploration, feedback)) tgt_dataset = { 'm_channels': ex.m_channels, 's_channels': ex.s_channels, 'explorations': tgt_explorations } # Graphs try: from bokeh import plotting plotting.output_file('goal_reuse.html') xs = [explo[1]['s_signal']['x'] for explo in dataset['explorations']] ys = [explo[1]['s_signal']['y'] for explo in dataset['explorations']] plotting.scatter(xs, ys, x_range=[-1, 1], y_range=[-1, 1], title='explorers library: goal reuse algorithm example', fill_alpha=0.5, line_color=None, radius=2.0, radius_units='screen') plotting.show() except ImportError: print( 'exploration went fine, but you need the bokeh library (http://bokeh.pydata.org/) to display it' )
# Graph # the red dots are from motor babbling # the blue dots from goal babbling try: from bokeh import plotting plotting.output_file('goal_babbling.html') xs = [explo[1]['s_signal']['x'] for explo in dataset['explorations']] ys = [explo[1]['s_signal']['y'] for explo in dataset['explorations']] plotting.scatter(xs, ys, x_range=[-1, 1], y_range=[-1, 1], title='explorers library: exploration example', fill_alpha=0.5, line_color=None, radius=2.0, radius_units='screen') plotting.hold(True) plotting.scatter(xs[:ex_cfg.eras[0]], ys[:ex_cfg.eras[0]], x_range=[-1, 1], y_range=[-1, 1], title='explorers library: exploration example', fill_alpha=0.5, color='red', line_color=None, radius=2.0, radius_units='screen')
def drawPlot(shapeFilename, zipBorough, zipCount): # Read the ShapeFile dat = shapefile.Reader(shapeFilename) # Creates a dictionary for zip: {lat_list: [], lng_list: []}. zipCodes = [] polygons = { 'lat_list': [], 'lng_list': [], 'centerLat_list': [], 'centerLon_list': [] } record_index = 0 for r in dat.iterRecords(): currentZip = r[0] # Keeps only zip codes in NY area. if currentZip in zipBorough: # was in zipBorough: # Gets shape for this zip. shape = dat.shapeRecord(record_index).shape ### Only first polygon if non-contiguous zip codes #firstPoint = shape.parts[0] #if len(shape.parts) > 1: # lastPoint = shape.parts[1] #else: # lastPoint = len(shape.points) #zipCodes.append(currentZip) #points = shape.points[firstPoint : lastPoint] # ## Breaks into lists for lat/lng. #lngs = [p[0] for p in points] #lats = [p[1] for p in points] ## Calculate centers #center_lngs = min(lngs) + (max(lngs) - min(lngs))/2 #center_lats = min(lats) + (max(lats) - min(lats))/2 ## Store centroids for current part shape #polygons['centerLat_list'].append(center_lats) #polygons['centerLon_list'].append(center_lngs) ## Stores lat/lng for current zip shape. #polygons['lng_list'].append(lngs) #polygons['lat_list'].append(lats) ### All shapes for each zip code for part in range(len(shape.parts)): zipCodes.append(currentZip) start = shape.parts[part] if part == len(shape.parts) - 1: end = len(shape.points) else: end = shape.parts[part + 1] points = shape.points[start:end] # Breaks into lists for lat/lng. lngs = [p[0] for p in points] lats = [p[1] for p in points] # Calculate centers center_lngs = min(lngs) + (max(lngs) - min(lngs)) / 2 center_lats = min(lats) + (max(lats) - min(lats)) / 2 # Store centroids for current part shape polygons['centerLat_list'].append(center_lats) polygons['centerLon_list'].append(center_lngs) # Stores lat/lng for current zip shape. polygons['lng_list'].append(lngs) polygons['lat_list'].append(lats) record_index += 1 # Creates the Plot bk.output_file("problem4.html") bk.hold() north = 40.915 east = -73.651 south = 40.496 west = -74.256 width = east - west height = north - south x_range = [west - 0.05 * width, east + 0.05 * width] y_range = [south - 0.05 * height, north + 0.05 * height] TOOLS = "pan,wheel_zoom,box_zoom,reset,previewsave" fig = bk.figure(title="311 Complaints by Zip Code", \ tools=TOOLS, background_fill = "#f8f8f8", x_range = x_range, y_range = y_range, plot_height=800, plot_width = 1200) #circleSizes = [zipCount[zipCode] for zipCode in zipCodes] circleSizes = [] for zipCode in zipCodes: if zipCode in zipCount: circleSizes.append(zipCount[zipCode]) else: circleSizes.append(1) # for taking logs #print circleSizes #print "aaaaaaaaaaaaaaaaaaaaa: ", len(polygons['lng_list']), len(circleSizes) logCircleSizes = ((np.log(np.array(circleSizes)))**3.7) * 0.009 #print logCircleSizes logCircleSizes = list(logCircleSizes) #circleSizes = [x for x in range(len(zipCodes))] # Creates the polygons bk.patches(polygons['lng_list'], polygons['lat_list'], fill_color="#fee8c8", line_color="gray") bk.scatter(polygons['centerLon_list'], polygons['centerLat_list'], size=logCircleSizes, alpha=0.4, line_color=None, fill_color='red') #circleSizes = [0.009 * (size ** 3.7) for size in circleSizes] #bk.scatter(centers['lon'], centers['lat'], size = circleSizes, alpha = 0.4, line_color = None, fill_color = 'red') #circleSizeArr = np.array(circleSizes) #maxSize = np.max(circleSizeArr) #circleSizeArr[circleSizeArr == 0] = 100 #minSize = np.min(circleSizeArr) #legendN = 5 #legendCircles = list(np.linspace(minSize, maxSize, legendN)) #legendCounts = [str(int((size ** (1.0/3.7))/0.009)) + " complaints" for size in legendCircles] #fakeScatter = [0 for x in range(legendN)] #fakeScatterX = [-74.23 for x in range(legendN)] #fakeScatterXtext = [-74.23 + 0.05 for x in range(legendN)] #fakeScatterY = [40.8, 40.8 + 0.02, 40.8 + 0.036, 40.8 + 0.056,40.8 + 0.083] #bk.scatter(fakeScatterX, fakeScatterY, size = legendCircles, fill_color = 'red', line_color = None, alpha = 0.4) #bk.text(fakeScatterXtext[1:], fakeScatterY[1:], text = legendCounts[1:], angle = 0, text_align = "left", text_font_size = "7pt", text_baseline = 'middle') # Disable background grid bk.grid().grid_line_color = None bk.show()