def get_plottag(script_path): """ Saves js in script_path and returns tag for embedding in html """ import numpy as np import pandas as pd import os y_data = np.random.randn(100) x_data = pd.date_range('31-Aug-2014', periods=len(y_data)) figure(x_axis_type='datetime', tools='pan,wheel_zoom,box_zoom,reset,previewsave,crosshair', name='test plot') hold() line(x_data, y_data, line_color="#D95B43", line_width=4, alpha=0.7, legend='random', background_fill= '#cccccc') circle(x_data, y_data, color='red', fill_color=None, size=6, legend='random') curplot().title = 'Test Plot' xaxis().axis_label='date' yaxis().axis_label='some random numbers' grid().grid_line_color='white' grid().grid_line_alpha = 0.5 plotid = curplot()._id script_path = os.path.join(script_path, plotid+'.js') js, tag = autoload_static(curplot(), CDN, script_path=script_path) with open(script_path, 'w') as f: f.write(js) return tag
def bokeh_quantiles(ticks, avgs, quantiles, color='#000055', alpha=1.0, **kwargs): plotting.line(ticks, avgs, color=color, line_alpha=alpha, title_text_font_size='6pt', **kwargs) plotting.hold(True) if quantiles is not None: print(quantiles) x_std = list(ticks) + list(reversed(ticks)) y_std = ([q_min for q_min, q_max in quantiles] + list(reversed([q_max for q_min, q_max in quantiles]))) plotting.patch(x_std, y_std, fill_color=color, fill_alpha=alpha * 0.25, line_color=None) plotting.grid().grid_line_color = 'white' plotting_axis() plotting.hold(False)
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 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 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 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 posture_signals(kin_env, m_signals, title='posture graphs', color='#666666', alpha=1.0, radius_factor=1.0, swap_xy=True, x_range=[-1.0, 1.0], y_range=[-1.0, 1.0], **kwargs): for m_signal in m_signals: m_vector = kin_env.flatten_synergies(m_signal) s_signal = kin_env._multiarm.forward_kin(m_vector) xs, ys = [0.0], [0.0] for i in range(kin_env.cfg.dim): xs.append(s_signal['x{}'.format(i + 1)]) ys.append(s_signal['y{}'.format(i + 1)]) if isinstance(kin_env.cfg.lengths, numbers.Real): total_length = kin_env.cfg.lengths * kin_env.cfg.dim else: total_length = sum(kin_env.cfg.lengths) total_length += 0.0 kwargs.update({ 'x_range': x_range, 'y_range': y_range, 'line_color': color, 'line_alpha': alpha, 'fill_color': color, 'fill_alpha': alpha, 'title': title }) if swap_xy: xs, ys = ys, xs plotting.line(xs, ys, line_width=2.0 * radius_factor, **kwargs) plotting.hold(True) plotting.grid().grid_line_color = None plotting.ygrid().grid_line_color = None plotting_axis() plotting.circle(xs[:1], ys[:1], radius=radius_factor * 0.015, **kwargs) plotting.circle(xs[1:-1], ys[1:-1], radius=radius_factor * 0.008, **kwargs) plotting.circle(xs[-1:], ys[-1:], radius=radius_factor * 0.01, color='red', alpha=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 posture_signals( kin_env, m_signals, title="posture graphs", color="#666666", alpha=1.0, radius_factor=1.0, swap_xy=True, x_range=[-1.0, 1.0], y_range=[-1.0, 1.0], **kwargs ): for m_signal in m_signals: m_vector = kin_env.flatten_synergies(m_signal) s_signal = kin_env._multiarm.forward_kin(m_vector) xs, ys = [0.0], [0.0] for i in range(kin_env.cfg.dim): xs.append(s_signal["x{}".format(i + 1)]) ys.append(s_signal["y{}".format(i + 1)]) if isinstance(kin_env.cfg.lengths, numbers.Real): total_length = kin_env.cfg.lengths * kin_env.cfg.dim else: total_length = sum(kin_env.cfg.lengths) total_length += 0.0 kwargs.update( { "x_range": x_range, "y_range": y_range, "line_color": color, "line_alpha": alpha, "fill_color": color, "fill_alpha": alpha, "title": title, } ) if swap_xy: xs, ys = ys, xs plotting.line(xs, ys, line_width=2.0 * radius_factor, **kwargs) plotting.hold(True) plotting.grid().grid_line_color = None plotting.ygrid().grid_line_color = None plotting_axis() plotting.circle(xs[:1], ys[:1], radius=radius_factor * 0.015, **kwargs) plotting.circle(xs[1:-1], ys[1:-1], radius=radius_factor * 0.008, **kwargs) plotting.circle(xs[-1:], ys[-1:], radius=radius_factor * 0.01, color="red", alpha=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 bokeh_quantiles(ticks, avgs, quantiles, color="#000055", alpha=1.0, **kwargs): plotting.line(ticks, avgs, color=color, line_alpha=alpha, title_text_font_size="6pt", **kwargs) plotting.hold(True) if quantiles is not None: print(quantiles) x_std = list(ticks) + list(reversed(ticks)) y_std = [q_min for q_min, q_max in quantiles] + list(reversed([q_max for q_min, q_max in quantiles])) plotting.patch(x_std, y_std, fill_color=color, fill_alpha=alpha * 0.25, line_color=None) plotting.grid().grid_line_color = "white" plotting_axis() plotting.hold(False)
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 count_by_day_graph(collection_name): # TODO: выделить на графике выходные дни import numpy as np from bokeh.plotting import output_file, hold, figure, line, curplot, grid, show a = count_by_day(collection_name) output_file("output/count_by_day_graph.html", title="count_by_day_graph") hold() figure(x_axis_type="datetime", tools="pan,wheel_zoom,box_zoom,reset,previewsave", plot_width=1800) line(np.array(a["date"], 'M64'), a['count'], color='#A6CEE3', legend='Количество статей') line(np.array(a["date"], 'M64'), [averange_count(collection_name)] * len(a["date"]), color='#ff0000', legend='Среднее количество статей') curplot().title = "Количество статей по дням" grid().grid_line_alpha=0.3 show()
def bokeh_kin(kin_env, m_signal, color='#DF4949', alpha=1.0, **kwargs): m_vector = tools.to_vector(m_signal, kin_env.m_channels) s_signal = kin_env._multiarm.forward_kin(m_vector) xs, ys = [0.0], [0.0] for i in range(kin_env.cfg.dim): xs.append(s_signal['x{}'.format(i + 1)]) ys.append(s_signal['y{}'.format(i + 1)]) xs, ys = ys, xs # we swap x and y for a more symmetrical look if isinstance(kin_env.cfg.lengths, numbers.Real): total_length = kin_env.cfg.lengths * kin_env.cfg.dim else: total_length = sum(kin_env.cfg.lengths) total_length += 0.0 kwargs = { 'plot_height': int(350 * 1.60), 'plot_width': int(350 * 1.60), 'x_range': [-1.0, 1.0], 'y_range': [-1.0, 1.0], 'line_color': color, 'line_alpha': alpha, 'fill_color': color, 'fill_alpha': alpha, 'title': '' } plotting.hold() plotting.line(xs, ys, **kwargs) plotting.grid().grid_line_color = None plotting.xaxis().major_tick_in = 0 plotting.ygrid().grid_line_color = None plotting.yaxis().major_tick_in = 0 plotting.circle(xs[:1], ys[:1], radius=0.015, **kwargs) plotting.circle(xs[1:-1], ys[1:-1], radius=0.008, **kwargs) plotting.circle(xs[-1:], ys[-1:], radius=0.01, color='red') plotting.hold(False)
def bokeh_kin(kin_env, m_signal, color='#DF4949', alpha=1.0, **kwargs): m_vector = tools.to_vector(m_signal, kin_env.m_channels) s_signal = kin_env._multiarm.forward_kin(m_vector) xs, ys = [0.0], [0.0] for i in range(kin_env.cfg.dim): xs.append(s_signal['x{}'.format(i+1)]) ys.append(s_signal['y{}'.format(i+1)]) xs, ys = ys, xs # we swap x and y for a more symmetrical look if isinstance(kin_env.cfg.lengths, numbers.Real): total_length = kin_env.cfg.lengths*kin_env.cfg.dim else: total_length = sum(kin_env.cfg.lengths) total_length += 0.0 kwargs ={'plot_height' : int(350*1.60), 'plot_width' : int(350*1.60), 'x_range' : [-1.0, 1.0], 'y_range' : [-1.0, 1.0], 'line_color' : color, 'line_alpha' : alpha, 'fill_color' : color, 'fill_alpha' : alpha, 'title':'' } plotting.hold() plotting.line(xs, ys, **kwargs) plotting.grid().grid_line_color = None plotting.xaxis().major_tick_in = 0 plotting.ygrid().grid_line_color = None plotting.yaxis().major_tick_in = 0 plotting.circle(xs[ : 1], ys[ : 1], radius=0.015, **kwargs) plotting.circle(xs[ 1:-1], ys[ 1:-1], radius=0.008, **kwargs) plotting.circle(xs[-1: ], ys[-1: ], radius=0.01, color='red') plotting.hold(False)
def perf_astd(ticks, avgs, astds, color=BLUE, alpha=1.0, sem=1.0, plot_width=1000, plot_height=500, **kwargs): plotting.line(ticks, avgs, color=color, line_alpha=alpha, title_text_font_size='6pt', plot_width=plot_width, plot_height=plot_height, **kwargs) plotting.hold(True) if astds is not None: x_std = list(ticks) + list(reversed(ticks)) y_std = ([ a - s_min / math.sqrt(sem) for a, (s_min, s_max) in zip(avgs, astds) ] + list( reversed([ a + s_max / math.sqrt(sem) for a, (s_min, s_max) in zip(avgs, astds) ]))) plotting.patch(x_std, y_std, fill_color=color, fill_alpha=alpha * 0.25, line_color=None) plotting.grid().grid_line_color = 'white' plotting_axis() plotting.hold(False)
def perf_astd(ticks, avgs, astds, color=BLUE, alpha=1.0, sem=1.0, plot_width=1000, plot_height=500, **kwargs): plotting.line( ticks, avgs, color=color, line_alpha=alpha, title_text_font_size="6pt", plot_width=plot_width, plot_height=plot_height, **kwargs ) plotting.hold(True) if astds is not None: x_std = list(ticks) + list(reversed(ticks)) y_std = [a - s_min / math.sqrt(sem) for a, (s_min, s_max) in zip(avgs, astds)] + list( reversed([a + s_max / math.sqrt(sem) for a, (s_min, s_max) in zip(avgs, astds)]) ) plotting.patch(x_std, y_std, fill_color=color, fill_alpha=alpha * 0.25, line_color=None) plotting.grid().grid_line_color = "white" plotting_axis() plotting.hold(False)
def get(self, request, *args, **kwargs): return self.render_to_json_response({'plot': u''}) # measurement_type_pk = kwargs.get('measurement_type_pk') # measurement_type = MeasurementType.objects.get(pk=measurement_type_pk) collection = Collection.objects.get(pk=kwargs.get('pk')) units = Unit.objects.filter(measurements__match={'measurement_type': measurement_type.pk, 'active': True}, pk__in=[unit.pk for unit in collection.units], active=True) if units: bk.hold() bk.figure(x_axis_type="datetime", tools="pan,wheel_zoom,box_zoom,reset,previewsave") colors = self.get_colors(len(units)) for i, unit in enumerate(units): measurements = [(measurement.created_at, measurement.value) for measurement in unit.measurements if measurement.active and measurement.measurement_type == measurement_type] measurements.sort(key=lambda measurement: measurement[0]) data = { 'date': [measurement[0] for measurement in measurements], 'value': [measurement[1] for measurement in measurements] } bk.line(np.array(data['date']), data['value'], color=colors[i], line_width=2, legend=unit.__unicode__()) bk.grid().grid_line_alpha = 0.3 xax, yax = bk.axis() xax.axis_label = ugettext('Date') yax.axis_label = ugettext('Values') plot = bk.curplot() bk.get_default_color() plot.title = ugettext('Measurements type') # plot.title = ugettext('Measurements type {}'.format(measurement_type.__unicode__())) js, tag = autoload_static(plot, Resources(mode='server', root_url=settings.STATIC_URL), "") return self.render_to_json_response({'plot': u'{}<script>{}</script>'.format(tag, js)}) return self.render_to_json_response(ugettext('Not found'), status=404)
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 test_grid(self): plt.figure() p = plt.circle([1,2,3], [1,2,3]) self.assertEqual(len(plt.grid()), 2)
import numpy as np # import the Bokeh plotting tools from bokeh import plotting as bp # as in the above example, load decays.csv into a numpy array decaydata = np.loadtxt("decays2.csv", delimiter=",", skiprows=1) # provide handles for the x and y columns time = decaydata[:, 0] decays = decaydata[:, 1] bp.output_file("decays.html", title="bokeh_decay.py example") bp.figure(tools="pan,wheel_zoom,box_zoom,reset,previewsave") bp.line(time, decays, x_axis_label="Time (s)", y_axis_label="Decays (#)", color="#1F78B4", legend="Decays per second") bp.curplot().title = "Decays" bp.grid().grid_line_alpha = 0.3 bp.show() # open a browser
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 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 predict_forcast(self, DF, sc, sc_y, target_testDF, regressor, X_train, X_test, y_train, y_test, forcast_ip_scaled, header, report_dict): self.report_dict = report_dict predicted_stock_price = regressor.predict( X_test) #This should be from row no 2 to 9 if (len(predicted_stock_price.shape)) < 2: predicted_stock_price = np.reshape( predicted_stock_price, (predicted_stock_price.shape[0], 1)) predicted_stock_price = sc.inverse_transform( predicted_stock_price).round(2) forcast_target_price = regressor.predict( forcast_ip_scaled) #This should give row no 10 for 9th row input if len(forcast_target_price.shape) < 2: forcast_target_price = np.reshape( forcast_target_price, (forcast_target_price.shape[0], 1)) forcast_target_price = sc.inverse_transform( forcast_target_price).round(2) #This should give row no 10 print(__name__, "predict_forcast1", forcast_target_price) y_test = sc.inverse_transform(y_test) # Creating Data Frame for visualisation and storage. if target_testDF.shape[0] > predicted_stock_price.shape[0]: print('target_testDF.shape greater', target_testDF.shape[0], predicted_stock_price.shape[0]) target_testDF = target_testDF.iloc[1:, :] elif target_testDF.shape[0] < predicted_stock_price.shape[0]: print('target_testDF.shape less', target_testDF.shape[0], predicted_stock_price.shape[0]) predicted_stock_price = predicted_stock_price.iloc[1:, :] target_testDF['predicted price'] = predicted_stock_price df = DF.iloc[:, 0:1].copy() df = df.rename(columns={'Close': 'Historic Price'}) target_testDF['y_test'] = y_test #result = pd.concat([df, target_testDF], axis=1) #result = result.rename(columns={'Close': 'Real test Price'}) print(self.flag) if not self.flag: #Get last n dates dates = DF.tail(n=self.predict_days).index else: # Get future n dates for forcased values dates = fr.get_forcasted_dates((target_testDF.index[-3:]), self.predict_days) fs_df = pd.DataFrame(index=dates) fs_df['forcast_stock_price'] = forcast_target_price.round(2) print('predict', predicted_stock_price[-1]) print('ytest', y_test[-1]) print('forcast', fs_df['forcast_stock_price'][0]) #result = pd.concat([df, fs_df], axis=1) if not self.flag: actual_price = DF.iloc[-self.predict_days:, 0:1].rename( columns={'Close': 'ActualPrice'}) #result = pd.concat([result, actual_price], axis=1) print('actual', actual_price['ActualPrice'][0]) try: self.report_dict = fr.create_report( self.report_dict, header, 'Dates', fs_df.index.values.astype('datetime64[m]')) self.report_dict = fr.create_report( self.report_dict, header, 'Forcasted', fs_df['forcast_stock_price'].tolist()) if not self.flag: self.report_dict = fr.create_report( self.report_dict, header, 'Actual', actual_price['ActualPrice'].values.tolist()) except Exception as e: print('reporterror', e) print('forcast_stock_price for {0}-D-{2} is {1} for {3}'.format( self.predict_days, fs_df['forcast_stock_price'].tolist(), header, fs_df.index.values.astype('datetime64[m]'))) if self.flag: self.report_dict[header].to_csv(final_reportpath, mode='a', header=False, index=False) else: self.report_dict[header].to_csv(reportpath, mode='a', header=False, index=False) shutil.copyfile(reportpath, techreport) #Visualising the results width = 18 height = 10 try: #result.plot(legend=True, title='Stock Price Prediction ' + header, figsize=(width, height)) plt.grid(color='b', linestyle='--', linewidth=1) plt.show() if self.flag: imgpath = os.path.join(repobasepath, 'finalimgs') imgpath = os.path.join( imgpath, str(date.today()) + header + '_final.png') else: imgpath = os.path.join(repobasepath, 'imgs') imgpath = os.path.join(imgpath, str(date.today()) + header + '.png') plt.savefig(imgpath) except Exception as e: print(e)
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()
import numpy as np # import the Bokeh plotting tools from bokeh import plotting as bp # as in the above example, load decays.csv into a numpy array decaydata = np.loadtxt('decays.csv', delimiter=",", skiprows=1) # provide handles for the x and y columns time = decaydata[:, 0] decays = decaydata[:, 1] bp.output_file("decays.html", title="bokeh_decay.py example") bp.figure(tools="pan,wheel_zoom,box_zoom,reset,previewsave") bp.line(time, decays, x_axis_label="Time (s)", y_axis_label="Decays (#)", color='#1F78B4', legend='Decays per second') bp.curplot().title = "Decays" bp.grid().grid_line_alpha = 0.3 bp.show() # open a browser
def make_spectrogram(): plot_kw = dict( tools="", min_border=1, h_symmetry=False, v_symmetry=False, toolbar_location=None ) freq = VBox( children=[ Paragraph(text="Freq Range"), Slider(orientation="vertical", start=1, end=MAX_FREQ, value=MAX_FREQ, step=1, name="freq") ] ) gain = VBox( children=[ Paragraph(text="Gain"), Slider(orientation="vertical", start=1, end=20, value=1, step=1, name="gain") ] ) spec_source = ColumnDataSource(data=dict(image=[], x=[])) spec = image_rgba( x='x', y=0, image='image', dw=TILE_WIDTH, dh=MAX_FREQ, cols=TILE_WIDTH, rows=SPECTROGRAM_LENGTH, title=None, source=spec_source, plot_width=800, plot_height=300, x_range=[0, NGRAMS], y_range=[0, MAX_FREQ], dilate=True, name="spectrogram", **plot_kw) spectrum_source = ColumnDataSource(data=dict(x=[], y=[])) spectrum = line( x="x", y="y", line_color="darkblue", title="Power Spectrum", source=spectrum_source, plot_width=800, plot_height=250, x_range=[0, MAX_FREQ], y_range=[10**(-4), 10**3], y_axis_type="log", name="spectrum", **plot_kw) signal_source = ColumnDataSource(data=dict(x=[], y=[])) signal = line( x="x", y="y", line_color="darkblue", title="Signal", source=signal_source, plot_width=800, plot_height=250, x_range=[0, TIMESLICE*1.01], y_range=[-0.1, 0.1], name="signal", **plot_kw) radial_source = ColumnDataSource(data=dict( inner_radius=[], outer_radius=[], start_angle=[], end_angle=[], fill_alpha=[], )) eq = annular_wedge( x=0, y=0, fill_color="#688AB9", fill_alpha="fill_alpha", line_color=None, inner_radius="inner_radius", outer_radius="outer_radius", start_angle="start_angle", end_angle="end_angle", title=None, source=radial_source, plot_width=500, plot_height=520, x_range=[-20, 20], y_range=[-20, 20], name="eq", **plot_kw) grid().grid_line_color=None lines = VBox( children=[spectrum, signal] ) layout = VBox( children = [ HBox(children=[freq, gain, spec]), HBox(children=[lines, eq]) ] ) return layout
def make_spectrogram(): plot_kw = dict(tools="", min_border=1, h_symmetry=False, v_symmetry=False, toolbar_location=None) freq = VBox(children=[ Paragraph(text="Freq Range"), Slider(orientation="vertical", start=1, end=MAX_FREQ, value=MAX_FREQ, step=1, name="freq") ]) gain = VBox(children=[ Paragraph(text="Gain"), Slider(orientation="vertical", start=1, end=20, value=1, step=1, name="gain") ]) spec_source = ColumnDataSource(data=dict(image=[], x=[])) spec = image_rgba(x='x', y=0, image='image', dw=TILE_WIDTH, dh=MAX_FREQ, cols=TILE_WIDTH, rows=SPECTROGRAM_LENGTH, title=None, source=spec_source, plot_width=800, plot_height=300, x_range=[0, NGRAMS], y_range=[0, MAX_FREQ], dilate=True, name="spectrogram", **plot_kw) spectrum_source = ColumnDataSource(data=dict(x=[], y=[])) spectrum = line(x="x", y="y", line_color="darkblue", title="Power Spectrum", source=spectrum_source, plot_width=800, plot_height=250, x_range=[0, MAX_FREQ], y_range=[10**(-4), 10**3], y_axis_type="log", name="spectrum", **plot_kw) signal_source = ColumnDataSource(data=dict(x=[], y=[])) signal = line(x="x", y="y", line_color="darkblue", title="Signal", source=signal_source, plot_width=800, plot_height=250, x_range=[0, TIMESLICE * 1.01], y_range=[-0.1, 0.1], name="signal", **plot_kw) radial_source = ColumnDataSource(data=dict( inner_radius=[], outer_radius=[], start_angle=[], end_angle=[], fill_alpha=[], )) eq = annular_wedge(x=0, y=0, fill_color="#688AB9", fill_alpha="fill_alpha", line_color=None, inner_radius="inner_radius", outer_radius="outer_radius", start_angle="start_angle", end_angle="end_angle", title=None, source=radial_source, plot_width=500, plot_height=520, x_range=[-20, 20], y_range=[-20, 20], name="eq", **plot_kw) grid().grid_line_color = None lines = VBox(children=[spectrum, signal]) layout = VBox(children=[ HBox(children=[freq, gain, spec]), HBox(children=[lines, eq]) ]) return layout
def grid(): return plotting.grid()
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()
# 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"), text=dict(field="number", units="data"), text_font_style="bold", text_font_size="12pt", **text_props) # turn off grid lines - bk.grid().grid_line_color = None bk.grid().grid_line_color = None # show the chart bk.show()
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()
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 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)