From 6c75012e20029c18eff2066632dc99d14bb75212 Mon Sep 17 00:00:00 2001 From: Anastasia Date: Fri, 7 Nov 2025 21:55:06 -0900 Subject: [PATCH 1/2] Added date column label to sw new df and then made barplot for ice loss --- data/new_sw_monthly_resampled.csv | 2 +- scripts/flash_talk.py | 79 +++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+), 1 deletion(-) create mode 100644 scripts/flash_talk.py diff --git a/data/new_sw_monthly_resampled.csv b/data/new_sw_monthly_resampled.csv index 23e155b..42a1a2d 100644 --- a/data/new_sw_monthly_resampled.csv +++ b/data/new_sw_monthly_resampled.csv @@ -1,4 +1,4 @@ -,sw_grace_gt_month,sw_grace_diff_gt_month,sw_avg_ice_disch_gt_month,sw_runoff_gt_month,sw_sum_ppt_gt_each_month,sw_monthly_avg_temp_2m_K,sw_total_ice_loss_gt_month,sw_ice_discharge_percent,sw_grace_diffs_gt_month +Date,sw_grace_gt_month,sw_grace_diff_gt_month,sw_avg_ice_disch_gt_month,sw_runoff_gt_month,sw_sum_ppt_gt_each_month,sw_monthly_avg_temp_2m_K,sw_total_ice_loss_gt_month,sw_ice_discharge_percent,sw_grace_diffs_gt_month 2009-01-31,215.0,78.0,1.6117628815,0.084670573,3.98366227,252.4179159,-1.6964334545,95.00890690551988,-75.7127711845 2009-02-28,191.0,-24.0,1.4594225319166665,0.066331881,3.217816942,250.8445241,-1.5257544129166665,95.6525191447293,25.692062529083334 2009-03-31,190.0,-1.0,1.6212689935,0.063233942,4.451839355,248.1550624,-1.6845029355,96.24613643185901,3.7673364194999994 diff --git a/scripts/flash_talk.py b/scripts/flash_talk.py new file mode 100644 index 0000000..3b23ca6 --- /dev/null +++ b/scripts/flash_talk.py @@ -0,0 +1,79 @@ +import pandas as pd +import matplotlib.pyplot as plt +from matplotlib.dates import DateFormatter + +from create_big_df import import_file +from plot_styling import fontsize_style + +from splots.saxes import thic_plot + + +def get_sw_file() -> pd.DataFrame: + """Returns my SW data file with columns + + ['sw_grace_gt_month', 'sw_grace_diff_gt_month', + 'sw_avg_ice_disch_gt_month', 'sw_runoff_gt_month', + 'sw_sum_ppt_gt_each_month', 'sw_monthly_avg_temp_2m_K', + 'sw_total_ice_loss_gt_month', 'sw_ice_discharge_percent', + 'sw_grace_diffs_gt_month'] + + and time index set. + + Returns: + pd.DataFrame: + """ + sw = import_file( + filename="new_sw_monthly_resampled.csv", + date_format="%Y-%m-%d", + parse_dates=["Date"], + ) + + sw.set_index("Date", inplace=True) + + return sw + + +def mass_loss_barplot(): + fontstyle = fontsize_style() + + sw = get_sw_file() + barplot_df = sw[ + [ + "sw_avg_ice_disch_gt_month", + "sw_runoff_gt_month", + ] + ] + + # Want the annual ice discharge and runoff so sum instead of average + barplot_df = barplot_df.resample("YE").sum() + # Rename columns for legend + barplot_df = barplot_df.rename( + columns={ + "sw_avg_ice_disch_gt_month": "Ice discharge", + "sw_runoff_gt_month": "Runoff", + } + ) + # All the dates said 1970 if this was not done + barplot_df.index = barplot_df.index.year.astype(str) + + ax = barplot_df.plot( + kind="bar", + stacked=True, + color={"Ice discharge": "#8d5fa8", "Runoff": "#fdb950"}, + ) + + ax.set_ylabel("Annual Sum Total Loss / Gt $\\cdot$ yr$^{{-1}}$", **fontstyle) + ax.set_xlabel("Year", **fontstyle) + + ax.legend(frameon=False, fontsize=22) + thic_plot(ax) + + plt.show() + + +def main(): + mass_loss_barplot() + + +if __name__ == "__main__": + main() From b545a3379a6aa969b0ea434a49945c1490e572c2 Mon Sep 17 00:00:00 2001 From: Anastasia Date: Sat, 8 Nov 2025 20:35:48 -0900 Subject: [PATCH 2/2] Created linestyle function and also finished off rest of plots for flash talk --- scripts/flash_talk.py | 66 +++++++++++++++++++++++++++++++++++++++---- src/plot_styling.py | 10 +++++++ 2 files changed, 70 insertions(+), 6 deletions(-) diff --git a/scripts/flash_talk.py b/scripts/flash_talk.py index 3b23ca6..f596382 100644 --- a/scripts/flash_talk.py +++ b/scripts/flash_talk.py @@ -1,9 +1,8 @@ import pandas as pd import matplotlib.pyplot as plt -from matplotlib.dates import DateFormatter from create_big_df import import_file -from plot_styling import fontsize_style +from plot_styling import fontsize_style, line_style from splots.saxes import thic_plot @@ -33,7 +32,9 @@ def get_sw_file() -> pd.DataFrame: return sw -def mass_loss_barplot(): +def mass_loss_barplot() -> None: + """Plots yearly sum of ID and R that is color coded + """ fontstyle = fontsize_style() sw = get_sw_file() @@ -59,10 +60,11 @@ def mass_loss_barplot(): ax = barplot_df.plot( kind="bar", stacked=True, - color={"Ice discharge": "#8d5fa8", "Runoff": "#fdb950"}, + color={"Ice discharge": "#fdb950", "Runoff": "#8d5fa8"}, + width=0.9 ) - ax.set_ylabel("Annual Sum Total Loss / Gt $\\cdot$ yr$^{{-1}}$", **fontstyle) + ax.set_ylabel("SW Annual Sum Total Loss / (Gt $\\cdot$ yr$^{{-1}}$)", **fontstyle) ax.set_xlabel("Year", **fontstyle) ax.legend(frameon=False, fontsize=22) @@ -70,10 +72,62 @@ def mass_loss_barplot(): plt.show() +def ice_discharge_runoff() -> None: + """Plots ID and R in subplot line form + """ + fontstyle = fontsize_style() + linestyle = line_style() + + sw = get_sw_file() + + fig, (ax1, ax2) = plt.subplots(nrows=2, ncols=1, sharex=True) + + x_time = sw.index + # ice discharge + ax1.plot(x_time, sw["sw_avg_ice_disch_gt_month"], **linestyle) + ax1.set_ylabel("SW Avg Ice Discharge /\n(Gt $\\cdot$ month$^{{-1}}$)", **fontstyle) + # runoff + ax2.plot(x_time, sw["sw_runoff_gt_month"], **linestyle) + ax2.set_ylabel("SW Runoff /\n(Gt $\\cdot$ month$^{{-1}}$)", **fontstyle) + ax2.set_xlabel("Date", **fontstyle) + + for ax in [ax1, ax2]: + thic_plot(ax) + + plt.show() + +def pred_mb() -> None: + """Plots P - R - D- aka predicted MB + """ + fontstyle = fontsize_style() + linestyle = line_style() + + sw = get_sw_file() + + fig, ax = plt.subplots() + + # Calculate p - r - d + y_pred_mb = ( + sw["sw_sum_ppt_gt_each_month"] + - sw["sw_runoff_gt_month"] + - sw["sw_avg_ice_disch_gt_month"] + ) + + ax.plot(sw.index, y_pred_mb, **linestyle) + ax.set_ylabel("SW Predicted MB ≝ (P - R - ID)/ (Gt $\\cdot$ month$^{{-1}}$)", **fontstyle) + ax.set_xlabel("Date", **fontstyle) + thic_plot(ax) + + plt.show() + def main(): - mass_loss_barplot() + #pred_mb() + pd.set_option("display.max_columns", None) + df = get_sw_file() + describe_df = df.describe() + print(describe_df.loc[["min", "max"]]) if __name__ == "__main__": main() diff --git a/src/plot_styling.py b/src/plot_styling.py index 49bff6e..d8863ef 100644 --- a/src/plot_styling.py +++ b/src/plot_styling.py @@ -20,3 +20,13 @@ def scatter_style() -> dict[str, float | str]: """ style = {"s": 100, "edgecolor": "white", "linewidth": 0.01, "c": "#ffa95e"} return style + +def line_style() -> dict[str, float | str]: + """Unpack in line plotting call + + Returns: + dict[str, float | str]: + """ + style = {"color": "#8d5fa8", + "linewidth": 3} + return style