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 plot_drought_deficit_mpl(scenario, annual_data, quantile): flowdata = scenario.get_data() if scenario.critical_season_begin and scenario.critical_season_end: season = ( scenario.critical_season_begin, scenario.critical_season_end ) else: season = None drought_analysis = analysis.DroughtYearFromFlowAnalysis( flowdata[scenario.get_attribute_name()], quantile, season = season ) data = annual_data.to_frame(name='Annual').merge( drought_analysis.label_years().to_frame("In Drought"), how='left', left_index=True, right_index=True ).dropna() fig, ax = new_figure() data['Annual'].plot( kind='bar', ax=ax, color=data['In Drought'].map({True: 'r', False: 'g'}), # legend=True, ) # Put in the legend to signal drought and non-drought years. nodrought = plt.Rectangle((0, 1), 50, 50, color='g') drought = plt.Rectangle((0, 1), 50, 50, color='r') ax.legend([nodrought, drought], ["Non-Drought Year", "Drought Year"]) return fig, ax
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 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_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 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 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 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)
def plot_drought_deficit_mpl(scenario, annual_data, quantile): flowdata = scenario.get_data() if scenario.critical_season_begin and scenario.critical_season_end: season = (scenario.critical_season_begin, scenario.critical_season_end) else: season = None drought_analysis = analysis.DroughtYearFromFlowAnalysis( flowdata[scenario.get_attribute_name()], quantile, season=season) data = annual_data.to_frame(name='Annual').merge( drought_analysis.label_years().to_frame("In Drought"), how='left', left_index=True, right_index=True).dropna() fig, ax = new_figure() data['Annual'].plot( kind='bar', ax=ax, color=data['In Drought'].map({ True: 'r', False: 'g' }), legend=True, ) return fig, ax
def __setup_scenario_plot(scenario_id): scenario = get_object_or_404(Scenario, pk=scenario_id) plt.style.use(DEFAULT_PLOT_STYLE) return scenario, new_figure()