def generate_group_bar(df, title="", scientific_notation=False): fig = plt.figure(title=title) bar_chart = plt.bar( x=df.columns.values.tolist(), y=df, labels=df.index.values.tolist(), display_legend=False, type="grouped", colors=chart_colors[:df.index.values.size], ) if df.columns.name: plt.xlabel(df.columns.name.rsplit(" ", 1)[0]) plt.ylim(0, np.amax(df.values)) if not scientific_notation: fig.axes[1].tick_format = ".1f" return fig
def __init__(self, animation_duration=100, aspect_ratio=1, tail_len=1000, lim=2): axes_options = {'x': {'label': 'x'}, 'y': {'label': 'y'}} self.fig = plt.figure(animation_duration=animation_duration, min_aspect_ratio=aspect_ratio, max_aspect_ratio=aspect_ratio) self.line = plt.plot([], [], marker_str='b-', axes_options=axes_options) self.hline = plt.hline(0, opacities=[0], colors=['red'], stroke_width=3) self.scat = plt.plot([], [], 'ro') self.tail_len = tail_len plt.xlim(-lim, lim) plt.ylim(-lim, lim)
def feature_byProperty(features, xProperties, seriesProperty, **kwargs): """Generates a Chart from a set of features. Plots property values of one or more features. Reference: https://developers.google.com/earth-engine/guides/charts_feature#uichartfeaturebyproperty Args: features (ee.FeatureCollection): The features to include in the chart. xProperties (list | dict): One of (1) a list of properties to be plotted on the x-axis; or (2) a (property, label) dictionary specifying labels for properties to be used as values on the x-axis. seriesProperty (str): The name of the property used to label each feature in the legend. Raises: Exception: If the provided xProperties is not a list or dict. Exception: If the chart fails to create. """ try: df = ee_to_df(features) if isinstance(xProperties, list): x_data = xProperties y_data = df[xProperties].values elif isinstance(xProperties, dict): x_data = list(xProperties.values()) y_data = df[list(xProperties.keys())].values else: raise Exception("xProperties must be a list or dictionary.") labels = list(df[seriesProperty]) if "ylim" in kwargs: min_value = kwargs["ylim"][0] max_value = kwargs["ylim"][1] else: min_value = y_data.min() max_value = y_data.max() max_value = max_value + 0.2 * (max_value - min_value) if "title" not in kwargs: title = "" else: title = kwargs["title"] if "legend_location" not in kwargs: legend_location = "top-left" else: legend_location = kwargs["legend_location"] if "display_legend" not in kwargs: display_legend = True else: display_legend = kwargs["display_legend"] fig = plt.figure( title=title, legend_location=legend_location, ) if "width" in kwargs: fig.layout.width = kwargs["width"] if "height" in kwargs: fig.layout.height = kwargs["height"] bar_chart = plt.bar(x=x_data, y=y_data, labels=labels, display_legend=display_legend) bar_chart.type = "grouped" if "colors" in kwargs: bar_chart.colors = kwargs["colors"] if "xlabel" in kwargs: plt.xlabel(kwargs["xlabel"]) if "ylabel" in kwargs: plt.ylabel(kwargs["ylabel"]) plt.ylim(min_value, max_value) if "xlabel" in kwargs and ("ylabel" in kwargs): bar_chart.tooltip = Tooltip( fields=["x", "y"], labels=[kwargs["xlabel"], kwargs["ylabel"]]) else: bar_chart.tooltip = Tooltip(fields=["x", "y"]) plt.show() except Exception as e: raise Exception(e)
def feature_groups(features, xProperty, yProperty, seriesProperty, **kwargs): """Generates a Chart from a set of features. Plots the value of one property for each feature. Reference: https://developers.google.com/earth-engine/guides/charts_feature#uichartfeaturegroups Args: features (ee.FeatureCollection): The feature collection to make a chart from. xProperty (str): Features labeled by xProperty. yProperty (str): Features labeled by yProperty. seriesProperty (str): The property used to label each feature in the legend. Raises: Exception: Errors when creating the chart. """ try: df = ee_to_df(features) df[yProperty] = pd.to_numeric(df[yProperty]) unique_series_values = df[seriesProperty].unique().tolist() new_column_names = [] for value in unique_series_values: sample_filter = (df[seriesProperty] == value).map({ True: 1, False: 0 }) column_name = str(yProperty) + "_" + str(value) df[column_name] = df[yProperty] * sample_filter new_column_names.append(column_name) if "labels" in kwargs: labels = kwargs["labels"] else: labels = [str(x) for x in unique_series_values] if "ylim" in kwargs: min_value = kwargs["ylim"][0] max_value = kwargs["ylim"][1] else: min_value = df[yProperty].to_numpy().min() max_value = df[yProperty].to_numpy().max() max_value = max_value + 0.2 * (max_value - min_value) if "title" not in kwargs: title = "" else: title = kwargs["title"] if "legend_location" not in kwargs: legend_location = "top-left" else: legend_location = kwargs["legend_location"] x_data = list(df[xProperty]) y_data = [df[x] for x in new_column_names] plt.bar(x_data, y_data) fig = plt.figure( title=title, legend_location=legend_location, ) if "width" in kwargs: fig.layout.width = kwargs["width"] if "height" in kwargs: fig.layout.height = kwargs["height"] if "display_legend" not in kwargs: display_legend = True else: display_legend = kwargs["display_legend"] bar_chart = plt.bar(x_data, y_data, labels=labels, display_legend=display_legend) if "colors" in kwargs: bar_chart.colors = kwargs["colors"] if "xlabel" in kwargs: plt.xlabel(kwargs["xlabel"]) if "ylabel" in kwargs: plt.ylabel(kwargs["ylabel"]) plt.ylim(min_value, max_value) if "xlabel" in kwargs and ("ylabel" in kwargs): bar_chart.tooltip = Tooltip( fields=["x", "y"], labels=[kwargs["xlabel"], kwargs["ylabel"]]) else: bar_chart.tooltip = Tooltip(fields=["x", "y"]) plt.show() except Exception as e: raise Exception(e)
def feature_byFeature(features, xProperty, yProperties, **kwargs): """Generates a Chart from a set of features. Plots the value of one or more properties for each feature. Reference: https://developers.google.com/earth-engine/guides/charts_feature#uichartfeaturebyfeature Args: features (ee.FeatureCollection): The feature collection to generate a chart from. xProperty (str): Features labeled by xProperty. yProperties (list): Values of yProperties. Raises: Exception: Errors when creating the chart. """ try: df = ee_to_df(features) if "ylim" in kwargs: min_value = kwargs["ylim"][0] max_value = kwargs["ylim"][1] else: min_value = df[yProperties].to_numpy().min() max_value = df[yProperties].to_numpy().max() max_value = max_value + 0.2 * (max_value - min_value) if "title" not in kwargs: title = "" else: title = kwargs["title"] if "legend_location" not in kwargs: legend_location = "top-left" else: legend_location = kwargs["legend_location"] x_data = list(df[xProperty]) y_data = df[yProperties].values.T.tolist() plt.bar(x_data, y_data) fig = plt.figure( title=title, legend_location=legend_location, ) if "width" in kwargs: fig.layout.width = kwargs["width"] if "height" in kwargs: fig.layout.height = kwargs["height"] if "labels" in kwargs: labels = kwargs["labels"] else: labels = yProperties if "display_legend" not in kwargs: display_legend = True else: display_legend = kwargs["display_legend"] bar_chart = plt.bar(x_data, y_data, labels=labels, display_legend=display_legend) bar_chart.type = "grouped" if "colors" in kwargs: bar_chart.colors = kwargs["colors"] if "xlabel" in kwargs: plt.xlabel(kwargs["xlabel"]) if "ylabel" in kwargs: plt.ylabel(kwargs["ylabel"]) plt.ylim(min_value, max_value) if "xlabel" in kwargs and ("ylabel" in kwargs): bar_chart.tooltip = Tooltip( fields=["x", "y"], labels=[kwargs["xlabel"], kwargs["ylabel"]]) else: bar_chart.tooltip = Tooltip(fields=["x", "y"]) plt.show() except Exception as e: raise Exception(e)
def plot_feature_importances(scenarios, backend): # Plot all the feature importances for id in scenarios: # Make scenario data scenario = scenarios[id] desc = scenario.description figure_name = f"Importance_{id}_{desc}" train_data = scenario.gendata("train") test_data = scenario.gendata("test") np.random.seed(42) X_train, y_train = scenario.get_modelling_data(train_data) X_test, y_test = scenario.get_modelling_data(test_data) dis = scenario.ticks[1] if dis != "SE Asian proxy": # proper noun dis = dis.lower() if "pinterest" in train_data.columns: ren = { "pinterest": scenario.proxy_name, 'disadv_flag': dis, } # warning: train data seems to be writable in-place X_train = X_train.rename(columns=ren) X_test = X_test.rename(columns=ren) clf = LogisticRegression() clf.fit(X_train, y_train) columns = list(X_train.columns) importance = clf.coef_[0] if True: # standardize? importance = importance / X_train.std(axis=0) heights = np.abs(importance) cols = ['orange', 'blue'] colors = (np.array(cols)[(importance >= 0).astype(int)]).tolist() title = "Model Feature Importance" print("Importance: {}".format(importance)) if backend == "matplotlib": fig, ax = mplt.subplots(figsize=(5, 5), dpi=DPI) ax.bar(columns, importance, color=colors) ax.axhline(0, color="Black", lw=1.) # Add some dummies for a legend c = columns[0] mplt.bar([c], [0], color=cols[1], label="Increases Score") mplt.bar([c], [0], color=cols[0], label="Decreases Score") mplt.plot([-0.5, len(columns) - 0.5], [0, 0], 'k') scale = 10 if int(id) == 5: # this scenario uses huge weights for some reason... scale = 40 mplt.ylim(-scale, scale) mplt.legend(bbox_to_anchor=(1.5, 0.5)) ax.set_title(title) fig.savefig("images/" + figure_name + ".png", bbox_inches="tight", dpi=300) elif backend == "bqplot": fig = plt.figure(title=title, min_aspect_ratio=1, max_aspect_ratio=1) for c, h, colr in zip(columns, importance, colors): plt.bar([c], [h], colors=[colr]) # each bar is its own bar plt.ylim(-10, 10) # was -10, 10 except for scenario 5? fig.axes[0].color = fig.axes[1].color = "Black" fig.layout.width = fig.layout.height = "5in" display(fig)
def slab(): """ Displays a widget illustrating line formation in a homogenous slab. Runs only in Jupyter notebook or JupyterLab. Requires bqplot. """ # Don't display some ipywidget warnings warnings.simplefilter(action='ignore', category=FutureWarning) def _compute_slab(i0, source, tau_cont, tau_line): """ Calculates slab line profile. """ NPT = 101 MAX_DX = 5. x = np.arange(NPT) - (NPT - 1.) / 2 x *= MAX_DX / x.max() tau = tau_cont + tau_line * np.exp(-x * x) extinc = np.exp(-tau) intensity = float(i0) * extinc + float(source) * (1. - extinc) return (x, intensity) I0 = 15 S = 65 x, y = _compute_slab(I0, S, 0.5, 0.9) base = np.zeros_like(x) fig = plt.figure(title='Slab line formation') int_plot = plt.plot(x, y, 'b-') source_line = plt.plot(x, base + S, 'k--') i0_line = plt.plot(x, base + I0, 'k:') labels = plt.label(['I₀', 'I', 'S'], x=np.array([int_plot.x[0] + 0.2, int_plot.x[-1] - 0.2, int_plot.x[0] + 0.2]), y=np.array([i0_line.y[0], int_plot.y[0], source_line.y[0]]) + 2, colors=['black']) plt.ylim(0, 100) i0_slider = IntSlider(min=0, max=100, value=I0, description=r'$I_0$') s_slider = IntSlider(min=0, max=100, value=S, description=r'$S$') tau_c_slider = FloatSlider(min=0, max=1., step=0.01, value=0.5, description=r'$\tau_{\mathrm{cont}}$') tau_l_slider = FloatSlider(min=0, max=10., step=0.01, value=0.9, description=r'$\tau_{\mathrm{line}}$') def plot_update(i0=I0, source=S, tau_cont=0.5, tau_line=0.9): _, y = _compute_slab(i0, source, tau_cont, tau_line) int_plot.y = y source_line.y = base + source i0_line.y = base + i0 labels.y = np.array([i0, y[0], source]) + 2 widg = interactive(plot_update, i0=i0_slider, source=s_slider, tau_cont=tau_c_slider, tau_line=tau_l_slider) help_w = HTMLMath("<p><b>Purpose: </b>" "This widget-based procedure is used for " "studying spectral line formation in a " "homogeneous slab.</p>" "<p><b>Inputs:</b></p>" "<ul>" r" <li>$I_0$: The incident intensity.</li>" r" <li>$S$: The source function.</li>" r" <li>$\tau_{\mathrm{cont}}$ : The continuum optical depth.</li>" r" <li>$\tau_{\mathrm{line}}$ : The integrated optical depth in the spectral line.</li>" "</ul>") return HBox([VBox([widg, help_w], layout=Layout(width='33%', top='50px', left='5px')), Box([fig], layout=Layout(width='66%'))], layout=Layout(border='50px'))