def __dataframe_barplot_helper(request, project_id, title, analysis_f, units=None, formatter=None, xlim=None, ylim=None): project = get_object_or_404(Project, pk=project_id) data = __get_deficit_stats_comparison(project, analysis_f, units) plt.style.use(DEFAULT_PLOT_STYLE) fig, ax = new_figure() data.plot(kind='bar', ax=ax, table=False) ax.set_title(title) if formatter: ax.yaxis.set_major_formatter(formatter) ylabel = "Volume" ax.set_ylabel("Volume (%s)" % units) if xlim: ax.set_xlim(xlim) if ylim: ax.set_ylim(ylim) return plot_to_response(fig)
def deficit_stats_plot_annual(request, scenario_id): scenario, (fig, ax) = __setup_scenario_plot(scenario_id) ax.set_ylabel(__label_volume_attribute(scenario)) ax.set_xlabel("Year") title = None plotting.volume_deficit_annual(scenario.get_data(), scenario.get_gap_attribute_name(), title, fig, ax) return plot_to_response(fig)
def deficit_stats_plot_annual(request, scenario_id): scenario, (fig, ax) = __setup_scenario_plot(scenario_id) ax.set_ylabel(__label_volume_attribute(scenario)) title = "Annual Volume Deficit" plotting.volume_deficit_annual(scenario.get_data(), scenario.get_gap_attribute_name(), title, fig, ax) return plot_to_response(fig)
def volume_deficit_drought_plot(request, scenario_id): scenario = get_object_or_404(Scenario, pk=scenario_id) quantile = 1.0 - (scenario.drought_exceedance / 100.0) plt.style.use(DEFAULT_PLOT_STYLE) fig, ax = plots.plot_drought_volume_deficit_mpl(scenario, quantile) ax.set_xlabel("Year") ax.set_ylabel("Volume (af)") return plot_to_response(fig)
def temporal_deficit_drought_plot(request, scenario_id): scenario = get_object_or_404(Scenario, pk=scenario_id) quantile = 1.0 - (scenario.drought_exceedance / 100.0) plt.style.use(DEFAULT_PLOT_STYLE) fig, ax = plots.plot_drought_temporal_deficit_mpl(scenario, quantile) ax.yaxis.set_major_formatter(FuncFormatter(to_percent)) ax.set_xlabel("Year") ax.set_ylabel("Percent of days") return plot_to_response(fig)
def deficit_stats_pct_plot_annual(request, scenario_id): scenario, (fig, ax) = __setup_scenario_plot(scenario_id) ax.yaxis.set_major_formatter(FuncFormatter(to_percent)) ax.set_ylim([0.0, 1.0]) plotting.volume_deficit_pct_annual( scenario.get_data(), scenario.get_gap_attribute_name(), scenario.get_target_attribute_name(), "Annual Volume Deficit Relative to Target", fig, ax) return plot_to_response(fig)
def right_plot(request, scenario_id): scenario = get_object_or_404(Scenario, pk=scenario_id) plotdata = target_comparison_data(scenario) plt.style.use(DEFAULT_PLOT_STYLE) fig, ax = new_figure() plotdata.plot(ax=ax) ax.set_xlabel("Month") ax.set_ylabel(__label_scenario_attribute(scenario)) plotting.label_months(ax) return plot_to_response(fig)
def dynamic_raster(request, scenario_id): scenario = get_object_or_404(Scenario, pk=scenario_id) data = scenario.get_data() # Get visualization parameters attribute = request.GET.get('attribute', None) if not attribute: return HttpResponseBadRequest() cmap = request.GET.get('cmap', None) title = request.GET.get('title', None) zero = request.GET.get('zero', 'False') logscale = request.GET.get('logscale', 'False') zero = zero == "True" logscale = logscale == "True" plt.style.use(DEFAULT_PLOT_STYLE) fig = Figure() ax = fig.add_subplot(111) if not zero: min_value = data[attribute].min() max_value = data[attribute].max() else: min_value = data[attribute].min() max_value = abs(data[attribute].min()) if cmap: colormap = cm.get_cmap(cmap) if zero: colormap = plotting.create_colormap(data, attribute, colormap, vmin=min_value, vmax=max_value) colormap.set_bad('black') else: colormap = None if logscale: if min_value <= 0: min_value = 0.001 norm = matplotlib.colors.LogNorm(vmin=min_value, vmax=max_value) else: norm = None plotting.rasterplot(data, attribute, title, show_colorbar=True, norm=norm, colormap=colormap, vmin=min_value, vmax=max_value, fig=fig, ax=ax) return plot_to_response(fig)
def project_low_flow_plot(request, project_id): low_flows = get_low_flows(project_id) plt.style.use(DEFAULT_PLOT_STYLE) fig, ax = new_figure() low_flows.plot(kind='bar', fig=fig, ax=ax) #ax.yaxis.set_major_formatter(FuncFormatter(to_percent)) ax.set_title("7-day Minimum Flow Trend") labels = ax.get_xticklabels() for label in labels: label.set_rotation(0) return plot_to_response(fig)
def deficit_days_plot(request, scenario_id): scenario = get_object_or_404(Scenario, pk=scenario_id) data = scenario.get_data() plt.style.use(DEFAULT_PLOT_STYLE) fig, ax = new_figure() title = "Monthly Temporal Deficit" ax = plotting.deficit_days_plot(data, scenario.get_gap_attribute_name(), title, fig, ax) ax.yaxis.set_major_formatter(FuncFormatter(to_percent)) ax.set_ylim([0.0, 1.0]) return plot_to_response(fig)
def project_deficit_days_plot(request, project_id): project = get_object_or_404(Project, pk=project_id) data = __get_deficit_days_comparison( project, lambda d, g, t: analysis.monthly_deficit_pct(d, g), "Month") plt.style.use(DEFAULT_PLOT_STYLE) fig, ax = new_figure() data.plot(kind='bar', ax=ax, table=False) ax.set_title("Deficit days comparison") ax.yaxis.set_major_formatter(FuncFormatter(to_percent)) ax.set_ylim([0.0, 1.0]) return plot_to_response(fig)
def deficit_days_plot(request, scenario_id): scenario = get_object_or_404(Scenario, pk=scenario_id) data = scenario.get_data() plt.style.use(DEFAULT_PLOT_STYLE) fig, ax = new_figure() title = None ax = plotting.deficit_days_plot(data, scenario.get_gap_attribute_name(), title, fig, ax) ax.yaxis.set_major_formatter(FuncFormatter(to_percent)) ax.set_ylim([0.0, 1.0]) ax.set_ylabel("Percent of Days") ax.set_xlabel("Month") return plot_to_response(fig)
def project_deficit_days_plot(request, project_id): project = get_object_or_404(Project, pk=project_id) data = __get_deficit_days_comparison( project, lambda d, g, t: analysis.monthly_deficit_pct(d, g), "Month" ) plt.style.use(DEFAULT_PLOT_STYLE) fig, ax = new_figure() data.plot(kind='bar', ax=ax, table=False) ax.set_title("Deficit days comparison") ax.yaxis.set_major_formatter(FuncFormatter(to_percent)) ax.set_ylim([0.0, 1.0]) return plot_to_response(fig)
def deficit_stats_pct_plot_annual(request, scenario_id): scenario, (fig, ax) = __setup_scenario_plot(scenario_id) ax.yaxis.set_major_formatter(FuncFormatter(to_percent)) ax.set_ylim([0.0, 1.0]) ax.set_ylabel("Percent of Target") ax.set_xlabel("Year") plotting.volume_deficit_pct_annual( scenario.get_data(), scenario.get_gap_attribute_name(), scenario.get_target_attribute_name(), None, fig, ax ) return plot_to_response(fig)
def long_term_minimum_plot(request, scenario_id): scenario = get_object_or_404(Scenario, pk=scenario_id) data = scenario.get_data() column = scenario.get_attribute_name() min_data = analysis.annual_minimum(data[column], 7, True) plt.style.use(DEFAULT_PLOT_STYLE) fig, ax = new_figure() plotting.plot_with_trendline_ols( min_data, title=None, fig=fig, ax=ax) ax.set_xlabel("Year") ax.set_ylabel(__label_scenario_attribute(scenario)) return plot_to_response(fig)
def long_term_minimum_plot(request, scenario_id): scenario = get_object_or_404(Scenario, pk=scenario_id) data = scenario.get_data() column = scenario.get_attribute_name() min_data = analysis.annual_minimum(data[column], 7, True) plt.style.use(DEFAULT_PLOT_STYLE) fig, ax = new_figure() plotting.plot_with_trendline_ols(min_data, title="7-day Minimum Flow", fig=fig, ax=ax) ax.set_xlabel("Year") ax.set_ylabel(__label_scenario_attribute(scenario)) return plot_to_response(fig)
def get(self, request, scenario_id): scenario = get_object_or_404(Scenario, pk=scenario_id) if scenario.instream_flow_rights: targetdata = scenario.get_data()[scenario.get_target_attribute_name()] flowdata = scenario.get_data()[scenario.get_attribute_name()] ratedata = self.get_rate_data( scenario.instream_flow_rights, targetdata.index.min(), targetdata.index.max() ) plt.style.use(DEFAULT_PLOT_STYLE) fig, ax = new_figure() self.plot(ax, targetdata, flowdata, ratedata) return plot_to_response(fig) else: raise Http404("Scenario has no instream flow rights.")
def get(self, request, scenario_id): scenario = get_object_or_404(Scenario, pk=scenario_id) plt.style.use(DEFAULT_PLOT_STYLE) # Crop data. if scenario.crop_mix: crop_mix, data, years, commodities = read_crop_mix( scenario.crop_mix.id) groups = [ g.as_cropgroup() for g in crop_mix.cropmixgroup_set.all() ] dataframe = self.data_function(data, groups) fig, ax = new_figure() self.plot_function(dataframe, ax) ax.set_xlabel(self.xlabel) ax.set_ylabel(self.ylabel) return plot_to_response(fig) else: raise Http404("Scenario contains no agriculture data.")
def get(self, request, scenario_id): scenario = get_object_or_404(Scenario, pk=scenario_id) plt.style.use(DEFAULT_PLOT_STYLE) # Crop data. if scenario.crop_mix: crop_mix, data, years, commodities = read_crop_mix(scenario.crop_mix.id) groups = [g.as_cropgroup() for g in crop_mix.cropmixgroup_set.all()] dataframe = self.data_function(crop_mix, data, groups) if isinstance(dataframe, pd.DataFrame): dataframe_reduced = waterkit.econ.analysis.select_top_n_columns(dataframe, 6) else: dataframe_reduced = dataframe fig, ax = new_figure() ax.set_xlabel(self.xlabel) ax.set_ylabel(self.ylabel) self.plot_function(dataframe_reduced, ax) return plot_to_response(fig) else: raise Http404("Scenario contains no agriculture data.")
def dynamic_raster(request, scenario_id): scenario = get_object_or_404(Scenario, pk=scenario_id) data = scenario.get_data() # Get visualization parameters attribute = request.GET.get('attribute', None) if not attribute: return HttpResponseBadRequest() cmap = request.GET.get('cmap', None) zero = request.GET.get('zero', 'False') logscale = request.GET.get('logscale', 'False') zero = zero == "True" logscale = logscale == "True" plt.style.use(DEFAULT_PLOT_STYLE) fig = Figure() ax = fig.add_subplot(111) if not zero: min_value = data[attribute].min() max_value = data[attribute].max() else: min_value = data[attribute].min() max_value = abs(data[attribute].min()) if cmap: colormap = cm.get_cmap(cmap) if zero: colormap = plotting.create_colormap(data, attribute, colormap, vmin=min_value, vmax=max_value) colormap.set_bad('black') else: colormap = None if logscale: if min_value <= 0: min_value = 0.001 norm = matplotlib.colors.LogNorm(vmin=min_value, vmax=max_value) else: norm = None plotting.rasterplot(data, attribute, None, show_colorbar=True, norm=norm, colormap=colormap, vmin=min_value, vmax=max_value, fig=fig, ax=ax) return plot_to_response(fig)
def right_plot(request, scenario_id): scenario = get_object_or_404(Scenario, pk=scenario_id) data = scenario.get_data() daygroups = data.groupby(lambda x: x.dayofyear) low = daygroups[scenario.get_attribute_name()].quantile(0.2) median = daygroups[scenario.get_attribute_name()].quantile(0.5) high = daygroups[scenario.get_attribute_name()].quantile(0.8) target = daygroups[scenario.get_target_attribute_name()].mean() plotdata = pd.concat([low, median, high, target], axis=1) plotdata.columns = ["80% Exceedance", "Median", "20% Exceedance", "Target"] plt.style.use(DEFAULT_PLOT_STYLE) fig, ax = new_figure() plotdata.plot(ax=ax) ax.set_xlabel("Month") ax.set_ylabel(__label_scenario_attribute(scenario)) plotting.label_months(ax) return plot_to_response(fig)