Skip to content

Commit 45486aa

Browse files
authored
Merge pull request #123 from mfmceneaney/update_systematics_plots
Update systematics plots
2 parents 561258b + 2235503 commit 45486aa

3 files changed

Lines changed: 83 additions & 17 deletions

File tree

py/saga/plot.py

Lines changed: 74 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,40 @@ def plot_watermark(
161161
)
162162

163163

164+
def plot_label(
165+
ax1,
166+
plot_label_args=None,
167+
):
168+
"""
169+
Parameters
170+
----------
171+
ax1 : matplotlib.axes._axes.Axes, required
172+
Matplotlib.pyplot figure axis
173+
plot_label_args : list, optional
174+
List of positional arguments for plot label
175+
176+
Description
177+
-----------
178+
Plot a text label.
179+
"""
180+
if plot_label_args is None:
181+
plot_label_args = [0.95, 0.15, None]
182+
if (
183+
isinstance(plot_label_args, tuple)
184+
or isinstance(plot_label_args, list)
185+
and len(plot_label_args) == 3
186+
and isinstance(plot_label_args[-1], str)
187+
):
188+
ax1.text(
189+
*plot_label_args,
190+
transform=ax1.transAxes,
191+
fontsize=plt.rcParams["axes.titlesize"],
192+
fontweight="bold",
193+
va="top",
194+
ha="right",
195+
)
196+
197+
164198
def plot_vlines(
165199
hist,
166200
binlims=None,
@@ -704,7 +738,9 @@ def plot_systematics(
704738
watermark="CLAS12 Preliminary",
705739
watermark_kwargs=None,
706740
use_default_plt_settings=True,
707-
legend_loc="upper left",
741+
title_pad=20,
742+
lg_kwargs=None,
743+
plot_label_args=None,
708744
axlinewidth=1.0,
709745
log=False,
710746
figsize=(16, 10),
@@ -742,8 +778,12 @@ def plot_systematics(
742778
Optional key word arguments for :meth:`plot_watermark`
743779
use_default_plt_settings : bool, optional
744780
Option to use default font and tick parameter style settings
745-
legend_loc : str, optional
746-
Matplotlib.pyplot legend location string, will not be plotted if set to :obj:`None` or :obj:`''`
781+
title_pad : int, optional
782+
Title padding
783+
lg_kwargs : dict, optional
784+
Matplotlib.pyplot legend keyword arguments
785+
plot_label_args : list, optional
786+
List of positional arguments for plot label
747787
axlinewidth : float, optional
748788
Axis line and injected asymmetries line width
749789
log : bool, optional
@@ -757,6 +797,14 @@ def plot_systematics(
757797
Save systematics breakdowns to CSV in :obj:`<outpath>.csv`. Note that this does **not** allow for asymmetric errors.
758798
"""
759799

800+
# Check arguments
801+
if lg_kwargs is None:
802+
lg_kwargs = {
803+
"loc": "upper left",
804+
"bbox_to_anchor": (1.05, 1.0),
805+
"frameon": False,
806+
}
807+
760808
# Set color palette
761809
sbn.set_palette(palette)
762810

@@ -768,7 +816,7 @@ def plot_systematics(
768816
f1, ax1 = plt.subplots(figsize=figsize)
769817
plt.xlim(*xlims)
770818
plt.ylim(*ylims)
771-
plt.title(title, usetex=True)
819+
plt.title(title, usetex=True, pad=title_pad)
772820
plt.xlabel(xtitle, usetex=True)
773821
plt.ylabel(ytitle, usetex=True)
774822

@@ -797,8 +845,11 @@ def plot_systematics(
797845
plot_watermark(ax1, watermark=watermark, **watermark_kwargs)
798846

799847
# Plot legend
800-
if legend_loc is not None and legend_loc != "":
801-
ax1.legend(loc=legend_loc)
848+
if isinstance(lg_kwargs, dict):
849+
ax1.legend(**lg_kwargs)
850+
851+
# Plot label
852+
plot_label(ax1, plot_label_args)
802853

803854
# Save figure
804855
f1.savefig(outpath)
@@ -856,7 +907,9 @@ def plot_results(
856907
watermark="CLAS12 Preliminary",
857908
watermark_kwargs=None,
858909
show_injected_asymmetries=False,
859-
legend_loc="upper left",
910+
title_pad=20,
911+
lg_kwargs=None,
912+
plot_label_args=None,
860913
ecolor="black",
861914
elinewidth=2.0,
862915
capsize=18,
@@ -956,8 +1009,12 @@ def plot_results(
9561009
Optional key word arguments for :meth:`plot_watermark`
9571010
show_injected_asymmetries : bool, optional
9581011
Option to show injected signal and background asymmetries
959-
legend_loc : str, optional
960-
Matplotlib.pyplot legend location string, will not be plotted if set to :obj:`None` or :obj:`''`
1012+
title_pad : int, optional
1013+
Title padding
1014+
lg_kwargs : dict, optional
1015+
Matplotlib.pyplot legend keyword arguments
1016+
plot_label_args : list, optional
1017+
List of positional arguments for plot label
9611018
ecolor : str, optional
9621019
Error line color
9631020
ecolor : float, optional
@@ -1049,6 +1106,8 @@ def plot_results(
10491106
sgasyms = [0.10]
10501107
if sg_colors is None:
10511108
sg_colors = ["blue"]
1109+
if lg_kwargs is None:
1110+
lg_kwargs = {"loc": "best", "frameon": False}
10521111

10531112
# Rescale graph
10541113
scaling, acceptanceratio = None, None
@@ -1093,7 +1152,7 @@ def plot_results(
10931152
# Set up plot
10941153
ax1.set_xlim(*xlims)
10951154
ax1.set_ylim(*ylims)
1096-
ax1.set_title(title, usetex=True)
1155+
ax1.set_title(title, usetex=True, pad=title_pad)
10971156
ax1.set_xlabel(xlabel, usetex=True)
10981157
ax1.set_ylabel(ylabel, usetex=True)
10991158

@@ -1207,8 +1266,11 @@ def plot_results(
12071266
plot_watermark(ax1, watermark=watermark, **watermark_kwargs)
12081267

12091268
# Plot legend
1210-
if legend_loc is not None and legend_loc != "":
1211-
ax1.legend(loc=legend_loc)
1269+
if isinstance(lg_kwargs, dict):
1270+
ax1.legend(**lg_kwargs)
1271+
1272+
# Plot label
1273+
plot_label(ax1, plot_label_args)
12121274

12131275
# Check whether you have graph data to save to CSV
12141276
if ct_mean is None:

py/saga/rescale.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,10 @@ def rescale_graph_data(
135135
y_mean = new_sim_graph["y"]
136136

137137
# Set y values to constant and update scaled y errors if requested
138-
scaled_y_mean = y_mean if yvalue < -1 else [np.abs(yvalue) for i in range(len(y_mean))]
139-
if yvalue >= -1 and yvalue<0:
138+
scaled_y_mean = (
139+
y_mean if yvalue < -1 else [np.abs(yvalue) for i in range(len(y_mean))]
140+
)
141+
if yvalue >= -1 and yvalue < 0:
140142
scaled_yerr_mean = (
141143
np.divide(1.0, np.sqrt(scaled_ct_mean)) * 1.0 / (tpol_factor * tdil_factor)
142144
) * np.sqrt(1 - np.square(yvalue * tpol_factor * tdil_factor))
@@ -281,9 +283,11 @@ def rescale_csv_data(
281283
if yvalue < -1
282284
else [np.abs(yvalue) for i in range(len(old_dat_df[y_key]))]
283285
)
284-
if yvalue >= -1 and yvalue<0:
286+
if yvalue >= -1 and yvalue < 0:
285287
scaled_yerrs = (
286-
np.divide(1.0, np.sqrt(new_dat_df_count)) * 1.0 / (tpol_factor * tdil_factor)
288+
np.divide(1.0, np.sqrt(new_dat_df_count))
289+
* 1.0
290+
/ (tpol_factor * tdil_factor)
287291
) * np.sqrt(1 - np.square(yvalue * tpol_factor * tdil_factor))
288292
elif yvalue >= 0 and yvalue <= 1:
289293
scaled_yerrs *= np.sqrt(1 - np.square(yvalue * tpol_factor * tdil_factor))

tutorials/aggregate_jobs_nested_kinematics2d.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
'hist_density':False,
4444
'axlinewidth':0,
4545
'hist_dim':2,
46-
'legend_loc':None #NOTE: Do not plot a legend since you are using 2d hists.
46+
'lg_kwargs':None #NOTE: Do not plot a legend since you are using 2d hists.
4747
}
4848

4949
# Set additional kwargs

0 commit comments

Comments
 (0)