def pyramid(): xdr = DataRange1d() ydr = FactorRange(factors=groups) plot = Plot(x_range=xdr, y_range=ydr, plot_width=600, plot_height=500, toolbar_location=None) xaxis = LinearAxis() plot.add_layout(xaxis, 'below') plot.add_layout(CategoricalAxis(), 'left') plot.add_layout(Grid(dimension=0, ticker=xaxis.ticker)) m = HBar(left="value", right=0, y="group", height=1, fill_color="#3B8686") mglyph = plot.add_glyph(source_pyramid_m, m) f = HBar(left=0, right="value", y="group", height=1, fill_color="#CFF09E") fglyph = plot.add_glyph(source_pyramid_f, f) plot.add_layout(Legend(items=[("Male", [mglyph]), ("Female", [fglyph])])) return plot
def make_plot(src): # Blank plot in linear scale p_lin = figure( y_range=FactorRange(factors=list(src.data['no_factors'])), plot_width=700, plot_height=450, title='Bar plot of number of victims injured or killed', y_axis_label='Contributing factor', x_axis_label='No of victims', toolbar_location=None) glyph = HBar(y='no_factors', right="no_victims", left=0, height=0.5, fill_color="#460E61") p_lin.add_glyph(src, glyph) # Blank plot in log scale p_log = figure( y_range=FactorRange(factors=list(src.data['no_factors'])), plot_width=700, plot_height=450, title='Bar plot of no of victims injured or killed', y_axis_label='Contributing factor', x_axis_label='No of victims', x_axis_type='log', toolbar_location=None) glyph = HBar(y='no_factors', right="no_victims", left=0.00001, height=0.5, fill_color="#460E61") p_log.add_glyph(src, glyph) # Hover tool with hline mode hover = HoverTool(tooltips=[('Number of victims', '@no_victims'), ('Contributing Factor', '@no_factors')], mode='hline') p_lin.add_tools(hover) p_log.add_tools(hover) return p_lin, p_log
t_start = df["time_start"].min() t_end = df["time_end"].max() source = ColumnDataSource(df) plot = figure( #title=None, plot_width=max(ceil(8000 * (t_end - t_start)), 300), plot_height=80*n_ranks, #min_border=0, sizing_mode="stretch_both", tools="pan,wheel_zoom, reset", active_scroll="wheel_zoom", ) glyph = HBar(y="rank", right="time_end", left="time_start", height=0.5, fill_color="#b3de69") plot.add_glyph(source, glyph) plot.add_tools( HoverTool(tooltips=[ ("name", "@name"), ("time_start", "@time_start"), ("time_taken", "@time_taken"), ], )) curdoc().add_root(plot) output_file("interactive_test.html") show(plot)
def Fig4(parea, themes): writeTo = "templates/figures/" + parea + "/Figure4.html" output_file(writeTo, mode="inline") #NOTE: mode attribute to run output from a local file. In theory, it can be removed for an online file... #load online stip compass data using pipe '|' separator and skipping the second header (multi-indexing causes problems in the filtering) #url = 'https://stip.oecd.org/assets/downloads/STIP_Survey.csv' url = 'INPUT/STIP-Data-Flatcsv-Apr2020.csv' head_df = pd.read_csv(url, sep='|', nrows=1) themes_df = head_df[themes] themes_df = themes_df.T #themes_df.rename(columns={"0": "THlabel"}, inplace=True) compass_df = pd.read_csv(url, sep='|', skiprows=[1]) d = ['EGY', 'IDN', 'IND', 'MAR', 'MYS', 'SAU', 'SGL', 'SRB', 'URY', 'VNM'] compass_df = compass_df[~compass_df['CountryCode'].isin(d)] compass_df.Tags.fillna("¬", inplace=True) compass_df['theme'] = "" Fig4_df = pd.DataFrame(columns=compass_df.columns) for th in themes: compass_df.loc[compass_df[th] == "1", th] = 1 compass_df.loc[compass_df[th] == 1, 'theme'] = themes_df.loc[th].values[0] Fig4_df = pd.concat([Fig4_df, compass_df[compass_df[th] == 1]]) Fig4_df.drop_duplicates(subset=['InitiativeID', 'YearlyBudgetRange'], inplace=True) Fig4_df['count'] = 1 grouped = Fig4_df.groupby('YearlyBudgetRange')[[ 'count', 'HasBeenEvaluated' ]].sum() groupedkw = Fig4_df.groupby( 'YearlyBudgetRange')['Tags'].apply(lambda x: nltk.FreqDist( nltk.tokenize.regexp_tokenize('¬'.join(x), pattern='¬', gaps=True)) ) kwlist = groupedkw.groupby( level='YearlyBudgetRange').nlargest(10).reset_index( level=0, drop=True).to_frame() kwlist.reset_index(level=1, inplace=True) kwlist.rename(columns={"level_1": "topconcepts"}, inplace=True) kwlist_merged = kwlist.groupby('YearlyBudgetRange')['topconcepts'].apply( list).to_frame() kwlist_merged = kwlist_merged.topconcepts.apply(str).to_frame() grouped = pd.concat([grouped, kwlist_merged], axis=1, sort=True) grouped.sort_values(by='count', ascending=True, inplace=True) budgets_index = [ 'More than 500M', '100M-500M', '50M-100M', '20M-50M', '5M-20M', '1M-5M', 'Less than 1M', 'Not applicable', "Don't know" ] budgets_stip = { 'budgets_links': ['BR15', 'BR14', 'BR13', 'BR12', 'BR11', 'BR10', 'BR9', 'BR16', 'BR1'] } budgets_links_df = pd.DataFrame(data=budgets_stip, index=budgets_index) grouped = grouped.join(budgets_links_df) if parea == "Governance": alink = "TH1" elif parea == "Public research system": alink = "TH2" elif parea == "Innovation in firms and innovative entrepreneurship": alink = "TH3" elif parea == "Science-industry knowledge transfer and sharing": alink = "TH5" elif parea == "Human resources for research and innovation": alink = "TH7" elif parea == "Research and innovation for society": alink = "TH8" else: alink = "TH84" grouped[ 'links'] = "https://stip.oecd.org/ws/STIP/API/getPolicyInitiatives.xqy?format=csv&th=" + str( alink) + "&br-extra=" + grouped['budgets_links'].map(str) source = ColumnDataSource(grouped) budgets = source.data['YearlyBudgetRange'].tolist() p = figure(plot_width=800, plot_height=400, y_range=budgets, tools="tap,pan,wheel_zoom,box_zoom,save,reset") #p.xaxis.major_label_orientation = pi/4 p.hbar(name="myHM", y='YearlyBudgetRange', right='count', left=0, source=source, height=0.50, color='#4292c6') title = "Figure 4. Policies reported by budget range, \"" + parea + "\" policy area" p.title.text = title p.yaxis.axis_label = 'Budget range (in EUR)' #p.xaxis.axis_label = 'Number of policy initiatives reported' p.add_layout( LinearAxis(axis_label='Number of policy initiatives reported'), 'above') p.title.align = 'right' p.title.vertical_align = 'top' p.title.text_font_size = '11pt' p.xaxis.axis_label_text_font_size = "11pt" p.xaxis.axis_label_text_font_style = "normal" p.xaxis.major_label_text_font_size = "10pt" p.yaxis.axis_label_text_font_size = "12pt" p.yaxis.axis_label_text_font_style = "normal" p.yaxis.major_label_text_font_size = "10pt" hover = HoverTool() hover.tooltips = """ <font color="#3eade0">Initiatives:</font> @count <br> <font color="#3eade0">Frequent keywords:</font> @topconcepts <br> <span style="font-weight: bold;">Click to download data</span> """ hover.mode = 'hline' p.add_tools(hover) #Prevent selection on click action to be highlighted renderer = p.select(name="myHM") renderer.nonselection_glyph = HBar(height=0.50, fill_color='#4292c6', line_color='#4292c6') callback = CustomJS(args={ 'source': source, 'title': p.title }, code=""" var idx = source.selected.indices var url = source.data['links'][idx] var temptext = title.text var tempcolor = title.text_color title.text = "Download in progress- this may take up to one minute." title.text_color = "red" fetch(url, { method: 'GET', }).then(function(resp) { return resp.blob(); }).then(function(blob) { const newBlob = new Blob([blob], { type: "text/csv", charset: "UTF-8" }) // IE doesn't allow using a blob object directly as link href // instead it is necessary to use msSaveOrOpenBlob if (window.navigator && window.navigator.msSaveOrOpenBlob) { window.navigator.msSaveOrOpenBlob(newBlob); return; } const data = window.URL.createObjectURL(newBlob); const link = document.createElement('a'); link.dataType = "json"; link.href = data; link.download = "STIP_COMPASS_Policy_Initiatives_Export.csv"; link.dispatchEvent(new MouseEvent('click')); setTimeout(function () { // For Firefox it is necessary to delay revoking the ObjectURL window.URL.revokeObjectURL(data), 60 }); }); setTimeout(function (){ title.text = temptext title.text_color = tempcolor }, 7000) """) #Add click action p.js_on_event(Tap, callback) save(p)
def Fig5(parea, themes): writeTo = "templates/figures/" + parea + "/Figure5.html" output_file(writeTo, mode="inline") #NOTE: mode attribute to run output from a local file. In theory, it can be removed for an online file... #load online stip compass data using pipe '|' separator and skipping the second header (multi-indexing causes problems in the filtering) #url = 'https://stip.oecd.org/assets/downloads/STIP_Survey.csv' url = 'INPUT/STIP-Data-Flatcsv-Apr2020.csv' compass_df = pd.read_csv(url, sep='|', skiprows=[1]) d = ['EGY', 'IDN', 'IND', 'MAR', 'MYS', 'SAU', 'SGL', 'SRB', 'URY', 'VNM'] compass_df = compass_df[~compass_df['CountryCode'].isin(d)] compass_df.Tags.fillna("¬", inplace=True) Fig5_df = pd.DataFrame(columns=compass_df.columns) for th in themes: compass_df.loc[compass_df[th] == "1", th] = 1 Fig5_df = pd.concat([Fig5_df, compass_df[compass_df[th] == 1]]) Fig5_df.drop_duplicates(subset="InitiativeID", inplace=True) Fig5_df['count'] = 1 grouped = Fig5_df.groupby('CoutryLabel')[['count', 'HasBeenEvaluated']].sum() groupedkw = Fig5_df.groupby( 'CoutryLabel')['Tags'].apply(lambda x: nltk.FreqDist( nltk.tokenize.regexp_tokenize('¬'.join(x), pattern='¬', gaps=True)) ) kwlist = groupedkw.groupby(level='CoutryLabel').nlargest(10).reset_index( level=0, drop=True).to_frame() kwlist.reset_index(level=1, inplace=True) kwlist.rename(columns={"level_1": "topconcepts"}, inplace=True) kwlist_merged = kwlist.groupby('CoutryLabel')['topconcepts'].apply( list).to_frame() kwlist_merged = kwlist_merged.topconcepts.apply(str).to_frame() grouped = pd.concat([grouped, kwlist_merged], axis=1, sort=True) ccodes_df = pd.read_csv('INPUT/ccodes.csv', index_col='CoutryLabel') grouped = grouped.join(ccodes_df) grouped.sort_values(by='count', ascending=True, inplace=True) source = ColumnDataSource(grouped) countries = source.data['CoutryLabel'].tolist() p = figure(plot_width=800, plot_height=1200, y_range=countries, tools="tap,pan,wheel_zoom,box_zoom,save,reset") #p.xaxis.major_label_orientation = pi/4 p.hbar(name="myHM", y='CoutryLabel', right='count', left=0, source=source, height=0.50, color='#4292c6') title = "Figure 5. Policy initiatives reported under the \"" + parea + "\" policy area" p.title.text = title p.yaxis.axis_label = 'Country or other reporting entity' #p.xaxis.axis_label = 'Number of initiatives reported' p.add_layout( LinearAxis(axis_label='Number of policy initiatives reported'), 'above') p.title.align = 'right' p.title.vertical_align = 'top' p.title.text_font_size = '11pt' p.xaxis.axis_label_text_font_size = "11pt" p.xaxis.axis_label_text_font_style = "normal" p.xaxis.major_label_text_font_size = "10pt" p.yaxis.axis_label_text_font_size = "12pt" p.yaxis.axis_label_text_font_style = "normal" p.yaxis.major_label_text_font_size = "10pt" hover = HoverTool() hover.tooltips = """ <font color="#3eade0">Initiatives:</font> @count <br> <font color="#3eade0">Frequent keywords:</font> @topconcepts <br> <span style="font-weight: bold;">Click to browse initiatives in STIP Compass</span> """ hover.mode = 'hline' p.add_tools(hover) #Prevent selection on click action to be highlighted renderer = p.select(name="myHM") renderer.nonselection_glyph = HBar(height=0.50, fill_color='#4292c6', line_color='#4292c6') if parea == "Governance": alink = "TH1" elif parea == "Public research system": alink = "TH2" elif parea == "Innovation in firms and innovative entrepreneurship": alink = "TH3" elif parea == "Science-industry knowledge transfer and sharing": alink = "TH5" elif parea == "Human resources for research and innovation": alink = "TH7" elif parea == "Research and innovation for society": alink = "TH8" else: alink = "TH84" compass = "******" + "/themes/" + alink taptool = p.select(type=TapTool) taptool.callback = OpenURL(url=compass) save(p)
source1 = ColumnDataSource({ "x": [1, 2, 3, 4, 5], "y": [1, 2, 3, 4, 5], "who": ["a", "b", "c", "d", "e"] }) source2 = ColumnDataSource({"x": x, "y": y}) source3 = ColumnDataSource({"x": x2, "y": z}) source4 = ColumnDataSource({"y": [2.5], "x": [0.5]}) plot = Plot(width=300, height=300) plot.title = Title(text="Themed glyphs") xaxis = LinearAxis(ticker=BasicTicker(), formatter=BasicTickFormatter()) yaxis = LinearAxis(ticker=BasicTicker(), formatter=BasicTickFormatter()) plot.add_layout(xaxis, "below") plot.add_layout(yaxis, "left") plot.add_glyph(source1, Scatter(x="x", y="y", marker="diamond", size=20)) plot.add_glyph(source1, Text(x=dodge("x", -0.2), y=dodge("y", 0.1), text="who")) plot.add_glyph(source2, Line(x="x", y="y")) plot.add_glyph(source3, Ellipse(x="x", y="y", width=0.2, height=0.3, angle=-0.7)) plot.add_glyph(source4, glyph=HBar(y="y", right="x", height=1.5)) output_file("theme_glyphs.html", title="themed_glyphs.py example") show(plot)
def Fig3(parea, themes): writeTo = "templates/figures/" + parea + "/Figure3.html" output_file(writeTo, mode="inline") #NOTE: mode attribute to run output from a local file. In theory, it can be removed for an online file... #load online stip compass data using pipe '|' separator and skipping the second header (multi-indexing causes problems in the filtering) #url = 'https://stip.oecd.org/assets/downloads/STIP_Survey.csv' url = 'INPUT/STIP-Data-Flatcsv-Apr2020.csv' head_df = pd.read_csv(url, sep='|', nrows=1) themes_df = head_df[themes] themes_df = themes_df.T #themes_df.rename(columns={"0": "THlabel"}, inplace=True) compass_df = pd.read_csv(url, sep='|', skiprows=[1]) d = ['EGY', 'IDN', 'IND', 'MAR', 'MYS', 'SAU', 'SGL', 'SRB', 'URY', 'VNM'] compass_df = compass_df[~compass_df['CountryCode'].isin(d)] compass_df.Tags.fillna("¬", inplace=True) compass_df['theme'] = "" Fig3_df = pd.DataFrame(columns=compass_df.columns) for th in themes: compass_df.loc[compass_df[th] == "1", th] = 1 compass_df.loc[compass_df[th] == 1, 'theme'] = themes_df.loc[th].values[0] Fig3_df = pd.concat([Fig3_df, compass_df[compass_df[th] == 1]]) Fig3_df.drop_duplicates(subset=['InitiativeID', 'InstrumentTypeLabel'], inplace=True) Fig3_df['count'] = 1 grouped = Fig3_df.groupby('InstrumentTypeLabel')[[ 'count', 'HasBeenEvaluated' ]].sum() groupedkw = Fig3_df.groupby( 'InstrumentTypeLabel')['Tags'].apply(lambda x: nltk.FreqDist( nltk.tokenize.regexp_tokenize('¬'.join(x), pattern='¬', gaps=True)) ) kwlist = groupedkw.groupby( level='InstrumentTypeLabel').nlargest(10).reset_index( level=0, drop=True).to_frame() kwlist.reset_index(level=1, inplace=True) kwlist.rename(columns={"level_1": "topconcepts"}, inplace=True) kwlist_merged = kwlist.groupby('InstrumentTypeLabel')['topconcepts'].apply( list).to_frame() kwlist_merged = kwlist_merged.topconcepts.apply(str).to_frame() grouped = pd.concat([grouped, kwlist_merged], axis=1, sort=True) grouped.sort_values(by='count', ascending=True, inplace=True) inst_index = [ 'Centres of excellence grants', 'Corporate tax relief for R&D and innovation', 'Creation or reform of governance structure or public body', 'Debt guarantees and risk sharing schemes', 'Dedicated support to research infrastructures', 'Emerging technology regulation', 'Equity financing', 'Fellowships and postgraduate loans and scholarships', 'Formal consultation of stakeholders or experts', 'Grants for business R&D and innovation', 'Horizontal STI coordination bodies', 'Information services and access to datasets', 'Innovation vouchers', 'Institutional funding for public research', 'Intellectual property regulation and incentives', 'Labour mobility regulation and incentives', 'Loans and credits for innovation in firms', 'National strategies, agendas and plans', 'Networking and collaborative platforms', 'Policy intelligence (e.g. evaluations, benchmarking and forecasts)', 'Procurement programmes for R&D and innovation', 'Project grants for public research', 'Public awareness campaigns and other outreach activities', 'Regulatory oversight and ethical advice bodies', 'Science and innovation challenges, prizes and awards', 'Standards and certification for technology development and adoption', 'Technology extension and business advisory services' ] inst_stip = { 'inst_links': [ 'Centres_of_excellence_grants', 'Tax_relief', 'Creation_or_reform_of_governance_structure_or_public_body', 'Debt_guarantees_and_risk_sharing_schemes', 'Dedicated_support_to_new_research_infrastructures', 'Emerging_technology_regulation', 'Equity_financing', 'Postgraduate_loans_scholarships_and_fellowships', 'Public_consultation_of_stakeholders', 'Project_grants_for_business_RD_and_innovation', 'Horizontal_STI_coordination_bodies', 'Information_services_and_databases', 'Innovation_vouchers', 'Institutional_funding_for_public_research', 'Intellectual_property_regulation_and_incentives', 'Labour_mobility_regulation_and_incentives', 'Loans_and_credits_for_innovation_in_firms', 'National_strategies_agendas_and_plans', 'Networking_and_collaborative_platforms', 'Policy_intelligence', 'Procurement_programmes_for_RD_and_innovation', 'Project_grants_for_public_research', 'Public_awareness_campaigns_and_other_outreach_activities', 'Regulatory_oversight_and_ethical_advice_bodies', 'Innovation_challenges_prizes_and_awards', 'Standards_and_certification_for_technology_development_and_adoption', 'Technology_transfer_and_business_advisory_services' ] } inst_links_df = pd.DataFrame(data=inst_stip, index=inst_index) grouped = grouped.join(inst_links_df) if parea == "Governance": alink = "TH1" elif parea == "Public research system": alink = "TH2" elif parea == "Innovation in firms and innovative entrepreneurship": alink = "TH3" elif parea == "Science-industry knowledge transfer and sharing": alink = "TH5" elif parea == "Human resources for research and innovation": alink = "TH7" elif parea == "Research and innovation for society": alink = "TH8" else: alink = "TH84" grouped[ 'links'] = "https://stip.oecd.org/ws/STIP/API/getPolicyInitiatives.xqy?format=csv&pi=" + grouped[ 'inst_links'].map(str) + "&th=" + str( alink) + "&br-extra=none,BR16,BR1&br=BR9,BR15" source = ColumnDataSource(grouped) instruments = source.data['InstrumentTypeLabel'].tolist() p = figure(plot_width=800, plot_height=800, y_range=instruments, tools="tap,pan,wheel_zoom,box_zoom,save,reset") #p.xaxis.major_label_orientation = pi/4 p.hbar(name="myHM", y='InstrumentTypeLabel', right='count', left=0, source=source, height=0.50, color='#4292c6') title = "Figure 3. Types of instruments reported, \"" + parea + "\" policy area" p.title.text = title p.yaxis.axis_label = 'Type of policy instrument' #p.xaxis.axis_label = 'Number of instances reported' p.add_layout(LinearAxis(axis_label='Number of instances reported'), 'above') p.title.align = 'right' p.title.vertical_align = 'top' p.title.text_font_size = '11pt' p.xaxis.axis_label_text_font_size = "11pt" p.xaxis.axis_label_text_font_style = "normal" p.xaxis.major_label_text_font_size = "10pt" p.yaxis.axis_label_text_font_size = "12pt" p.yaxis.axis_label_text_font_style = "normal" p.yaxis.major_label_text_font_size = "10pt" hover = HoverTool() hover.tooltips = """ <font color="#3eade0">Instruments:</font> @count <br> <font color="#3eade0">Frequent keywords:</font> @topconcepts <br> <span style="font-weight: bold;">Click to download data</span> """ hover.mode = 'hline' p.add_tools(hover) #Prevent selection on click action to be highlighted renderer = p.select(name="myHM") renderer.nonselection_glyph = HBar(height=0.50, fill_color='#4292c6', line_color='#4292c6') callback = CustomJS(args={ 'source': source, 'title': p.title }, code=""" var idx = source.selected.indices var url = source.data['links'][idx] var temptext = title.text var tempcolor = title.text_color title.text = "Download in progress- this may take up to one minute." title.text_color = "red" fetch(url, { method: 'GET', }).then(function(resp) { return resp.blob(); }).then(function(blob) { const newBlob = new Blob([blob], { type: "text/csv", charset: "UTF-8" }) // IE doesn't allow using a blob object directly as link href // instead it is necessary to use msSaveOrOpenBlob if (window.navigator && window.navigator.msSaveOrOpenBlob) { window.navigator.msSaveOrOpenBlob(newBlob); return; } const data = window.URL.createObjectURL(newBlob); const link = document.createElement('a'); link.dataType = "json"; link.href = data; link.download = "STIP_COMPASS_Policy_Initiatives_Export.csv"; link.dispatchEvent(new MouseEvent('click')); setTimeout(function () { // For Firefox it is necessary to delay revoking the ObjectURL window.URL.revokeObjectURL(data), 60 }); }); setTimeout(function (){ title.text = temptext title.text_color = tempcolor }, 7000) """) #Add click action p.js_on_event(Tap, callback) save(p)
def init_emotions_points(type_filter): df_emotions = general_dict['individual_df'][type_filter][[ 'rec_sc', 'eas_sc' ]] rec_score = round(df_emotions['rec_sc'].mean() * 100, 2) easy_score = round(df_emotions['eas_sc'].mean() * 100, 2) plot_name = type_filter + '_emotions_plot' create_emotions_plot_layout(plot_name) if 'emotions_rec_score' not in general_dict: general_dict['emotions_rec_score'] = {} if 'emotions_easy_score' not in general_dict: general_dict['emotions_easy_score'] = {} general_dict['emotions_rec_score'][type_filter] = ColumnDataSource( dict(right=[rec_score], left=[rec_score], text=[rec_score], text_x=[rec_score + 2.5])) general_dict['emotions_easy_score'][type_filter] = ColumnDataSource( dict(right=[easy_score], left=[easy_score], text=[easy_score], text_x=[easy_score + 2.5])) citation = LabelSet(x='text_x', y=0.43, text='text', text_font_size='1vw', render_mode='css', text_color="black", source=general_dict['emotions_rec_score'][type_filter]) general_dict[plot_name].add_layout(citation) citation = LabelSet( x='text_x', y=0.03, text='text', text_font_size='1vw', render_mode='css', text_color="black", source=general_dict['emotions_easy_score'][type_filter]) general_dict[plot_name].add_layout(citation) general_dict[plot_name].add_glyph( general_dict['emotions_rec_score'][type_filter], HBar(y=0.4, right='right', left='left', height=0.2, fill_color="#1a1c20", line_width=4), name='rec_s') general_dict[plot_name].add_glyph( general_dict['emotions_easy_score'][type_filter], HBar(y=0.0, right='right', left='left', height=0.2, fill_color="#1a1c20", line_width=4), name='easy_s') return general_dict[plot_name]
def create_emotions_plot_layout(points_plot_name): general_dict[points_plot_name] = Plot(title=None, plot_width=600, plot_height=180, min_border=0, toolbar_location=None, outline_line_color=None, output_backend="webgl") general_dict[points_plot_name].add_glyph( HBar(y=0.4, right=0, left=-100, height=0.2, fill_color="#931a25", line_width=0)) general_dict[points_plot_name].add_glyph( HBar(y=0.0, right=0, left=-100, height=0.2, fill_color="#931a25", line_width=0)) general_dict[points_plot_name].add_glyph( HBar(y=0.4, right=30, left=0, height=0.2, fill_color="#ffc93c", line_width=0)) general_dict[points_plot_name].add_glyph( HBar(y=0.0, right=30, left=0, height=0.2, fill_color="#ffc93c", line_width=0)) general_dict[points_plot_name].add_glyph( HBar(y=0.4, right=70, left=30, height=0.2, fill_color="#b3de69", line_width=0)) general_dict[points_plot_name].add_glyph( HBar(y=0.0, right=70, left=30, height=0.2, fill_color="#b3de69", line_width=0)) general_dict[points_plot_name].add_glyph( HBar(y=0.4, right=100, left=70, height=0.2, fill_color="#158467", line_width=0)) general_dict[points_plot_name].add_glyph( HBar(y=0.0, right=100, left=70, height=0.2, fill_color="#158467", line_width=0)) # Create labels citation = Label(y=0.55, text='Recommendation', text_align='center', render_mode='css', text_color="#4c4c4c", text_font_style='bold') general_dict[points_plot_name].add_layout(citation) citation = Label(y=0.16, text='Easiness', text_align='center', render_mode='css', text_color="#4c4c4c", text_font_style='bold') general_dict[points_plot_name].add_layout(citation) citation = Label(x=-86, y=-0.2, text='NEEDS IMPROVEMENT', text_font_size='1vw', render_mode='css', text_color="#931a25") general_dict[points_plot_name].add_layout(citation) citation = Label(x=7, y=-0.2, text='GOOD', text_font_size='1vw', render_mode='css', text_color="#ffc93c") general_dict[points_plot_name].add_layout(citation) citation = Label(x=40, y=-0.2, text='GREAT', text_font_size='1vw', render_mode='css', text_color="#b3de69") general_dict[points_plot_name].add_layout(citation) citation = Label(x=68, y=-0.2, text='EXCELLENT', text_font_size='1vw', render_mode='css', text_color="#158467") general_dict[points_plot_name].add_layout(citation) citation = Label(x=-103, y=0.23, text='-100', text_font_size='1vw', render_mode='css', text_color="#4c4c4c") general_dict[points_plot_name].add_layout(citation) citation = Label(x=93, y=0.23, text='100', text_font_size='1vw', render_mode='css', text_color="#4c4c4c") general_dict[points_plot_name].add_layout(citation)
def bokehDrawArray(dataFrame, query, figureArray, **kwargs): """ Wrapper bokeh draw array of figures :param dataFrame: - input data frame :param query: - query :param figureArray: - figure array :param kwargs: :return: variable list: * pAll * handle * source * plotArray See example test: RootInteractive/InteractiveDrawing/bokeh/test_bokehDrawSA.py """ options = { 'line': -1, 'size': 2, 'tools': 'pan,box_zoom, wheel_zoom,box_select,lasso_select,reset,save', 'tooltips': [], 'y_axis_type': 'auto', 'x_axis_type': 'auto', 'plot_width': 600, 'plot_height': 400, 'errX': '', 'errY': '', 'commonX': 0, 'commonY': 0, 'ncols': -1, 'layout': '', 'widgetLayout': '', 'palette': Spectral6, "marker": "square", "markers": bokehMarkers, "color": "#000000", "colors": 'Category10', "colorZvar": '', "filter": '', 'doDraw': 0 } options.update(kwargs) dfQuery = dataFrame.query(query) if hasattr(dataFrame, 'metaData'): dfQuery.metaData = dataFrame.metaData logging.info(dfQuery.metaData) # Check/resp. load derived variables i: int for i, variables in enumerate(figureArray): if len(variables) > 1 and variables[0] is not "table": lengthX = len(variables[0]) lengthY = len(variables[1]) length = max(len(variables[0]), len(variables[1])) if len(variables) > 2: optionLocal = options.copy() optionLocal.update(variables[2]) else: optionLocal = options for j in range(0, length): dfQuery, varNameX = pandaGetOrMakeColumn( dfQuery, variables[0][j % lengthX]) dfQuery, varNameY = pandaGetOrMakeColumn( dfQuery, variables[1][j % lengthY]) if ('errY' in optionLocal.keys()) & (optionLocal['errY'] != ''): seriesErrY = dfQuery.eval(optionLocal['errY']) if varNameY + '_lower' not in dfQuery.columns: seriesLower = dfQuery[varNameY] - seriesErrY dfQuery[varNameY + '_lower'] = seriesLower if varNameY + '_upper' not in dfQuery.columns: seriesUpper = dfQuery[varNameY] + seriesErrY dfQuery[varNameY + '_upper'] = seriesUpper if ('errX' in optionLocal.keys()) & (optionLocal['errX'] != ''): seriesErrX = dfQuery.eval(optionLocal['errX']) if varNameX + '_lower' not in dfQuery.columns: seriesLower = dfQuery[varNameX] - seriesErrX dfQuery[varNameX + '_lower'] = seriesLower if varNameX + '_upper' not in dfQuery.columns: seriesUpper = dfQuery[varNameX] + seriesErrX dfQuery[varNameX + '_upper'] = seriesUpper try: source = ColumnDataSource(dfQuery) except: logging.error("Invalid source:", source) # define default options plotArray = [] colorAll = all_palettes[options['colors']] if isinstance(figureArray[-1], dict): options.update(figureArray[-1]) for i, variables in enumerate(figureArray): logging.info(i, variables) if isinstance(variables, dict): continue if variables[0] == 'table': TOptions = {'include': '', 'exclude': ''} if len(variables) > 1: TOptions.update(variables[1]) plotArray.append( makeBokehDataTable(dfQuery, source, TOptions['include'], TOptions['exclude'])) continue xAxisTitle = "" yAxisTitle = "" # zAxisTitle = "" plotTitle = "" for varY in variables[1]: if hasattr(dfQuery, "meta"): yAxisTitle += dfQuery.meta.metaData.get( varY + ".AxisTitle", varY) else: yAxisTitle += varY yAxisTitle += ',' for varX in variables[0]: if hasattr(dfQuery, "meta"): xAxisTitle += dfQuery.meta.metaData.get( varX + ".AxisTitle", varX) else: xAxisTitle += varX xAxisTitle += ',' xAxisTitle = xAxisTitle[:-1] yAxisTitle = yAxisTitle[:-1] plotTitle += yAxisTitle + " vs " + xAxisTitle figureI = figure(plot_width=options['plot_width'], plot_height=options['plot_height'], title=plotTitle, tools=options['tools'], tooltips=options['tooltips'], x_axis_type=options['x_axis_type'], y_axis_type=options['y_axis_type']) figureI.xaxis.axis_label = xAxisTitle figureI.yaxis.axis_label = yAxisTitle # graphArray=drawGraphArray(df, variables) lengthX = len(variables[0]) lengthY = len(variables[1]) length = max(len(variables[0]), len(variables[1])) color_bar = None mapperC = None for i in range(0, length): dfQuery, varNameY = pandaGetOrMakeColumn(dfQuery, variables[1][i % lengthY]) dummy, varNameX = pandaGetOrMakeColumn(dfQuery, variables[0][i % lengthX]) optionLocal = copy.copy(options) optionLocal['color'] = colorAll[max(length, 4)][i] optionLocal['marker'] = optionLocal['markers'][i] if len(variables) > 2: logging.info("Option", variables[2]) optionLocal.update(variables[2]) varX = variables[0][i % lengthX] varY = variables[1][i % lengthY] if (len(optionLocal["colorZvar"]) > 0): logging.info(optionLocal["colorZvar"]) varColor = optionLocal["colorZvar"] mapperC = linear_cmap(field_name=varColor, palette=options['palette'], low=min(dfQuery[varColor]), high=max(dfQuery[varColor])) optionLocal["color"] = mapperC color_bar = ColorBar(color_mapper=mapperC['transform'], width=8, location=(0, 0), title=varColor) # zAxisTitle +=varColor + "," # view = CDSView(source=source, filters=[GroupFilter(column_name=optionLocal['filter'], group=True)]) figureI.scatter(x=varNameX, y=varNameY, fill_alpha=1, source=source, size=optionLocal['size'], color=optionLocal["color"], marker=optionLocal["marker"], legend_label=varY + " vs " + varX) if ('errX' in optionLocal.keys()) & (optionLocal['errX'] != ''): errors = HBar(y=varNameY, left=varNameX + "_lower", right=varNameX + "_upper", line_color=optionLocal["color"]) figureI.add_glyph(source, errors) if ('errY' in optionLocal.keys()) & (optionLocal['errY'] != ''): errors = VBar(x=varNameX, bottom=varNameY + "_lower", top=varNameY + "_upper", line_color=optionLocal["color"]) figureI.add_glyph(source, errors) # errors = Band(base=varNameX, lower=varNameY+"_lower", upper=varNameY+"_upper",source=source) # figureI.add_layout(errors) if color_bar != None: figureI.add_layout(color_bar, 'right') figureI.legend.click_policy = "hide" # zAxisTitle=zAxisTitle[:-1] # if(len(zAxisTitle)>0): # plotTitle += " Color:" + zAxisTitle # figureI.title = plotTitle plotArray.append(figureI) if isinstance(options['layout'], list): pAll = processBokehLayoutArray(options['layout'], plotArray) layoutList = [pAll] else: if len(options['layout']) > 0: # make figure according layout x, layoutList, optionsLayout = processBokehLayout( options["layout"], plotArray) pAll = gridplotRow(layoutList, **optionsLayout) if options['doDraw'] > 0: show(pAll) return pAll, source, layoutList, dfQuery
N = 9 y = np.linspace(-2, 2, N) x = y**2 source = ColumnDataSource(dict( y=y, right=x, )) plot = Plot(title=None, width=300, height=300, min_border=0, toolbar_location=None) glyph = HBar(y="y", right="right", left=0, height=0.5, fill_color="#b3de69") plot.add_glyph(source, glyph) xaxis = LinearAxis() plot.add_layout(xaxis, 'below') yaxis = LinearAxis() plot.add_layout(yaxis, 'left') plot.add_layout(Grid(dimension=0, ticker=xaxis.ticker)) plot.add_layout(Grid(dimension=1, ticker=yaxis.ticker)) curdoc().add_root(plot) show(plot)
source_data = ColumnDataSource(data=dict(x=x, y=y, timestamp=d_data["date"] + ", " + d_data["year"].apply(str))) pred_data = ColumnDataSource( data=dict(x=x, y=[0, ] * len(y), timestamp=d_data["date"] + ", " + d_data["year"].apply(str))) plot_mood_scatter.scatter('x', 'y', source=source_data) xrange_data = Range1d(bounds=[None, None], start=0, end=len(y)) yrange_data = Range1d(bounds=[None, None], start=Y_MIN, end=Y_MAX) plot_mood_scatter.x_range = xrange_data plot_mood_scatter.y_range = yrange_data # Set up bar graph source_bars = ColumnDataSource(dict(y=d_data["mood"].value_counts().index, right=d_data["mood"].value_counts())) pred_line = ColumnDataSource(dict(y=d_data["mood"].value_counts().index, x=[0, ] * len(d_data["mood"].value_counts()))) hbar_glyph = HBar(y="y", right="right", left=0, height=0.3, fill_color='#1d76B4') prebar_glyph = Line(y="y", x="x", line_color='red', line_width=3) plot_mood_bar.add_glyph(source_bars, hbar_glyph) plot_mood_bar.yaxis.major_label_overrides = rml.MOOD_INT_TO_STR plot_mood_bar.ygrid.grid_line_color = None # Set up feature embeddings display tsne_results = rml.tsne_projection(d_features) tsne = ColumnDataSource(data=dict(x=tsne_results[:, 0], y=tsne_results[:, 1], mood=d_data["mood"])) tsne_min, tsne_max = rml.np.min(tsne), rml.np.max(tsne) plot_feature_scatter.scatter('x', 'y', source=tsne, fill_color=linear_cmap('mood', 'Viridis256', -1, 1)) # Callbacks def update_plot(*args, **kwargs): """ Update all UI graphs based on new UI state """