def plot_Scatterplot(): plotname = inspect.stack()[0][3][5:] pandas_bokeh.output_file(os.path.join(PLOT_DIR, f"{plotname}.html")) df = df_iris() df = df.sample(frac=1) #Create Bokeh-Table with DataFrame: from bokeh.models.widgets import DataTable, TableColumn from bokeh.models import ColumnDataSource data_table = DataTable( columns=[TableColumn(field=Ci, title=Ci) for Ci in df.columns], source=ColumnDataSource(df.head(10)), ) #Create Scatterplot: p_scatter = df.plot_bokeh.scatter( x="petal length (cm)", y="sepal width (cm)", category="species", title="Iris DataSet Visualization", show_figure=False) #Combine Div and Scatterplot via grid layout: pandas_bokeh.plot_grid([[data_table, p_scatter]], plot_width=400, plot_height=350)
def gen_template(dataframe, outputfile, w, h): output_file(outputfile) df_table = DataTable( columns=[TableColumn(field=Ci, title=Ci) for Ci in dataframe.columns], source=ColumnDataSource(dataframe), width=1500, height=200, ) x = dataframe[['Country', 'TotalCases']] i = x.sort_values("TotalCases", axis=0, ascending=False) i = pd.DataFrame(i).set_index("Country") i_bar = i.plot_bokeh(kind="bar", ylabel="Total Cases", title="", show_figure=False, color='orange') y = dataframe[['Country', 'TotalDeaths']] j = y.sort_values("TotalDeaths", axis=0, ascending=False) j = pd.DataFrame(j).set_index("Country") j_bar = j.plot_bokeh(kind="bar", ylabel="Total Deaths", title="", show_figure=False, color='red') pandas_bokeh.plot_grid([[df_table], [i_bar], [j_bar]], plot_width=w, plot_height=h)
def graph_AllVariables(self, show_plot = True): ''' Returns a HTML graph of the variables that the sensor measures. If the database is Flux, it also gives a graph of the flows ''' if self.database == 'Raw': fig_ux_uy = self.graph_TimeSerie(self.data, ['Ux','Uy'],ylabel = 'm/s',name_graph = 'Time Serie - Horizontal Velocity' ,show_figure = False) fig_uz = self.graph_TimeSerie(self.data, ['Uz'],ylabel = 'm/s',name_graph = 'Time Serie - Vertical Velocity' , show_figure = False) fig_temp = self.graph_TimeSerie(self.data, ['T_SONIC'],ylabel = '°C',name_graph = 'Time Serie - Temperature' ,show_figure = False) fig_c02 = self.graph_TimeSerie(self.data, ['CO2_density'],ylabel = 'mg/m³',name_graph = 'Time Serie - CO2 Density' ,show_figure = False) fig_h20 = self.graph_TimeSerie(self.data, ['H2O_density'],ylabel = 'g/m³',name_graph = 'Time Serie - H2O Density' ,show_figure = False) fig_p = self.graph_TimeSerie(self.data, ['PA'], ylabel = 'kPa',name_graph = 'Time Serie - Pressure' ,show_figure = False) tab1 = Panel(child = fig_ux_uy, title = 'Horizontal velocity') tab2 = Panel(child = fig_uz, title = 'Vertical velocity') tab3 = Panel(child = fig_temp, title = 'Sonic temperature') tab4 = Panel(child = fig_c02, title = 'CO₂ density') tab5 = Panel(child = fig_h20, title = 'H₂O density') tab6 = Panel(child = fig_p, title = 'Atmospheric pressure') fig = pandas_bokeh.plot_grid([[Tabs(tabs = [tab1, tab2, tab3, tab4, tab5, tab6])]], merge_tools = True,toolbar_location = 'left', show_plot=show_plot) elif self.database == 'Flux': fig_ux_uy = self.graph_TimeSerie(self.data, ['Ux','Uy'],ylabel = 'm/s', show_figure = False) fig_uz = self.graph_TimeSerie(self.data, ['Uz'],ylabel = 'm/s', show_figure = False) fig_temp = self.graph_TimeSerie(self.data, ['T_SONIC'],ylabel = '°C', show_figure = False) fig_c02 = self.graph_TimeSerie(self.data, ['CO2_density'],ylabel = 'mg/m³', show_figure = False) fig_h20 = self.graph_TimeSerie(self.data, ['H2O_density'],ylabel = 'g/m³', show_figure = False) fig_p = self.graph_TimeSerie(self.data, ['PA'], ylabel = 'kPa', show_figure = False) tab1 = Panel(child = fig_ux_uy, title = 'Horizontal velocity') tab2 = Panel(child = fig_uz, title = 'Vertical velocity') tab3 = Panel(child = fig_temp, title = 'Sonic temperature') tab4 = Panel(child = fig_c02, title = 'CO₂') tab5 = Panel(child = fig_h20, title = 'H₂O') tab6 = Panel(child = fig_p, title = 'Atmospheric pressure') fig_LE = self.graph_TimeSerie(self.data, ['LE'], ylabel = 'W/m²', show_figure = False) fig_H = self.graph_TimeSerie(self.data, ['H'], ylabel = 'W/m2', show_figure = False) fig_TAU = self.graph_TimeSerie(self.data, ['TAU'], ylabel = 'kg/ms²', show_figure = False) tab7 = Panel(child = fig_LE, title = 'Latent Heat Flux ') tab8 = Panel(child = fig_H, title = 'Sensible Heat Flux') tab9 = Panel(child = fig_TAU, title = 'Momentum Flux') fig = pandas_bokeh.plot_grid([[Tabs(tabs = [tab1, tab2, tab3, tab4, tab5, tab6])], [Tabs(tabs = [tab7, tab8, tab9])]], merge_tools = True,toolbar_location = 'left', show_plot=show_plot) return fig
def plot_Barplot3(): plotname = inspect.stack()[0][3][5:] pandas_bokeh.output_file(os.path.join(PLOT_DIR, f"{plotname}.html")) df = df_fruits() p_bar = df.plot_bokeh.bar( ylabel="Price per Unit [€]", title="Fruit prices per Year", alpha=0.6, show_figure=False, ) p_stacked_bar = df.plot_bokeh.bar( ylabel="Price per Unit [€]", title="Fruit prices per Year", stacked=True, alpha=0.6, show_figure=False, ) # Reset index, such that "fruits" is now a column of the DataFrame: df.reset_index(inplace=True) # Create horizontal bar (via kind keyword): p_hbar = df.plot_bokeh( kind="barh", x="fruits", xlabel="Price per Unit [€]", title="Fruit prices per Year", alpha=0.6, legend="bottom_right", show_figure=False, ) # Create stacked horizontal bar (via barh accessor): p_stacked_hbar = df.plot_bokeh.barh( x="fruits", stacked=True, xlabel="Price per Unit [€]", title="Fruit prices per Year", alpha=0.6, legend="bottom_right", show_figure=False, ) pandas_bokeh.plot_grid([[p_bar, p_stacked_bar], [p_hbar, p_stacked_hbar]], plot_width=450)
def test_mapplot(df_mapplot): "Mapplot test" kwargs = dict( x="longitude", y="latitude", hovertool_string="""<h2> @{name} </h2> <h3> Population: @{pop_max} </h3>""", tile_provider="STAMEN_TERRAIN_RETINA", size="size", figsize=(900, 600), title="World cities with more than 1.000.000 inhabitants", show_figure=False, ) p_map = df_mapplot.plot_bokeh(kind="map", **kwargs) p_map_accessor = df_mapplot.plot_bokeh.map(**kwargs) p_map_pandas_backend = df_mapplot.plot(kind="map", **kwargs) p_map_accessor_pandas_backend = df_mapplot.plot.map(**kwargs) layout = pandas_bokeh.plot_grid([[p_map, p_map_accessor]], plot_width=450, plot_height=300, show_plot=False) pandas_bokeh.output_file(os.path.join(DIRECTORY, "Plots", "Mapplot.html")) pandas_bokeh.save(layout)
def plot_Scatterplot(): df = df_iris() df = df.sample(frac=1) # Create Bokeh-Table with DataFrame: from bokeh.models import ColumnDataSource from bokeh.models.widgets import DataTable, TableColumn data_table = DataTable( columns=[TableColumn(field=Ci, title=Ci) for Ci in df.columns], source=ColumnDataSource(df.head(10)), ) # Create Scatterplot: p_scatter = df.plot_bokeh.scatter( x="petal length (cm)", y="sepal width (cm)", category="species", title="Iris DataSet Visualization", show_figure=False, ) # Combine Div and Scatterplot via grid layout: return pandas_bokeh.plot_grid([[data_table, p_scatter]], plot_width=400, plot_height=350, show_plot=False)
def test_pieplot(): "Test Pieplot" p_pie = df_pie = pd.read_csv( os.path.join(test_sets_directory, "Bundestagswahl", "Bundestagswahl.csv") ) p_pie = df_pie.plot_bokeh.pie( x="Partei", y="2017", colormap=["blue", "red", "yellow", "green", "purple", "orange", "grey"], title="Results of German Bundestag Election 2017", show_figure=False, ) p_pie_multiple = df_pie.plot_bokeh( kind="pie", x="Partei", colormap=["blue", "red", "yellow", "green", "purple", "orange", "grey"], title="Results of German Bundestag Elections [2002-2017]", line_color="grey", show_figure=False, ) layout = pandas_bokeh.plot_grid( [[p_pie, p_pie_multiple]], plot_width=450, plot_height=300, show_plot=False ) pandas_bokeh.output_file(os.path.join(directory, "Plots", "Pieplot.html")) pandas_bokeh.save(layout)
def test_scatterplot(): "Test for scatterplot" # Load Iris Dataset: df = pd.read_csv(os.path.join(test_sets_directory, "iris", "iris.csv")) # Create Div with DataFrame: from bokeh.models import Div div_df = Div(text=df.head(10).to_html(index=False), width=550) div_df_accessor = Div(text=df.head(10).to_html(index=False), width=550) # Create Scatterplot: arguments = dict( x="petal length (cm)", y="sepal width (cm)", category="species", title="Iris DataSet Visualization", show_figure=False, ) p_scatter = df.plot_bokeh(kind="scatter", **arguments) p_scatter_accessor = df.plot_bokeh.scatter(**arguments) # Combine Div and Scatterplot via grid layout: output = pandas_bokeh.plot_grid( [[div_df, p_scatter], [div_df_accessor, p_scatter_accessor]], show_plot=False, return_html=True, ) with open(os.path.join(directory, "Plots", "Scatterplot.html"), "w") as f: f.write(output) assert True
def gen_template_1(outputfile): df = corona_df[0] #df = df[['Country,Other','TotalCases']] df = df.head(21) df = df.fillna(method='ffill') data_table = DataTable( columns=[TableColumn(field=Ci, title=Ci) for Ci in df.columns], source=ColumnDataSource(df), width=800, ) df = pd.DataFrame(df).set_index("Country,Other") bar_graph = df.plot_bokeh.barh(xlabel='TotalCases Count', ylabel='Country', show_figure=False) pandas_bokeh.plot_grid([[data_table, bar_graph]], plot_width=800, plot_height=350)
def test_pieplot(df_election): "Test Pieplot" p_pie = df_election.plot_bokeh.pie( x="Partei", y="2017", colormap=[ "blue", "red", "yellow", "green", "purple", "orange", "grey" ], title="Results of German Bundestag Election 2017", show_figure=False, ) p_pie_multiple = df_election.plot_bokeh( kind="pie", x="Partei", colormap=[ "blue", "red", "yellow", "green", "purple", "orange", "grey" ], title="Results of German Bundestag Elections [2002-2017]", line_color="grey", show_figure=False, ) p_pie_pandas_backend = df_election.plot.pie( x="Partei", y="2017", colormap=[ "blue", "red", "yellow", "green", "purple", "orange", "grey" ], title="Results of German Bundestag Election 2017", show_figure=False, ) p_pie_multiple_pandas_backend = df_election.plot( kind="pie", x="Partei", colormap=[ "blue", "red", "yellow", "green", "purple", "orange", "grey" ], title="Results of German Bundestag Elections [2002-2017]", line_color="grey", show_figure=False, ) layout = pandas_bokeh.plot_grid( [ [p_pie, p_pie_multiple], [p_pie_pandas_backend, p_pie_multiple_pandas_backend], ], plot_width=450, plot_height=300, show_plot=False, ) pandas_bokeh.output_file(os.path.join(DIRECTORY, "Plots", "Pieplot.html")) pandas_bokeh.save(layout)
def test_area_plots(df_energy): "Test area plots" p_area = df_energy.plot_bokeh.area( x="Year", stacked=True, legend="top_left", colormap=["brown", "orange", "black", "grey", "blue", "green"], title="Worldwide energy consumption split by energy source", ylabel="Million tonnes oil equivalent", ylim=(0, 16000), show_figure=False, ) p_area_normed = df_energy.plot_bokeh( kind="area", x="Year", stacked=True, normed=100, legend="bottom_left", colormap=["brown", "orange", "black", "grey", "blue", "green"], title="Worldwide energy consumption split by energy source", ylabel="Million tonnes oil equivalent", show_figure=False, ) p_area_pandas_backend = df_energy.plot.area( x="Year", stacked=True, legend="top_left", colormap=["brown", "orange", "black", "grey", "blue", "green"], title="Worldwide energy consumption split by energy source", ylabel="Million tonnes oil equivalent", ylim=(0, 16000), show_figure=False, ) p_area_normed_pandas_backend = df_energy.plot( kind="area", x="Year", stacked=True, normed=100, legend="bottom_left", colormap=["brown", "orange", "black", "grey", "blue", "green"], title="Worldwide energy consumption split by energy source", ylabel="Million tonnes oil equivalent", show_figure=False, ) layout = pandas_bokeh.plot_grid([[p_area, p_area_normed]], plot_width=450, plot_height=300, show_plot=False) pandas_bokeh.output_file(os.path.join(DIRECTORY, "Plots", "Areaplot.html")) pandas_bokeh.save(layout)
def plot_Histogram(): plotname = inspect.stack()[0][3][5:] pandas_bokeh.output_file(os.path.join(PLOT_DIR, f"{plotname}.html")) df = df_hist() # Top-on-Top Histogram (Default): p1 = df.plot_bokeh.hist( bins=np.linspace(-5, 5, 41), vertical_xlabel=True, hovertool=False, title="Normal distributions (Top-on-Top)", line_color="black", show_figure=False, ) # Side-by-Side Histogram (multiple bars share bin side-by-side) also accessible via # kind="hist": p2 = df.plot_bokeh( kind="hist", bins=np.linspace(-5, 5, 41), histogram_type="sidebyside", vertical_xlabel=True, hovertool=False, title="Normal distributions (Side-by-Side)", line_color="black", show_figure=False, ) # Stacked histogram: p3 = df.plot_bokeh.hist( bins=np.linspace(-5, 5, 41), histogram_type="stacked", vertical_xlabel=True, hovertool=False, title="Normal distributions (Stacked)", line_color="black", show_figure=False, ) pandas_bokeh.plot_grid([[p1], [p2], [p3]])
def get_plot(): nu_df = getdata() mac_df = nu_df[[ 'Food items', 'Protein (g)', 'Total Fat(g)', 'Total Carbs(g)', 'Fiber, total dietary(g)' ]] min_df = nu_df[[ 'Food items', 'Vitamin C(mg)', 'Vitamin E(mg)', 'Vitamin B6(mg)', 'Calcium, Ca(mg)', 'Iron, Fe(mg)', 'Magnesium, Mg(mg)', 'Phosphorus, P(mg)', 'Zinc, Zn(mg)', 'Sodium, Na(mg)', 'Potassium, K(mg)' ]] pw = nu_df.plot_bokeh.scatter(x='Food items', y='Calories (kcal)', show_figure=False) px = nu_df.plot_bokeh.scatter(x='Calories (kcal)', y='Protein (g)', category='Food items', show_figure=False) py = nu_df.plot_bokeh.scatter(x='Calories (kcal)', y='Total Fat(g)', category='Food items', show_figure=False) pz = nu_df.plot_bokeh.scatter(x='Calories (kcal)', y='Total Carbs(g)', category='Food items', show_figure=False) pm = mac_df.plot_bokeh.bar(x='Food items', ylabel="grams (g)", show_figure=False) pn = min_df.plot_bokeh.bar(x='Food items', ylabel="milligrams (mg)", show_figure=False) a_plot = pandas_bokeh.plot_grid([[pw, px], [py, pz]]) b_plot = pandas_bokeh.plot_grid([[pm, pn]]) return (a_plot, b_plot)
def test_scatterplot(df_iris): "Test for scatterplot" # Create Bokeh-Table with DataFrame: from bokeh.models import ColumnDataSource from bokeh.models.widgets import DataTable, TableColumn data_table = DataTable( columns=[TableColumn(field=Ci, title=Ci) for Ci in df_iris.columns], source=ColumnDataSource(df_iris.head(10)), ) data_table_accessor = DataTable( columns=[TableColumn(field=Ci, title=Ci) for Ci in df_iris.columns], source=ColumnDataSource(df_iris.head(10)), ) # Create Scatterplot: arguments = dict( x="petal length (cm)", y="sepal width (cm)", category="species", title="Iris DataSet Visualization", show_figure=False, ) p_scatter = df_iris.plot_bokeh(kind="scatter", **arguments) p_scatter_accessor = df_iris.plot_bokeh.scatter(**arguments) p_scatter_pandas_backend = df_iris.plot(kind="scatter", **arguments) p_scatter_accessor_pandas_backend = df_iris.plot.scatter(**arguments) # Combine Div and Scatterplot via grid layout: output = pandas_bokeh.plot_grid( [ [data_table, p_scatter], [ data_table_accessor, p_scatter_accessor, p_scatter_pandas_backend, p_scatter_accessor_pandas_backend, ], ], show_plot=False, return_html=True, ) with open(os.path.join(DIRECTORY, "Plots", "Scatterplot.html"), "w") as f: f.write(output) assert True
def test_histogram_average_diplay(df_hist): "Test average & cumulative function of histogram" p_hist = df_hist.plot_bokeh.hist( y=["a", "b"], bins=np.arange(-4, 6.5, 0.5), normed=100, vertical_xlabel=True, ylabel="Share[%]", title="Normal distributions (normed)", show_average=True, xlim=(-4, 6), ylim=(0, 30), show_figure=False, ) p_hist_cum = df_hist.plot_bokeh.hist( y=["a", "b"], bins=np.arange(-4, 6.5, 0.5), normed=100, cumulative=True, vertical_xlabel=True, ylabel="Share[%]", title="Normal distributions (normed & cumulative)", show_figure=False, ) p_hist_cum_pandas_backend = df_hist.plot.hist( by=["a", "b"], bins=np.arange(-4, 6.5, 0.5), normed=100, cumulative=True, vertical_xlabel=True, ylabel="Share[%]", title="Normal distributions (normed & cumulative)", show_figure=False, ) p_average = pandas_bokeh.plot_grid( [[p_hist, p_hist_cum, p_hist_cum_pandas_backend]], plot_width=450, plot_height=300, show_plot=False, ) pandas_bokeh.output_file( os.path.join(DIRECTORY, "Plots", "Histogram_average_and_cumulative.html")) pandas_bokeh.save(p_average)
def plot_lines_dashboard_ita(cases_df, figures_path, geo_name, plot_dashboard_flag): for save_flag in [False]: p1 = plot_df_lines_bokeh(cases_df, "data", [ "totale_tamponi", "totale_casi", ], figures_path, "tamponi", save_flag) p2 = plot_df_lines_bokeh(cases_df, "data", [ "totale_attualmente_positivi", "totale_deceduti", "totale_dimessi_guariti" ], figures_path, "totali_principali", save_flag) p3 = plot_df_lines_bokeh(cases_df, "data", [ "nuovi_positivi", "nuovi_attualmente_positivi", "nuovi_deceduti", "nuovi_dimessi_guariti" ], figures_path, "nuovi_principali", save_flag) p4 = plot_df_lines_bokeh(cases_df, "data", [ "tasso_positivi_tamponi", "tasso_nuovi_positivi", "tasso_mortalita", "tasso_guarigione" ], figures_path, "tassi_principali", save_flag) p5 = plot_df_lines_bokeh(cases_df, "data", [ "attualmente_isolamento_domiciliare", "attualmente_ricoverati", "attualmente_terapia_intensiva", ], figures_path, "dettaglio_pazienti_attuali", save_flag) p6 = plot_df_lines_bokeh(cases_df, "data", [ "tasso_ricoverati_con_sintomi", "tasso_terapia_intensiva", "tasso_terapia_intensiva_ricoverati", ], figures_path, "tassi_condizioni_cliniche", save_flag) if plot_dashboard_flag: outfp = os.path.join(figures_path, geo_name + ".html") output_file(outfp) pandas_bokeh.output_file(outfp) plot_grid = pandas_bokeh.plot_grid([ [p1, p2], [p3, p4], [p5, p6], ], toolbar_location="left") # pandas_bokeh.save(plot_grid) save(plot_grid)
def plot_Barplot3(): df = df_fruits() p_bar = df.plot_bokeh.bar( ylabel="Price per Unit [€]", title="Fruit prices per Year", alpha=0.6, show_figure=False, ) p_stacked_bar = df.plot_bokeh.bar( ylabel="Price per Unit [€]", title="Fruit prices per Year", stacked=True, alpha=0.6, show_figure=False, ) # Reset index, such that "fruits" is now a column of the DataFrame: df.reset_index(inplace=True) # Create horizontal bar (via kind keyword): p_hbar = df.plot_bokeh( kind="barh", x="fruits", xlabel="Price per Unit [€]", title="Fruit prices per Year", alpha=0.6, legend="bottom_right", show_figure=False, ) # Create stacked horizontal bar (via barh accessor): p_stacked_hbar = df.plot_bokeh.barh( x="fruits", stacked=True, xlabel="Price per Unit [€]", title="Fruit prices per Year", alpha=0.6, legend="bottom_right", show_figure=False, ) return pandas_bokeh.plot_grid( [[p_bar, p_stacked_bar], [p_hbar, p_stacked_hbar]], plot_width=450, show_plot=False, )
def test_histogram_average_diplay(): "Test average & cumulative function of histogram" import numpy as np df_hist = pd.DataFrame( { "a": np.random.randn(1000) + 1, "b": np.random.randn(1000), "c": np.random.randn(1000) - 1, }, columns=["a", "b", "c"], ) p_hist = df_hist.plot_bokeh.hist( y=["a", "b"], bins=np.arange(-4, 6.5, 0.5), normed=100, vertical_xlabel=True, ylabel="Share[%]", title="Normal distributions (normed)", show_average=True, xlim=(-4, 6), ylim=(0, 30), show_figure=False, ) p_hist_cum = df_hist.plot_bokeh.hist( y=["a", "b"], bins=np.arange(-4, 6.5, 0.5), normed=100, cumulative=True, vertical_xlabel=True, ylabel="Share[%]", title="Normal distributions (normed & cumulative)", show_figure=False, ) p_average = pandas_bokeh.plot_grid( [[p_hist, p_hist_cum]], plot_width=450, plot_height=300, show_plot=False ) pandas_bokeh.output_file( os.path.join(directory, "Plots", "Histogram_average_and_cumulative.html") ) pandas_bokeh.save(p_average)
def draw_graph(df, key_selected): plots = [] result_type_name = key_selected.replace("_", " ").capitalize() for alg in set(sorted(df[k0_algorithm].values)): alg_name = alg alg_type = "scheduling" if "heuristic" in alg: alg_name = "OGSA" elif "optimal" in alg: alg_name = "Optimisation model" if "fw" in alg: alg_name += " with FW" alg_type = "pricing" df_selected = df.loc[lambda df2: df[k0_algorithm] == alg, lambda df2: [k0_households_no, key_selected]] df_selected = df_selected.set_index(k0_households_no) y_label = "" if "time" in key_selected: y_label = "Run time (seconds)" elif "iteration" in key_selected: y_label = "Iteration" elif "reduction" in key_selected: y_label = key_selected.replace("_", " ").capitalize() p_line = df_selected.plot_bokeh( kind="line", title="{} of {}, {}".format(result_type_name, alg_type, alg_name), xlabel="Number of Households", ylabel=y_label, show_figure=False, sizing_mode="scale_width", ) if "reduction" in key_selected: p_line.yaxis.formatter = NumeralTickFormatter(format='0 %') p_line.legend.location = "top_left" p_line.y_range.start = 0 plots.append(p_line) pandas_bokeh.output_file("{}{}.html".format(dt_folder, result_type_name)) grid = pandas_bokeh.plot_grid(plots, ncols=2, show_plot=False) pandas_bokeh.save(grid)
def plot_lines_dashboard(cases_df, figures_path, geo_name, plot_dashboard_flag): if plot_dashboard_flag: save_flag = False outfp = os.path.join(figures_path, geo_name + ".html") output_file(outfp) pandas_bokeh.output_file(outfp) else: save_flag = True p1 = plot_df_lines_bokeh(cases_df, "datetime", [ "total_cases", ], figures_path, "cases", save_flag) p2 = plot_df_lines_bokeh(cases_df, "datetime", ["total_deaths", "total_recovered"], figures_path, "main_totals", save_flag) p3 = plot_df_lines_bokeh(cases_df, "datetime", [ "new_positives", "new_currently_positives", "new_deaths", "new_recovered" ], figures_path, "main_new", save_flag) p4 = plot_df_lines_bokeh( cases_df, "datetime", ["rate_new_positives", "rate_deaths", "rate_recovered"], figures_path, "main_rate", save_flag) if plot_dashboard_flag: plot_grid = pandas_bokeh.plot_grid([ [p1, p2], [p3, p4], ], toolbar_location="left") # pandas_bokeh.save(plot_grid) save(plot_grid)
def plot_Histogram(): df = df_hist() # Top-on-Top Histogram (Default): p1 = df.plot_bokeh.hist( bins=np.linspace(-5, 5, 41), vertical_xlabel=True, hovertool=False, title="Normal distributions (Top-on-Top)", line_color="black", show_figure=False, ) # Side-by-Side Histogram (multiple bars share bin side-by-side) also accessible via # kind="hist": p2 = df.plot_bokeh( kind="hist", bins=np.linspace(-5, 5, 41), histogram_type="sidebyside", vertical_xlabel=True, hovertool=False, title="Normal distributions (Side-by-Side)", line_color="black", show_figure=False, ) # Stacked histogram: p3 = df.plot_bokeh.hist( bins=np.linspace(-5, 5, 41), histogram_type="stacked", vertical_xlabel=True, hovertool=False, title="Normal distributions (Stacked)", line_color="black", show_figure=False, ) return pandas_bokeh.plot_grid([[p1], [p2], [p3]], show_plot=False)
def test_barplot_layout(df_fruits): "Test for Barplot layout" p_bar = df_fruits.plot_bokeh.bar( ylabel="Price per Unit [€]", title="Fruit prices per Year", alpha=0.6, show_figure=False, ) p_stacked_bar = df_fruits.plot_bokeh.bar( ylabel="Price per Unit [€]", title="Fruit prices per Year", stacked=True, alpha=0.6, show_figure=False, ) p_bar_pandas_backend = df_fruits.plot.bar( ylabel="Price per Unit [€]", title="Fruit prices per Year", alpha=0.6, show_figure=False, ) p_stacked_bar_pandas_backend = df_fruits.plot.bar( ylabel="Price per Unit [€]", title="Fruit prices per Year", stacked=True, alpha=0.6, show_figure=False, ) # Reset index, such that "fruits" is now a column of the DataFrame: df_fruits.reset_index(inplace=True) p_hbar = df_fruits.plot_bokeh( kind="barh", x="fruits", xlabel="Price per Unit [€]", title="Fruit prices per Year", alpha=0.6, legend="bottom_right", show_figure=False, ) p_stacked_hbar = df_fruits.plot_bokeh.barh( x="fruits", stacked=True, xlabel="Price per Unit [€]", title="Fruit prices per Year", alpha=0.6, legend="bottom_right", show_figure=False, ) p_hbar_pandas_backend = df_fruits.plot( kind="barh", x="fruits", xlabel="Price per Unit [€]", title="Fruit prices per Year", alpha=0.6, legend="bottom_right", show_figure=False, ) p_stacked_hbar_pandas_backend = df_fruits.plot.barh( x="fruits", stacked=True, xlabel="Price per Unit [€]", title="Fruit prices per Year", alpha=0.6, legend="bottom_right", show_figure=False, ) # Plot all barplot examples in a grid: output = pandas_bokeh.plot_grid( [[p_bar, p_stacked_bar], [p_hbar, p_stacked_hbar]], plot_width=450, show_plot=False, return_html=True, ) # Output plot as HTML: with open(os.path.join(DIRECTORY, "Plots", "Barplot_layout.html"), "w") as f: f.write(output) assert True
gdp_df = gdp[['Country', 'GDP_nominal_2017', 'GDP_abbrev']] data = gdp_df.rename(columns={'index': 'Country'}) l = data['GDP_nominal_2017'].to_list() l = [sub.replace('$', '').replace(',', '') for sub in l] l = [int(i) for i in l] l = pd.Series(l) data['angle'] = l / l.sum() * 2 * pi data['color'] = Category20c[len(gdp)] p = figure(plot_height=350, title="World GDP Distribution", toolbar_location='above', tooltips="@Country:" + "@GDP_abbrev", x_range=(-0.5, 1.0)) p.wedge(x=0, y=1, radius=0.4, start_angle=cumsum('angle', include_zero=True), end_angle=cumsum('angle'), line_color="white", fill_color='color', legend_field='Country', source=data) p.axis.axis_label = None p.axis.visible = False p.grid.grid_line_color = None pandas_bokeh.plot_grid([[gdp_table, p]])
def plot_Startimage(): plotname = inspect.stack()[0][3][5:] pandas_bokeh.output_file(os.path.join(PLOT_DIR, f"{plotname}.html")) # Barplot: data = { "fruits": ["Apples", "Pears", "Nectarines", "Plums", "Grapes", "Strawberries"], "2015": [2, 1, 4, 3, 2, 4], "2016": [5, 3, 3, 2, 4, 6], "2017": [3, 2, 4, 4, 5, 3], } df = pd.DataFrame(data).set_index("fruits") p_bar = df.plot_bokeh( kind="bar", ylabel="Price per Unit [€]", title="Fruit prices per Year", show_figure=False, ) # Lineplot: np.random.seed(42) df = pd.DataFrame( {"Google": np.random.randn(1000) + 0.2, "Apple": np.random.randn(1000) + 0.17}, index=pd.date_range("1/1/2000", periods=1000), ) df = df.cumsum() df = df + 50 p_line = df.plot_bokeh( kind="line", title="Apple vs Google", xlabel="Date", ylabel="Stock price [$]", yticks=[0, 100, 200, 300, 400], ylim=(0, 400), colormap=["red", "blue"], show_figure=False, ) # Scatterplot: df = df_iris() p_scatter = df.plot_bokeh( kind="scatter", x="petal length (cm)", y="sepal width (cm)", category="species", title="Iris DataSet Visualization", show_figure=False, ) # Histogram: df_hist = pd.DataFrame( { "a": np.random.randn(1000) + 1, "b": np.random.randn(1000), "c": np.random.randn(1000) - 1, }, columns=["a", "b", "c"], ) p_hist = df_hist.plot_bokeh( kind="hist", bins=np.arange(-6, 6.5, 0.5), vertical_xlabel=True, normed=100, hovertool=False, title="Normal distributions", show_figure=False, ) # Make Dashboard with Grid Layout: plot = pandas_bokeh.plot_grid( [[p_line, p_bar], [p_scatter, p_hist]], plot_width=450 )
def plot_top_words_conditional(texts, labels, vectorizer=default_vectorizer, topk=20, interactive=False, save_path='../visualizations/'): if type(texts[0]) == list: texts = [" ".join(LemmaTokenizer()(" ".join(i))) for i in texts] else: texts = [" ".join(LemmaTokenizer()(i)) for i in texts] vectorized_corpus = vectorizer.fit_transform(texts) top_words = vectorizer.get_feature_names() top_words_df = pd.DataFrame(vectorized_corpus.toarray(), columns=top_words) top_words_df['label'] = labels top_words_df = top_words_df.groupby(['label' ]).mean().reset_index(drop=False) #print (top_words_df.head(5)) #top_words_df = pd.melt(top_words_df, id_vars=['label'],value_vars=list(top_words)) top_words_df = pd.melt(top_words_df, col_level=0, id_vars=['label'], value_vars=top_words_df.columns[1:]) top_words_df.columns = ['label', 'top_word', 'word_weight'] top_words_df = top_words_df.sort_values( by=['label', 'word_weight'], ascending=[True, True]).reset_index(drop=True) top_words_df = top_words_df.groupby(['label' ]).tail(topk).reset_index(drop=True) if interactive: pd.set_option('plotting.backend', 'pandas_bokeh') html_plots = [] for lb in top_words_df.label.unique(): html_plots.append(top_words_df[top_words_df['label'] == lb][[ 'top_word', 'word_weight' ]].set_index('top_word').plot_bokeh( kind="barh", title="Top Words: Label = {}".format(lb), legend=False, show_figure=False, xlabel="Weight", ylabel="Word")) html_plot = pandas_bokeh.plot_grid([html_plots], plot_width=450, return_html=True, show_plot=False) #Export the HTML string to an external HTML file and show it: with open(os.path.join(save_path, "top_words_conditional.html"), "w") as f: f.write(html_plot) else: pd.set_option('plotting.backend', 'matplotlib') fig, ax = plt.subplots(1, top_words_df.label.nunique(), figsize=(15, 6)) fig.tight_layout() for i, lb in enumerate(top_words_df.label.unique()): top_words_df[top_words_df['label'] == lb][[ 'top_word', 'word_weight' ]].set_index('top_word').plot.barh(legend=False, ax=ax[i]) ax[i].set_xlabel("Weight") if i == 0: ax[i].set_ylabel("Word") else: ax[i].set_ylabel(None) ax[i].set_title("Top Words: Label = {}".format(lb)) #plt.title("Top Words") plt.savefig(os.path.join(save_path, "top_words_conditional.png"), dpi=200, bbox_inches='tight') plt.close()
def visualize_polytrend_polygon(result): """ Create maps for polygons Args: result: list contains what comes out of PolyTrend R package Returns: render_template with graphics """ ## get staticstics for all points result_to_display = get_PT_statistics(result) trend_stats = { "concealed": result_to_display["count_concealed"], "linear": result_to_display["count_linear"], "no trend": result_to_display["count_no_trend"], "quadratic": result_to_display["count_quadratic"], "cubic": result_to_display["count_cubic"], } # get pie plots for summary statistics trend_data = (pd.Series(trend_stats).reset_index(name="value").rename( columns={"index": "trend"})) trend_data["angle"] = trend_data["value"] / trend_data["value"].sum( ) * 2 * pi trend_data["color"] = ["gray", "forestgreen", "yellow", "blue", "red"] trend_pie = figure( plot_height=350, title="Trend type (share in total number of pixels)", toolbar_location=None, tools="hover", tooltips="@trend: @value", x_range=(-0.5, 1.0), sizing_mode="scale_both", ) trend_pie.wedge( x=0, y=1, radius=0.4, start_angle=cumsum("angle", include_zero=True), end_angle=cumsum("angle"), line_color="white", fill_color="color", legend="trend", source=trend_data, ) trend_pie.axis.axis_label = None trend_pie.axis.visible = False trend_pie.grid.grid_line_color = None direction_stats = { "negative": result_to_display["count_negative"], "positive": result_to_display["count_positive"], } dir_data = (pd.Series(direction_stats).reset_index(name="value").rename( columns={"index": "direction"})) dir_data["angle"] = dir_data["value"] / dir_data["value"].sum() * 2 * pi dir_data["color"] = ["gray", "forestgreen"] direction_pie = figure( plot_height=350, title="Change direction (share in total number of pixels)", toolbar_location=None, tools="hover", x_range=(-0.5, 1.0), sizing_mode="scale_both", tooltips="@direction: @value", ) direction_pie.wedge( x=0, y=1, radius=0.4, start_angle=cumsum("angle", include_zero=True), end_angle=cumsum("angle"), line_color="white", fill_color="color", legend="direction", source=dir_data, ) direction_pie.axis.axis_label = None direction_pie.axis.visible = False direction_pie.grid.grid_line_color = None pie_layout = row([trend_pie, direction_pie], sizing_mode="stretch_both") script, div = components(pie_layout) ### get maps gpd_coordinates = result["geometry"].apply(Point) gpd_df = gpd.GeoDataFrame(result, geometry=gpd_coordinates) gpd_df.crs = {"init": "epsg:4326"} pointA = result["geometry"][0] pointB = result["geometry"][1] buffer_size = pointA.distance(pointB) / 2 gpd_df.geometry = gpd_df.geometry.buffer(buffer_size).envelope colormap_trend = ["grey", "yellow", "green", "blue", "red"] trend_map = gpd_df.plot_bokeh( category="trend_type", colormap=colormap_trend, line_color=None, title="Map of trend types", legend="Trend type", show_figure=False, show_colorbar=True, hovertool_columns="all", ) colormap_dir = ["yellow", "green"] direction_map = gpd_df.plot_bokeh( category="direction", colormap=colormap_dir, line_color=None, title="Map of direction", legend="Direction", show_figure=False, show_colorbar=True, hovertool_columns="all", ) slope_map = gpd_df.plot_bokeh( category="slope", fill_color=palette, legend="Slope", title="Slope map", show_figure=False, line_color=None, hovertool_columns="all", ) plot_grid = pandas_bokeh.plot_grid( [[trend_map, direction_map], [slope_map]], show_plot=False, return_html=True) return render_template( "results_polytrend.html", result=result_to_display, pt_map=plot_grid, script=script, div=div, is_point=False, )
def dbest_visualize_polygon(result, algorithm, data_type): """ Create maps for polygons Args: result: dataframe contains what comes out of DBEST package algorithm: string 'generalization' or 'change detection' depending on user's choice Returns: render_template with graphics """ result_to_display = {} if data_type == "cyclical": gpd_coordinates = result["geometry"].apply(Point) gpd_df = gpd.GeoDataFrame(result, geometry=gpd_coordinates) gpd_df.crs = {"init": "epsg:4326"} pointA = result["geometry"][0] pointB = result["geometry"][1] buffer_size = pointA.distance(pointB) / 2 gpd_df.geometry = gpd_df.geometry.buffer(buffer_size).envelope mapper = LinearColorMapper(palette=palette) colormap = ["grey", "yellow"] start_map = gpd_df.plot_bokeh( category="start", colormap=palette, title="Start time", legend="Start time", line_color=None, show_figure=False, show_colorbar=True, ) duration_map = gpd_df.plot_bokeh( category="duration", colormap=palette, title="Duration (months)", legend="Duration", line_color=None, show_figure=False, ) change_map = gpd_df.plot_bokeh( category="change", colormap=palette, title="Change map", legend="Change value", line_color=None, show_figure=False, ) change_type_map = gpd_df.plot_bokeh( category="change_type", colormap=colormap, title="Change type map - abrupt (1), non-abrupt (0)", legend="Change type", line_color=None, show_figure=False, ) plot_grid = pandas_bokeh.plot_grid( [[change_map, duration_map], [start_map, change_type_map]], return_html=True, show_plot=False, ) script = "" div = "" generalization = "" change_detection = "" elif data_type == "non-cyclical": plot_grid = "" script = "" div = "" generalization = "No result for non-cyclical data yet..." change_detection = "" return render_template( "results_DBEST.html", generalization=generalization, change_detection=change_detection, dbest_maps=plot_grid, result=result_to_display, is_point=False, script=script, div=div, )
def test_barplot_layout(): "Test for Barplot layout" data = { "fruits": ["Apples", "Pears", "Nectarines", "Plums", "Grapes", "Strawberries"], "2015": [2, 1, 4, 3, 2, 4], "2016": [5, 3, 3, 2, 4, 6], "2017": [3, 2, 4, 4, 5, 3], } df = pd.DataFrame(data).set_index("fruits") p_bar = df.plot_bokeh.bar( ylabel="Price per Unit [€]", title="Fruit prices per Year", alpha=0.6, show_figure=False, ) p_stacked_bar = df.plot_bokeh.bar( ylabel="Price per Unit [€]", title="Fruit prices per Year", stacked=True, alpha=0.6, show_figure=False, ) # Reset index, such that "fruits" is now a column of the DataFrame: df.reset_index(inplace=True) # Create horizontal bar (via kind keyword): p_hbar = df.plot_bokeh( kind="barh", x="fruits", xlabel="Price per Unit [€]", title="Fruit prices per Year", alpha=0.6, legend="bottom_right", show_figure=False, ) # Create stacked horizontal bar (via barh accessor): p_stacked_hbar = df.plot_bokeh.barh( x="fruits", stacked=True, xlabel="Price per Unit [€]", title="Fruit prices per Year", alpha=0.6, legend="bottom_right", show_figure=False, ) # Plot all barplot examples in a grid: output = pandas_bokeh.plot_grid( [[p_bar, p_stacked_bar], [p_hbar, p_stacked_hbar]], plot_width=450, show_plot=False, return_html=True, ) # Output plot as HTML: with open(os.path.join(directory, "Plots", "Barplot_layout.html"), "w") as f: f.write(output) assert True
dataUSEstd[['大区名称', '小区名称', '城市', '经销商代码', '客户来源更新', '车型new', '潜客量']]) dataEsti = dataEsti.groupby(['大区名称', '小区名称', '城市', '经销商代码', '客户来源更新', '车型new'])['潜客量'].sum().reset_index() # 添加车型站比 dataEsti['dealer汇总'] = dataEsti.groupby( ['大区名称', '小区名称', '城市', '经销商代码', '客户来源更新'])['潜客量'].transform('sum') dataEsti['车型占比'] = dataEsti['潜客量'] / dataEsti['dealer汇总'] dataEsti['日均概率'] = dataEsti['潜客量'] / monthDays dealerUnique = dataCOMPUTed[['大区名称', '小区名称', '经销商代码', '城市', '客户来源更新']].drop_duplicates() dealerUnique = dealerUnique.reset_index(drop=True)[['经销商代码', '客户来源更新']] # 生成数据 start = 0 for idx, row in dealerUnique.iterrows(): print(idx, row['经销商代码'], row['客户来源更新']) start = genNewData(row['经销商代码'], row['客户来源更新'], start) print(start) # 这时再做合并就是类似dataAvailable一样了 newMiss = dataMiss.groupby( ['日期', '大区名称', '小区名称', '经销商代码', '经销商名称', '城市', '客户来源更新', '车型new']).sum().reset_index() # 画图,并生成html pics = [] for pic in graphLVL: pics.extend(batDraw(pic)) html = pb.plot_grid(pics, plot_width=300, return_html=True) with open("tt.html", 'w') as f: f.write(html)
source = ColumnDataSource(dg) val = dg[group_by][0] for k in dg: if k != col: continue p.line(x=datecolumn, y=k, source=source, legend_label=f"{k}[{val}]", name=f"{k}[{val}]", color=next(colors)) continue else: p = df.plot_bokeh(title=title, kind='line', show_figure=False) plots.append(p) for p in plots: p.legend.click_policy = "hide" grid = [] for i in range(0, len(plots), args.chart_cols): grid.append(plots[i:i + args.chart_cols]) pandas_bokeh.plot_grid(grid)