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_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 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 test_basic_lineplot_rangetool(df_stock): """Test for basic lineplot above with a rangetool extension""" p_basic_lineplot_accessor_pandas_backend = df_stock.plot.line( show_figure=False, rangetool=True) # Output plot as HTML: pandas_bokeh.output_file( os.path.join(DIRECTORY, "Plots", "Basic_lineplot_rangetool.html")) pandas_bokeh.save(p_basic_lineplot_accessor_pandas_backend) assert True
def test_histogram(): "Test for histograms" 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"], ) # Top-on-Top Histogram (Default): p_tt = df_hist.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": p_ss = df_hist.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: p_stack = df_hist.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, ) layout = pandas_bokeh.column([p_tt, p_ss, p_stack]) pandas_bokeh.output_file(os.path.join(directory, "Plots", "Histogram.html")) pandas_bokeh.save(layout)
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 test_autosizing(df_fruits): """ Autoscaling test """ kwargs = dict(figsize=(500, 200), sizing_mode="scale_width", show_figure=False) p_autoscale = df_fruits.plot_bokeh(kind="bar", **kwargs) pandas_bokeh.output_file(os.path.join(DIRECTORY, "Plots", "AutoScale.html")) pandas_bokeh.save(p_autoscale) assert True
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 test_mapplot(): "Mapplot test" df_mapplot = pd.read_csv(os.path.join(test_sets_directory, "populated places","populated_places.csv")) df_mapplot["size"] = df_mapplot["pop_max"] / 1000000 p_map = df_mapplot.plot_bokeh.map( 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) pandas_bokeh.output_file(os.path.join(directory, "Plots", "Mapplot.html")) pandas_bokeh.save(p_map)
def lineplot(x: str, y: list, data, title="Line Plot", output_file="", **lineplot_kwargs): """ Plots a line plot. Parameters ---------- x : str X axis column y : list Y axis column data : Dataframe Dataframe title : str, optional Title of the plot, by default 'Line Plot' output_file : str, optional If a name is provided save the plot to an html file, by default '' """ data_copy = data[[x] + y].copy() data_copy = data_copy.set_index(x) xlabel = lineplot_kwargs.pop("xlabel", x) p_line = data_copy.plot_bokeh.line(title=title, xlabel=xlabel, **lineplot_kwargs) if output_file: pandas_bokeh.output_file(output_file) pandas_bokeh.save(p_line)
def test_histogram(df_hist): "Test for histograms" # Top-on-Top Histogram (Default): p_tt = df_hist.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": p_ss = df_hist.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: p_stack = df_hist.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, ) # Top-on-Top Histogram (Default): p_tt_pandas_backend = df_hist.plot.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": p_ss_pandas_backend = df_hist.plot( 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: p_stack_pandas_backend = df_hist.plot.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, ) layout = pandas_bokeh.column([p_tt, p_ss, p_stack]) pandas_bokeh.output_file(os.path.join(DIRECTORY, "Plots", "Histogram.html")) pandas_bokeh.save(layout)
def draw_graphs(df_summary, df_demands_dt, df_prices_dt, df_others_dt, exp_folder): df_summary.index.names = [k0_algorithm] df_summary = df_summary.reset_index() data_table_summary = DataTable( columns=[TableColumn(field=Ci, title=Ci) for Ci in df_summary.columns], source=ColumnDataSource(df_summary), height=50, sizing_mode="scale_both", # height_policy='auto', ) def place_legend(pl): legend = pl.legend[0] legend.items[0].label['value'] = "Preferred" legend.items[-1].label['value'] = "Optimised" for item in legend.items[1:-1]: item.label['value'] = "Itr {}".format(item.label['value']) legend.click_policy = "mute" pl.add_layout(legend, 'right') return pl rows = [[data_table_summary]] x_ticks = [i for i in range(48) if i % 4 == 0] p_height = 250 for alg in df_demands_dt: alg_name = alg if "heuristic" in alg: alg_name = "OGSA and FW" elif "optimal" in alg: alg_name = "Optimisation model and FW" if 'fw' in alg: df_cost = df_others_dt[alg].T[k0_cost].div(100) p_line_cost = df_cost.plot_bokeh( kind="line", title="Costs, {}".format(alg_name), xlabel="Iteration", ylabel="Costs", show_figure=False, marker="circle", ) p_line_cost.plot_height = p_height p_line_cost.y_range.start = 0 p_line_demand = df_demands_dt[alg].T.plot_bokeh( kind="line", title="Demand Profiles, {}".format(alg_name), xlabel="Time Period", ylabel="Demand (kWh)", xticks=x_ticks, show_figure=False, muted_alpha=0, muted=True, sizing_mode="scale_width", toolbar_location="above", ) p_line_demand.plot_height = p_height p_line_demand.y_range.start = 0 p_line_demand = place_legend(p_line_demand) p_line_demand.renderers[0].muted = False p_line_demand.renderers[-1].muted = False p_line_price = df_prices_dt[alg].T.plot_bokeh( kind="line", title="Prices, {}".format(alg_name), xlabel="Time Period", ylabel="Price (Dollar)", xticks=x_ticks, show_figure=False, muted_alpha=0, muted=True, sizing_mode="scale_width", toolbar_location="above", ) p_line_price.plot_height = p_height p_line_price.y_range.start = 0 p_line_price = place_legend(p_line_price) p_line_price.renderers[0].muted = False p_line_price.renderers[-1].muted = False rows.append([p_line_demand, p_line_price]) pandas_bokeh.output_file("{}plots.html".format(exp_folder)) layout = pandas_bokeh.layout(rows, sizing_mode='scale_width') pandas_bokeh.save(layout)
def barplot( x, y, data, groupby=None, method=None, orient="v", stacked=False, output_file="", **barplot_kwargs, ): """ Visualizes a bar plot. Kwargs are bokeh plot, vbar, and hbar bokeh plots. Parameters ---------- x : str Column name for the x axis. y : list Columns for the y axis data : Dataframe Dataset groupby : str Data to groupby - xaxis, optional, by default None method : str Method to aggregate groupy data Examples: min, max, mean, etc., optional by default None orient : str, optional Orientation of graph, 'h' for horizontal 'v' for vertical, by default 'v' stacked : bool Whether to stack the different columns resulting in a stacked bar chart, by default False """ alpha = barplot_kwargs.pop("alpha", 0.6) data_copy = data[[x] + y].copy() data_copy = data_copy.set_index(x) if groupby: data_copy = data_copy.groupby(groupby, as_index=False) data_copy = getattr(data_copy, method)() if orient == "v": p_bar = data_copy.plot_bokeh.bar( figsize=(1500, 500), # For now until auto scale is implemented stacked=stacked, alpha=alpha, **barplot_kwargs, ) else: p_bar = data_copy.plot_bokeh.barh(figsize=(1500, 500), stacked=stacked, alpha=alpha, **barplot_kwargs) if output_file: pandas_bokeh.output_file(output_file) pandas_bokeh.save(p_bar)
def scatterplot( x: str, y: str, z=None, data=None, category=None, title="Scatter Plot", size=8, output_file="", **scatterplot_kwargs, ): """ Plots a scatter plot. Parameters ---------- x : str X axis column y : str Y axis column z: str Z axis column data : Dataframe Dataframe category : str, optional Category to group your data, by default None title : str, optional Title of the plot, by default 'Scatterplot' size : int or str, optional Size of the circle, can either be a number or a column name to scale the size, by default 8 output_file : str, optional If a name is provided save the plot to an html file, by default '' """ if z is None: fill_alpha = scatterplot_kwargs.pop("fill_alpha", 0.6) data[[x, y]] = data[[x, y]].apply(pd.to_numeric) p_scatter = data.plot_bokeh.scatter( x=x, y=y, category=category, title=title, size=size, fill_alpha=fill_alpha, **scatterplot_kwargs, ) if output_file: pandas_bokeh.output_file(output_file) pandas_bokeh.save(p_scatter) else: fig = px.scatter_3d(data, x=x, y=y, z=z, **scatterplot_kwargs) fig.show()