def perf_std_discrete(ticks, avgs, stds, legend=None, std_width=0.3, plot_width=1000, plot_height=300, color=BLUE, alpha=1.0, **kwargs): plotting.rect(ticks, avgs, [std_width for _ in stds], 2 * np.array(stds), line_color=None, fill_color=color, fill_alpha=alpha * 0.5, plot_width=plot_width, plot_height=plot_height, **kwargs) plotting.hold(True) plotting.line(ticks, avgs, line_color=color, line_alpha=alpha, legend=legend) plotting.circle(ticks, avgs, line_color=None, fill_color=color, fill_alpha=alpha) plotting.grid().grid_line_color = 'white' plotting_axis() plotting.hold(False)
def mesh(meshgrid, s_vectors=(), s_goals=(), mesh_timescale=(1000000,), mesh_colors=(C_COLOR_H,), title='no title', e_radius=1.0, e_color=E_COLOR, e_alpha=0.75, g_radius=1.0, g_color=G_COLOR, g_alpha=0.75, swap_xy=True, tile_ratio=0.97, x_range=None, y_range=None): x_range, y_range = ranges(meshgrid.s_channels, x_range=x_range, y_range=y_range) xm = zip(*[b.bounds[0] for b in meshgrid.nonempty_bins]) ym = zip(*[b.bounds[1] for b in meshgrid.nonempty_bins]) if swap_xy: x_range, y_range = y_range, x_range xm, ym = ym, xm color = [] for b in meshgrid.nonempty_bins: t = b.elements[0][0] color.append(mesh_colors[bisect.bisect_left(mesh_timescale, t)]) plotting.rect((np.array(xm[1])+np.array(xm[0]))/2 , (np.array(ym[1])+np.array(ym[0]))/2, (np.array(xm[1])-np.array(xm[0]))*tile_ratio, (np.array(ym[1])-np.array(ym[0]))*tile_ratio, x_range=x_range, y_range=y_range, fill_color=color, fill_alpha=0.5, line_color='#444444', line_alpha=0.0, title=title) plotting.hold(True) plotting.grid().grid_line_color='white' spread(meshgrid.s_channels, s_vectors=s_vectors, s_goals=s_goals, swap_xy=swap_xy, e_radius=e_radius, e_color=e_color, e_alpha=e_alpha, g_radius=g_radius, g_color=g_color, g_alpha=g_alpha) plotting.hold(False)
def mesh(meshgrid, s_vectors=(), s_goals=(), mesh_timescale=(1000000, ), mesh_colors=(C_COLOR_H, ), title='no title', e_radius=1.0, e_color=E_COLOR, e_alpha=0.75, g_radius=1.0, g_color=G_COLOR, g_alpha=0.75, swap_xy=True, tile_ratio=0.97, x_range=None, y_range=None): x_range, y_range = ranges(meshgrid.s_channels, x_range=x_range, y_range=y_range) xm = zip(*[b.bounds[0] for b in meshgrid.nonempty_bins]) ym = zip(*[b.bounds[1] for b in meshgrid.nonempty_bins]) if swap_xy: x_range, y_range = y_range, x_range xm, ym = ym, xm color = [] for b in meshgrid.nonempty_bins: t = b.elements[0][0] color.append(mesh_colors[bisect.bisect_left(mesh_timescale, t)]) plotting.rect((np.array(xm[1]) + np.array(xm[0])) / 2, (np.array(ym[1]) + np.array(ym[0])) / 2, (np.array(xm[1]) - np.array(xm[0])) * tile_ratio, (np.array(ym[1]) - np.array(ym[0])) * tile_ratio, x_range=x_range, y_range=y_range, fill_color=color, fill_alpha=0.5, line_color='#444444', line_alpha=0.0, title=title) plotting.hold(True) plotting.grid().grid_line_color = 'white' spread(meshgrid.s_channels, s_vectors=s_vectors, s_goals=s_goals, swap_xy=swap_xy, e_radius=e_radius, e_color=e_color, e_alpha=e_alpha, g_radius=g_radius, g_color=g_color, g_alpha=g_alpha) plotting.hold(False)
def line(x_range, avg, std, color='#E84A5F', dashes=(4, 2), alpha=1.0): plotting.line(x_range, [avg, avg], line_color=color, line_dash=list(dashes), line_alpha=alpha) plotting.hold(True) plotting.rect([(x_range[0] + x_range[1]) / 2.0], [avg], [x_range[1] - x_range[0]], [2 * std], fill_color=color, line_color=None, fill_alpha=0.1 * alpha) plotting.hold(False)
def build_punchcard(datamap_list, concept_list, radii_list, fields_in_concept_list, datamaps, concepts, plot_width=1200, plot_height=800): source = ColumnDataSource( data=dict( datamap=datamap_list, # x concept=concept_list, # y radii=radii_list, fields_in_concept=fields_in_concept_list, ) ) output_file('') hold() figure() plot_properties = { 'title': None, 'tools': "hover,resize,previewsave", 'y_range': [get_datamap_label(datamap) for datamap in datamaps], 'x_range': concepts, 'plot_width': plot_width, 'plot_height': plot_height, } rect('concept', 'datamap', # x, y 1, 1, # height, width source=source, color='white', # put in background **plot_properties) circle('concept', 'datamap', # x, y size='radii', source=source, color='black', **plot_properties) grid().grid_line_color = None x = xaxis() x.major_label_orientation = pi / 4 hover = [t for t in curplot().tools if isinstance(t, HoverTool)][0] hover.tooltips = OrderedDict([ ("Datamap", "@datamap"), ("Concept", "@concept"), ("Fields", "@fields_in_concept"), ]) return curplot().create_html_snippet( static_path=settings.STATIC_URL, embed_save_loc=settings.BOKEH_EMBED_JS_DIR, embed_base_url=reverse('bokeh'), )
def perf_std_discrete(ticks, avgs, stds, legend=None, std_width=0.3, plot_width=1000, plot_height=300, color=BLUE, alpha=1.0, **kwargs): plotting.rect(ticks, avgs, [std_width for _ in stds], 2*np.array(stds), line_color=None, fill_color=color, fill_alpha=alpha*0.5, plot_width=plot_width, plot_height=plot_height, **kwargs) plotting.hold(True) plotting.line(ticks, avgs, line_color=color, line_alpha=alpha, legend=legend) plotting.circle(ticks, avgs, line_color=None, fill_color=color, fill_alpha=alpha) plotting.grid().grid_line_color = 'white' plotting_axis() plotting.hold(False)
def line(x_range, avg, std, color="#E84A5F", dashes=(4, 2), alpha=1.0): plotting.line(x_range, [avg, avg], line_color=color, line_dash=list(dashes), line_alpha=alpha) plotting.hold(True) plotting.rect( [(x_range[0] + x_range[1]) / 2.0], [avg], [x_range[1] - x_range[0]], [2 * std], fill_color=color, line_color=None, fill_alpha=0.1 * alpha, ) plotting.hold(False)
def make_box_violin_plot_2(data, maxwidth=0.9): """ data: dict[Str -> List[Number]] maxwidth: float Maximum width of tornado plot within each factor/facet Returns the plot object """ df = pd.DataFrame(columns=["group", "centers", "width", "height", "texts"]) bar_height = 50 bins = [0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6] # Compute histograms, while keeping track of max Y values and max counts for i, (group, vals) in enumerate(data.iteritems()): hist, edges = np.histogram(vals, bins) df = df.append(pd.DataFrame(dict( group = group, centers = np.arange(len(hist))*bar_height, width = np.log10(hist), height = np.ones(hist.shape)*bar_height, texts = map(str,hist), ))) df.replace(-np.inf, 0) # Normalize the widths df["width"] *= (maxwidth / df["width"].max()) hold() width, height = 800, 800 figure(plot_width=width, plot_height=height, title="Degree Distribution", tools="previewsave", x_axis_type=None, y_axis_type=None, x_range=[-420, 420], y_range=[-420, 420], x_axis_label="Number of nodes (log)", y_label="distribution of degree", min_border=0, outline_line_color=None, background_fill="#f0e1d2", border_fill="#f0e1d2") size = len(df) rect(np.zeros(size), df["centers"], height=[50 for i in range(size)], width=df["width"]*width * .8) text(np.zeros(size), df["centers"], text=df["texts"], angle=np.zeros(size), text_color= ["#000000"] + ["#FFFFFF" for i in xrange(size-1)], text_align="center", text_baseline="middle") text(np.ones(size+1) * -width/2, [(idx-0.75)*bar_height for idx in range(size+1)], text=map(str, map(int, bins)), angle=0) show()
def make_trip_distance_histogram(self): bins = self.trip_distance_bins centers = pd.rolling_mean(bins, 2)[1:] figure(title="trip distance in miles", title_text_font='12pt', plot_width=300, plot_height=200, x_range=[bins[0], bins[-1]], y_range=[0, 1], tools="pan,wheel_zoom,box_zoom,select,reset" ) source = HistogramDataSource( data_url="/bokeh/taxidata/distancehist/", ) hold() plot = rect("centers", "y", np.mean(np.diff(centers)) * 0.7, "counts", source=source) self.trip_distance_source = plot.select({'type' : ColumnDataSource})[0] self.trip_distance_ar_source = source plot.min_border=0 plot.h_symmetry=False plot.v_symmetry=False select_tool = _get_select_tool(plot) if select_tool: select_tool.dimensions = ['width'] self.distance_histogram = plot
def graph(): print("Graphing") with open(DATA_FILE, "rb") as fin: issues = [Issue(x) for x in pickle.load(fin)] plotting.output_file("{}.{}.html".format(GH_USER, GH_REPO), title="ghantt.py") numbers = [iss.number for iss in issues] source = ColumnDataSource( data=dict( number = [iss.number for iss in issues], ago = [iss.ago + iss.length/2 for iss in issues], length = [iss.length for iss in issues], title = [iss.title for iss in issues], pull_request = [iss.pull_request for iss in issues], color = [assign_color(iss) for iss in issues], since = ["{} days".format(int(abs(iss.ago))) for iss in issues], ), ) plotting.hold() plotting.rect("ago", "number", "length", 1, source=source, color="color", title="{}/{}".format(GH_USER, GH_REPO), y_range=(min(numbers), max(numbers)), tools="resize,hover,previewsave,pan,wheel_zoom", fill_alpha=0.8) text_props = { "source": source, "angle": 0, "color": "black", "text_align": "left", "text_baseline": "middle" } plotting.grid().grid_line_color = None hover = [t for t in plotting.curplot().tools if isinstance(t, HoverTool)][0] hover.tooltips = OrderedDict([ ("number", "@number"), ("title", "@title"), ("since", "@since"), ("pull_request", "@pull_request"), ]) plotting.show()
def hist_plot(self, ticker): global_hist, global_bins = np.histogram(self.df[ticker + "_returns"], bins=50) hist, bins = np.histogram(self.selected_df[ticker + "_returns"], bins=50) width = 0.7 * (bins[1] - bins[0]) center = (bins[:-1] + bins[1:]) / 2 start = global_bins.min() end = global_bins.max() top = hist.max() return rect(center, hist/2.0, width, hist, title="%s hist" % ticker, plot_width=500, plot_height=200, tools="", title_text_font_size="10pt", x_range=Range1d(start=start, end=end), y_range=Range1d(start=0, end=top))
def hist_plot(self, ticker): global_hist, global_bins = np.histogram(self.df[ticker + "_returns"], bins=50) hist, bins = np.histogram(self.selected_df[ticker + "_returns"], bins=50) width = 0.7 * (bins[1] - bins[0]) center = (bins[:-1] + bins[1:]) / 2 start = global_bins.min() end = global_bins.max() top = hist.max() return rect( center, hist/2.0, width, hist, title="%s hist" % ticker, plot_width=500, plot_height=200, tools="", title_text_font_size="10pt", x_range=[start, end], y_range=[0, top], )
def drawPlot(shapeFilename, zipBorough, zipMaxAgency): # Read the ShapeFile dat = shapefile.Reader(shapeFilename) # Creates a dictionary for zip: {lat_list: [], lng_list: []}. zipCodes = [] hoverZip = [] hoverAgency = [] hoverComplaints = [] 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 zipMaxAgency: # was in zipBorough: # zipCodes.append(currentZip) # moving this line into the parts loop # Gets shape for this zip. shape = dat.shapeRecord(record_index).shape for part in range(len(shape.parts)): zipCodes.append(currentZip) hoverZip.append(currentZip) hoverAgency.append(zipMaxAgency[currentZip][0]) hoverComplaints.append(zipMaxAgency[currentZip][1]) 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 # Palette ##d9d9d9 brewer11 = ['#8dd3c7', '#ffffb3', '#bebada','#fb8072','#80b1d3','#fdb462','#b3de69','#fccde5','#d9d9d9','#bc80bd','#ccebc5'] #brewer11 = ['#a6cee3', '#1f78b4','#b2df8a','#33a02c','#fb9a99','#e31a1c','#fdbf6f','#ff7f00','#cab2d6','#6a3d9a','#ffff99'] #agencies = sorted(list({zipMaxAgency[zipCode] for zipCode in zipMaxAgency})) biggestComplaints = {} for zz, aa in zipMaxAgency.iteritems(): if aa[0] in biggestComplaints: biggestComplaints[aa[0]] += 1 else: biggestComplaints[aa[0]] = 1 # sorting agencies by number of zip codes (to try to get better colors) agencies = list(biggestComplaints.iteritems()) agencies = sorted(agencies, key = lambda x: x[1], reverse = True) agencies = [agency[0] for agency in agencies] # Assign colors to agencies agencyColor = {agencies[i] : brewer11[i] for i in range(len(brewer11))} polygons['colors'] = [agencyColor[zipMaxAgency[zipCode][0]] for zipCode in zipCodes] # Prepare hover #source = bk.ColumnDataSource(data=dict(hoverAgency=hoverAgency, hoverZip=hoverZip, hoverComplaintCount=hoverComplaintCount,)) source = bk.ColumnDataSource(data=dict(hoverZip = hoverZip, hoverAgency = hoverAgency, hoverComplaints = hoverComplaints),) # Creates the Plot bk.output_file("problem1.html") bk.hold() TOOLS="pan,wheel_zoom,box_zoom,reset,previewsave,hover" fig = bk.figure(title="311 Complaints by Zip Code", \ tools=TOOLS, plot_width=800, plot_height=650) # Creates the polygons. bk.patches(polygons['lng_list'], polygons['lat_list'], fill_color=polygons['colors'], line_color="gray", source = source) # RP: add hover hover = bk.curplot().select(dict(type=HoverTool)) hover.tooltips = OrderedDict([ ("Zip", "@hoverZip"), ("Agency", "@hoverAgency"), ("Number of complaints", "@hoverComplaints"), ]) ### Zip codes as text on polygons #for i in range(len(polygons['centerLat_list'])): # y = polygons['centerLat_list'][i] # x = polygons['centerLon_list'][i] # zipCode = zipCodes[i] # bk.text([x], [y], text=zipCode, angle=0, text_font_size="8pt", text_align="center", text_baseline="middle") fonts = ["Comic sans MS", "Papyrus", "Curlz", "Impact", "Zapf dingbats", "Comic sans MS", "Papyrus", "Curlz", "Impact", "Zapf Dingbats", "Comic sans MS"] ### Legend x = -73.66 y = 40.50 #x = -74.25 #y = 40.9 for agency, color in agencyColor.iteritems(): #print "Color: ", a #print "x:", x #print "y:", y bk.rect([x], [y], color = color, width=0.03, height=0.015) bk.text([x], [y], text = agency, angle=0, text_font_size="7pt", text_align="center", text_baseline="middle") y = y + 0.015 #bokeh.embed.components(fig, bokeh.resources.CDN) bk.show()
ypos = [str(y) for y in df['y']], number = df['prime'], color_prime = df['color'] ) ) # hold() to put all the boxes on one grid bk.hold() # Set x-y ranges x_range = [str(x) for x in range(-1,11)] y_range = [str(y) for y in range(-1,-11)] # Make a rect glyph for each factor bk.rect("xpos","ypos",0.9,0.9,source=source, x_range=x_range, y_range=y_range, fill_alpha=0.6, color="color_prime", tools="resize", title="Prime Factor Visualization" ) # Add text to display the number for each box # Use text_props = dict to set properties of text elements text_props = { "source": source, "angle": 0, "color": "black", "text_align": "center", "text_baseline": "middle" } bk.text(x=dict(field="xpos", units="data"), y=dict(field="ypos", units="data"),
def bokeh_mesh_density(meshgrid, s_vectors=(), s_goals=(), swap_xy=True, mesh_colors=('#DDDDDD', '#BBBBBB', '#999999', '#777777', '#555555'), title='no title', e_radius=1.0, e_color=E_COLOR, e_alpha=0.75, g_radius=1.0, g_color=G_COLOR, g_alpha=0.75, x_range=None, y_range=None, **kwargs): x_range, y_range = ranges(s_channels, x_range=x_range, y_range=y_range) xm = zip(*[b.bounds[0] for b in meshgrid.nonempty_bins]) ym = zip(*[b.bounds[1] for b in meshgrid.nonempty_bins]) if swap_xy: x_range, y_range = y_range, x_range xm, ym = ym, xm d_max = max(len(b) for b in meshgrid.nonempty_bins) mesh_scale = [ 1.0 + i / (len(mesh_colors) - 1) * d_max for i in range(len(mesh_colors)) ] colorbar = ColorBar(mesh_scale, mesh_colors, continuous=True) color = [] for b in meshgrid.nonempty_bins: color.append(colorbar.color(len(b))) plotting.rect( (np.array(xm[1]) + np.array(xm[0])) / 2, (np.array(ym[1]) + np.array(ym[0])) / 2, (np.array(xm[1]) - np.array(xm[0])) * 0.97, (np.array(ym[1]) - np.array(ym[0])) * 0.97, #x_range=env.s_channels[0].bounds, y_range=env.s_channels[1].bounds, x_range=x_range, y_range=y_range, fill_color=color, fill_alpha=0.5, line_color='#444444', line_alpha=0.0, title=title) plotting.hold(True) plotting.grid().grid_line_color = 'white' spread(meshgrid.s_channels, s_vectors=s_vectors, s_goals=s_goals, swap_xy=swap_xy, e_radius=e_radius, e_color=e_color, e_alpha=e_alpha, g_radius=g_radius, g_color=g_color, g_alpha=g_alpha) plotting.hold(False) plotting.hold(False)
for i in range(len(werte)): anteil.append(0) for i in range(len(anteil)): anteil[i] = werte[i]/sumVal print anteil # Create `Range1d` object to set the plot's `y_range`. yr = Range1d(start=0, end=1) figure(y_range=yr) # Plot each `rect` object and `show()` the plot. hold(True) for i in range(len(mitten)): rect([mitten[i]],[anteil[i]/2], width=balkenbreite, height=anteil[i], plot_width=400, color = "#ff1200", plot_height=400) xaxis().axis_label="Areas" yaxis().axis_label="Frequency" # Bonus round: you can even change the `x_range` attribute of the current plot (referenced as `curplot()`) at any time. Let's give the plot a bit of padding on each side. xr = Range1d(start=5, end=45) curplot().x_range = xr show()
def make_plot(xr): yr = Range1d(start=-10, end=10) figure(plot_width=800, plot_height=350, y_range=yr, x_range=xr, tools="xpan,xwheel_zoom,hover,box_zoom,reset") hold() genes = pd.read_csv('/home/hugoshi/data/lab7/genes.refseq.hg19.bed', sep='\t', skiprows=[0], header=None) genes.rename(columns={0: "chromosome", 1: "start", 2: "end"}, inplace=True) genes = genes[genes.chromosome == 'chr5'] g_len = len(genes) quad( genes.start - 0.5, # left edge genes.end - 0.5, # right edge [2.3] * g_len, # top edge [1.7] * g_len, ['blue'] * g_len) # bottom edge exons = pd.read_csv('/home/hugoshi/data/lab7/exons.refseq.hg19.bed', sep='\t', skiprows=[0], header=None) exons.columns = ["chromosome", "start", "end", "meta1", "meta2", "meta3"] exons = exons[exons.chromosome == 'chr5'] e_len = len(exons) quad( exons.start - 0.5, # left edge exons.end - 0.5, # right edge [1.3] * e_len, # top edge [0.7] * e_len, ['blue'] * e_len) # bottom edge df = pd.read_csv('/home/hugoshi/data/lab7/CHP2.20131001.hotspots.bed', sep='\t', skiprows=[0], header=None) df.columns = ["chromosome", "start", "end", "meta1", "meta2", "meta3"] df = df[df.chromosome == 'chr5'] singles = df[df.start + 1 == df.end] widers = df[df.start + 1 != df.end] slen = len(singles) wlen = len(widers) s_source = ColumnDataSource(data=dict(start=singles.start, end=singles.end, meta1=singles.meta1, meta2=singles.meta2, meta3=singles.meta3)) rect( 'start', # x center [1] * slen, # y center [0.9] * slen, [1] * slen, color=['red'] * slen, source=s_source) # height hover = [t for t in curplot().tools if isinstance(t, HoverTool)][0] hover.tooltips = OrderedDict([ # add to this ("position", "@start"), ("meta 1", "@meta1"), ("meta 2", "@meta2"), ("meta 3", "@meta3") ]) quad( widers.start - 0.5, # left edge widers.end - 0.5, # right edge [0.3] * wlen, # top edge [-0.3] * wlen) # bottom edge hold() return curplot()
name = df['cabinetName'], loc = df['labLocation'], sub = df['subsystem'], equip = df['equipment'], phone = df['phone'], color_type = [colormap[str(x)] for x in df['subsystem']] ) ) # hold() to put all glyphs on the same graph bk.hold() # Make a rect glyph for each element of data bk.rect("posx", "posy", 0.9, 0.9, source=source, x_range=x_range, y_range=y_range, line_color="color_type", line_cap='round', fill_alpha=0.6, color="color_type", tools="resize,hover,previewsave,ywheel_zoom", title="ESS Lab Layout", plot_width=1600, plot_height=3200 ) # Use text_props = dict to set properties of text elements text_props = { "source": source, "angle": 0, "color": "black", "text_align": "center", "text_baseline": "middle" } # Make a text glyph for each rect glyph to show the cabinet names and locations bk.text(x=dict(field="posx", units="data"),
def bokeh_mesh_density( meshgrid, s_vectors=(), s_goals=(), swap_xy=True, mesh_colors=("#DDDDDD", "#BBBBBB", "#999999", "#777777", "#555555"), title="no title", e_radius=1.0, e_color=E_COLOR, e_alpha=0.75, g_radius=1.0, g_color=G_COLOR, g_alpha=0.75, x_range=None, y_range=None, **kwargs ): x_range, y_range = ranges(s_channels, x_range=x_range, y_range=y_range) xm = zip(*[b.bounds[0] for b in meshgrid.nonempty_bins]) ym = zip(*[b.bounds[1] for b in meshgrid.nonempty_bins]) if swap_xy: x_range, y_range = y_range, x_range xm, ym = ym, xm d_max = max(len(b) for b in meshgrid.nonempty_bins) mesh_scale = [1.0 + i / (len(mesh_colors) - 1) * d_max for i in range(len(mesh_colors))] colorbar = ColorBar(mesh_scale, mesh_colors, continuous=True) color = [] for b in meshgrid.nonempty_bins: color.append(colorbar.color(len(b))) plotting.rect( (np.array(xm[1]) + np.array(xm[0])) / 2, (np.array(ym[1]) + np.array(ym[0])) / 2, (np.array(xm[1]) - np.array(xm[0])) * 0.97, (np.array(ym[1]) - np.array(ym[0])) * 0.97, # x_range=env.s_channels[0].bounds, y_range=env.s_channels[1].bounds, x_range=x_range, y_range=y_range, fill_color=color, fill_alpha=0.5, line_color="#444444", line_alpha=0.0, title=title, ) plotting.hold(True) plotting.grid().grid_line_color = "white" spread( meshgrid.s_channels, s_vectors=s_vectors, s_goals=s_goals, swap_xy=swap_xy, e_radius=e_radius, e_color=e_color, e_alpha=e_alpha, g_radius=g_radius, g_color=g_color, g_alpha=g_alpha, ) plotting.hold(False) plotting.hold(False)
def drawPlot(shapeFilename, zipBorough, zipMaxAgency): # Read the ShapeFile dat = shapefile.Reader(shapeFilename) # Creates a dictionary for zip: {lat_list: [], lng_list: []}. zipCodes = [] hoverZip = [] hoverAgency = [] hoverComplaints = [] 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 zipMaxAgency: # was in zipBorough: # zipCodes.append(currentZip) # moving this line into the parts loop # Gets shape for this zip. shape = dat.shapeRecord(record_index).shape for part in range(len(shape.parts)): zipCodes.append(currentZip) hoverZip.append(currentZip) hoverAgency.append(zipMaxAgency[currentZip][0]) hoverComplaints.append(zipMaxAgency[currentZip][1]) 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 # Palette ##d9d9d9 brewer11 = [ '#8dd3c7', '#ffffb3', '#bebada', '#fb8072', '#80b1d3', '#fdb462', '#b3de69', '#fccde5', '#d9d9d9', '#bc80bd', '#ccebc5' ] #brewer11 = ['#a6cee3', '#1f78b4','#b2df8a','#33a02c','#fb9a99','#e31a1c','#fdbf6f','#ff7f00','#cab2d6','#6a3d9a','#ffff99'] #agencies = sorted(list({zipMaxAgency[zipCode] for zipCode in zipMaxAgency})) biggestComplaints = {} for zz, aa in zipMaxAgency.iteritems(): if aa[0] in biggestComplaints: biggestComplaints[aa[0]] += 1 else: biggestComplaints[aa[0]] = 1 # sorting agencies by number of zip codes (to try to get better colors) agencies = list(biggestComplaints.iteritems()) agencies = sorted(agencies, key=lambda x: x[1], reverse=True) agencies = [agency[0] for agency in agencies] # Assign colors to agencies agencyColor = {agencies[i]: brewer11[i] for i in range(len(brewer11))} polygons['colors'] = [ agencyColor[zipMaxAgency[zipCode][0]] for zipCode in zipCodes ] # Prepare hover #source = bk.ColumnDataSource(data=dict(hoverAgency=hoverAgency, hoverZip=hoverZip, hoverComplaintCount=hoverComplaintCount,)) source = bk.ColumnDataSource(data=dict(hoverZip=hoverZip, hoverAgency=hoverAgency, hoverComplaints=hoverComplaints), ) # Creates the Plot bk.output_file("problem1.html") bk.hold() TOOLS = "pan,wheel_zoom,box_zoom,reset,previewsave,hover" fig = bk.figure(title="311 Complaints by Zip Code", \ tools=TOOLS, plot_width=800, plot_height=650) # Creates the polygons. bk.patches(polygons['lng_list'], polygons['lat_list'], fill_color=polygons['colors'], line_color="gray", source=source) # RP: add hover hover = bk.curplot().select(dict(type=HoverTool)) hover.tooltips = OrderedDict([ ("Zip", "@hoverZip"), ("Agency", "@hoverAgency"), ("Number of complaints", "@hoverComplaints"), ]) ### Zip codes as text on polygons #for i in range(len(polygons['centerLat_list'])): # y = polygons['centerLat_list'][i] # x = polygons['centerLon_list'][i] # zipCode = zipCodes[i] # bk.text([x], [y], text=zipCode, angle=0, text_font_size="8pt", text_align="center", text_baseline="middle") fonts = [ "Comic sans MS", "Papyrus", "Curlz", "Impact", "Zapf dingbats", "Comic sans MS", "Papyrus", "Curlz", "Impact", "Zapf Dingbats", "Comic sans MS" ] ### Legend x = -73.66 y = 40.50 #x = -74.25 #y = 40.9 for agency, color in agencyColor.iteritems(): #print "Color: ", a #print "x:", x #print "y:", y bk.rect([x], [y], color=color, width=0.03, height=0.015) bk.text([x], [y], text=agency, angle=0, text_font_size="7pt", text_align="center", text_baseline="middle") y = y + 0.015 #bokeh.embed.components(fig, bokeh.resources.CDN) bk.show()
def make_plot(xr): yr = Range1d(start=-10, end=10) figure(plot_width=800, plot_height=350, y_range=yr, x_range=xr, tools="xpan,xwheel_zoom,hover,box_zoom,reset") hold() genes = pd.read_csv('/home/hugoshi/data/lab7/genes.refseq.hg19.bed', sep='\t', skiprows=[0], header=None) genes.rename(columns={ 0: "chromosome", 1: "start", 2: "end"}, inplace=True ) genes = genes[genes.chromosome == 'chr5'] g_len = len(genes) quad(genes.start - 0.5, # left edge genes.end - 0.5, # right edge [2.3] * g_len, # top edge [1.7] * g_len, ['blue'] * g_len) # bottom edge exons = pd.read_csv('/home/hugoshi/data/lab7/exons.refseq.hg19.bed', sep='\t', skiprows=[0], header=None) exons.columns = ["chromosome", "start", "end", "meta1", "meta2", "meta3"] exons = exons[exons.chromosome == 'chr5'] e_len = len(exons) quad(exons.start - 0.5, # left edge exons.end - 0.5, # right edge [1.3] * e_len, # top edge [0.7] * e_len, ['blue'] * e_len) # bottom edge df = pd.read_csv('/home/hugoshi/data/lab7/CHP2.20131001.hotspots.bed', sep='\t', skiprows=[0], header=None) df.columns = ["chromosome", "start", "end", "meta1", "meta2", "meta3"] df = df[df.chromosome == 'chr5'] singles = df[df.start+1 == df.end] widers = df[df.start+1 != df.end] slen = len(singles) wlen = len(widers) s_source = ColumnDataSource( data = dict( start=singles.start, end=singles.end, meta1=singles.meta1, meta2=singles.meta2, meta3=singles.meta3)) rect('start', # x center [1]*slen, # y center [0.9]*slen, [1]*slen, color=['red']*slen, source=s_source) # height hover = [t for t in curplot().tools if isinstance(t, HoverTool)][0] hover.tooltips = OrderedDict([ # add to this ("position", "@start"), ("meta 1", "@meta1"), ("meta 2", "@meta2"), ("meta 3", "@meta3") ]) quad(widers.start - 0.5, # left edge widers.end - 0.5, # right edge [0.3] * wlen, # top edge [-0.3] * wlen) # bottom edge hold() return curplot()
def make_box_violin_plot_2(data, maxwidth=0.9): """ data: dict[Str -> List[Number]] maxwidth: float Maximum width of tornado plot within each factor/facet Returns the plot object """ df = pd.DataFrame(columns=["group", "centers", "width", "height", "texts"]) bar_height = 50 bins = [0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6] # Compute histograms, while keeping track of max Y values and max counts for i, (group, vals) in enumerate(data.iteritems()): hist, edges = np.histogram(vals, bins) df = df.append( pd.DataFrame( dict( group=group, centers=np.arange(len(hist)) * bar_height, width=np.log10(hist), height=np.ones(hist.shape) * bar_height, texts=map(str, hist), ))) df.replace(-np.inf, 0) # Normalize the widths df["width"] *= (maxwidth / df["width"].max()) hold() width, height = 800, 800 figure(plot_width=width, plot_height=height, title="Degree Distribution", tools="previewsave", x_axis_type=None, y_axis_type=None, x_range=[-420, 420], y_range=[-420, 420], x_axis_label="Number of nodes (log)", y_label="distribution of degree", min_border=0, outline_line_color=None, background_fill="#f0e1d2", border_fill="#f0e1d2") size = len(df) rect(np.zeros(size), df["centers"], height=[50 for i in range(size)], width=df["width"] * width * .8) text(np.zeros(size), df["centers"], text=df["texts"], angle=np.zeros(size), text_color=["#000000"] + ["#FFFFFF" for i in xrange(size - 1)], text_align="center", text_baseline="middle") text(np.ones(size + 1) * -width / 2, [(idx - 0.75) * bar_height for idx in range(size + 1)], text=map(str, map(int, bins)), angle=0) show()