legend = Legend(items=[(colors[0], [wedge1]), (colors[1], [wedge2]), (colors[2], [wedge3]), (colors[3], [wedge4]), (colors[4], [wedge5])], location="bottom_right", name="the_legend") p.add_layout(legend, 'right') labels = LabelSet(x='x', y='y', text='colors', level='glyph', source=pie_label_data, render_mode='canvas', text_color='black', name="the_pie_labels") p.add_layout(labels) p.xaxis.visible = False p.yaxis.visible = False p.grid[0].visible = False p.grid[1].visible = False p.outline_line_color = None # Export to HTML, PNG, and get bbox data data = export_png_and_data(p, "pie_glyphs_legend.png", "pie_glyphs_legend.html") with open("pie_glyphs_legend.json", "w") as f: json.dump(data, f)
from bokeh.io import export_png_and_data # Custom function from bokeh.plotting import figure if __name__ == "__main__": # Set up the plot categories = ['cat_a','cat_b','cat_c'] p = figure(plot_width=400, plot_height=400, title="title_title", toolbar_location=None, x_range=categories) p.vbar(x=categories, width=0.5, bottom=0, top=[1.2, 2.5, 3.7], color="firebrick", name="the_bars") # Set identifiers for the figure elements p.xaxis.name = "the_xaxis" p.xaxis.axis_label = "xaxis_label" p.yaxis.name = "the_yaxis" p.yaxis.axis_label = "yaxis_label" p.title.name = "the_title" p.xaxis.major_label_orientation = "vertical" if p.grid[0].dimension == 0: p.grid[0].name = "the_x_gridlines" p.grid[1].name = "the_y_gridlines" else: p.grid[0].name = "the_y_gridlines" p.grid[1].name = "the_x_gridlines" # Export to HTML, PNG, and get bbox data data = export_png_and_data(p, "vbar_categorical.png", "vbar_categorical.html") with open("vbar_categorical.json", "w") as f: json.dump(data, f)
background_fill_color=None, border_line_color=None, background_fill_alpha=0 ) # Control where the legend appears # Inside if LEGEND_INSIDE: p.add_layout(legend) # Outside else: p.add_layout(legend, 'right') p.xaxis.name = "the_xaxis" p.xaxis.axis_label = "xaxis_label" p.yaxis.name = "the_yaxis" p.yaxis.axis_label = "yaxis_label" p.title.name = "the_title" if p.grid[0].dimension == 0: p.grid[0].name = "the_x_gridlines" p.grid[1].name = "the_y_gridlines" else: p.grid[0].name = "the_y_gridlines" p.grid[1].name = "the_x_gridlines" # Export to HTML, PNG, and get bbox data data = export_png_and_data(p, "scatter_plot_legend.png", "scatter_plot_legend.html") with open("scatter_plot_legend.json", "w") as f: json.dump(data, f)
def generate_figures(source_data_json, destination_directory, add_bboxes=False, supplied_webdriver=None): # Setup dest dirs qa_json_dir = os.path.join(destination_directory, "json_qa") annotations_json_dir = os.path.join(destination_directory, "json_annotations") html_dir = destination_directory png_dir = os.path.join(destination_directory, "png") dirs = [destination_directory, qa_json_dir, annotations_json_dir, png_dir] if add_bboxes: bbox_img_dir = os.path.join(destination_directory, "bbox_png") dirs.append(bbox_img_dir) for dirpath in dirs: if not os.path.exists(dirpath): os.mkdir(dirpath) # Create web driver if supplied_webdriver: webdriver = supplied_webdriver else: webdriver = seldriver.PhantomJS() # Read in the synthetic data with open(source_data_json, 'r') as f: source_data_json = json.load(f) for fig_id, source in tqdm(iter(enumerate(source_data_json['data'])), total=len(source_data_json['data']), desc="Plotting figures"): point_sets = source['data'] fig = None fig_type = source['type'] if fig_type == 'vbar_categorical': fig = VBarGraphCategorical(point_sets[0], source['visuals']) elif fig_type == 'hbar_categorical': fig = HBarGraphCategorical(point_sets[0], source['visuals']) elif fig_type == 'line': fig = LinePlot(point_sets, source['visuals']) elif fig_type == 'dot_line': fig = DotLinePlot(point_sets, source['visuals']) elif fig_type == 'pie': fig = Pie(point_sets[0], source['visuals']) if not fig: continue html_file = os.path.join(html_dir, "%d_%s.html" % (fig_id, fig_type)) png_file = os.path.join(png_dir, "%d_%s.png" % (fig_id, fig_type)) # Export to HTML, PNG, and get rendered data rendered_data = export_png_and_data(fig.figure, png_file, html_file, webdriver) all_plot_data = combine_source_and_rendered_data(source, rendered_data) qa_json_file = os.path.join(qa_json_dir, "%s_%s.json" % (fig_id, fig_type)) annotations_json_file = os.path.join( annotations_json_dir, "%d_%s_annotations.json" % (fig_id, fig_type)) for qa in source['qa_pairs']: qa['image'] = os.path.basename(png_file) qa['annotations'] = os.path.basename(annotations_json_file) with open(qa_json_file, 'w') as f: json.dump( { 'qa_pairs': source['qa_pairs'], 'total_distinct_questions': source_data_json['total_distinct_questions'], 'total_distinct_colors': source_data_json['total_distinct_colors'] }, f) with open(annotations_json_file, 'w') as f: json.dump(all_plot_data, f) if add_bboxes: all_plot_data['image_index'] = fig_id generate_all_images_with_bboxes_for_plot(all_plot_data, png_file, bbox_img_dir, 'red', load_image=True) # Cleanup os.remove(html_file) # Kill the newly created webdriver if not supplied_webdriver: webdriver.service.process.send_signal(signal.SIGTERM) try: RemoteWebDriver.quit(webdriver) except: pass
toolbar_location=None) p.add_glyph( segment_data, Segment(x0="x_start", y0="y_start", x1="x_end", y1="y_end", line_color="#f4a582", line_width=3, name="the_segments")) p.xaxis.name = "the_xaxis" p.xaxis.axis_label = "xaxis_label" p.yaxis.name = "the_yaxis" p.yaxis.axis_label = "yaxis_label" p.title.name = "the_title" if p.grid[0].dimension == 0: p.grid[0].name = "the_x_gridlines" p.grid[1].name = "the_y_gridlines" else: p.grid[0].name = "the_y_gridlines" p.grid[1].name = "the_x_gridlines" # Export to HTML, PNG, and get bbox data data = export_png_and_data(p, "segments.png", "segments.html") #print data with open("segments.json", "w") as f: json.dump(data, f)