Skip to content

Commit b56239e

Browse files
committed
Adds options to plot_ref_sld
1 parent 1dbeedc commit b56239e

File tree

1 file changed

+66
-19
lines changed

1 file changed

+66
-19
lines changed

RATapi/utils/plotting.py

Lines changed: 66 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ def plot_ref_sld_helper(
4848
fig: Optional[matplotlib.pyplot.figure] = None,
4949
delay: bool = True,
5050
confidence_intervals: Union[dict, None] = None,
51+
linear_x: bool = False,
52+
q4: bool = False,
53+
show_error_bar: bool = True,
54+
show_grid: bool = True,
55+
show_legend: bool = True,
5156
):
5257
"""Clears the previous plots and updates the ref and SLD plots.
5358
@@ -63,7 +68,16 @@ def plot_ref_sld_helper(
6368
confidence_intervals : dict or None, default None
6469
The Bayesian confidence intervals for reflectivity and SLD.
6570
Only relevant if the procedure used is Bayesian (NS or DREAM)
66-
71+
linear_x : bool, default: False
72+
Controls whether the x-axis on reflectivity plot uses the linear scale
73+
q4 : bool, default: False
74+
Controls whether Q^4 is plotted on the reflectivity plot
75+
show_error_bar : bool, default: True
76+
Controls whether the error bars are shown
77+
show_grid : bool, default: True
78+
Controls whether the grid is shown
79+
show_legend : bool, default: True
80+
Controls whether the lengend is shown
6781
Returns
6882
-------
6983
fig : matplotlib.pyplot.figure
@@ -93,32 +107,36 @@ def plot_ref_sld_helper(
93107
zip(data.reflectivity, data.shiftedData, data.sldProfiles, data.contrastNames),
94108
):
95109
# Calculate the divisor
96-
div = 1 if i == 0 else 2 ** (4 * (i + 1))
110+
div = 1 if i == 0 and not q4 else 2 ** (4 * (i + 1))
111+
q4_data = 1 if not q4 or not data.dataPresent[i] else sd[:, 0] ** 4
112+
mult = q4_data / div
97113

98114
# Plot the reflectivity on plot (1,1)
99-
ref_plot.plot(r[:, 0], r[:, 1] / div, label=name, linewidth=2)
115+
ref_plot.plot(r[:, 0], r[:, 1] * mult, label=name, linewidth=2)
100116
color = ref_plot.get_lines()[-1].get_color()
101117

102118
# Plot confidence intervals if required
103119
if confidence_intervals is not None:
104120
ref_min, ref_max = confidence_intervals["reflectivity"][i]
105121
# FIXME: remove x-data once rascalsoftware/RAT#249 is merged
106122
ref_x_data = confidence_intervals["reflectivity-x-data"][i][0]
107-
ref_plot.fill_between(ref_x_data, ref_min / div, ref_max / div, alpha=0.6, color="grey")
123+
mult = (1 if not q4 else ref_x_data**4) / div
124+
ref_plot.fill_between(ref_x_data, ref_min * mult, ref_max * mult, alpha=0.6, color="grey")
108125

109126
if data.dataPresent[i]:
110127
sd_x = sd[:, 0]
111-
sd_y, sd_e = map(lambda x: x / div, (sd[:, 1], sd[:, 2]))
128+
sd_y, sd_e = map(lambda x: x * mult, (sd[:, 1], sd[:, 2]))
112129

113-
# Plot the errorbars
114-
indices_removed = np.flip(np.nonzero(sd_y - sd_e < 0)[0])
115-
sd_x_r, sd_y_r, sd_e_r = map(lambda x: np.delete(x, indices_removed), (sd_x, sd_y, sd_e))
116-
plot_errorbars(ref_plot, sd_x_r, sd_y_r, sd_e_r, False, color)
130+
if show_error_bar:
131+
# Plot the errorbars
132+
indices_removed = np.flip(np.nonzero(sd_y - sd_e < 0)[0])
133+
sd_x_r, sd_y_r, sd_e_r = map(lambda x: np.delete(x, indices_removed), (sd_x, sd_y, sd_e))
134+
plot_errorbars(ref_plot, sd_x_r, sd_y_r, sd_e_r, False, color)
117135

118-
# Plot one sided errorbars
119-
indices_selected = [x for x in indices_removed if x not in np.nonzero(sd_y < 0)[0]]
120-
sd_x_s, sd_y_s, sd_e_s = map(lambda x: [x[i] for i in indices_selected], (sd_x, sd_y, sd_e))
121-
plot_errorbars(ref_plot, sd_x_s, sd_y_s, sd_e_s, True, color)
136+
# Plot one sided errorbars
137+
indices_selected = [x for x in indices_removed if x not in np.nonzero(sd_y < 0)[0]]
138+
sd_x_s, sd_y_s, sd_e_s = map(lambda x: [x[i] for i in indices_selected], (sd_x, sd_y, sd_e))
139+
plot_errorbars(ref_plot, sd_x_s, sd_y_s, sd_e_s, True, color)
122140

123141
# Plot the slds on plot (1,2)
124142
for j in range(len(sld)):
@@ -156,16 +174,21 @@ def plot_ref_sld_helper(
156174

157175
# Format the axis
158176
ref_plot.set_yscale("log")
159-
ref_plot.set_xscale("log")
177+
if not linear_x:
178+
ref_plot.set_xscale("log")
160179
ref_plot.set_xlabel("$Q_{z} (\u00c5^{-1})$")
161180
ref_plot.set_ylabel("Reflectivity")
162-
ref_plot.legend()
163-
ref_plot.grid()
164181

165182
sld_plot.set_xlabel("$Z (\u00c5)$")
166183
sld_plot.set_ylabel("$SLD (\u00c5^{-2})$")
167-
sld_plot.legend()
168-
sld_plot.grid()
184+
185+
if show_legend:
186+
ref_plot.legend()
187+
sld_plot.legend()
188+
189+
if show_grid:
190+
ref_plot.grid()
191+
sld_plot.grid()
169192

170193
if preserve_zoom:
171194
fig.canvas.toolbar.back()
@@ -181,6 +204,11 @@ def plot_ref_sld(
181204
block: bool = False,
182205
return_fig: bool = False,
183206
bayes: Literal[65, 95, None] = None,
207+
linear_x: bool = False,
208+
q4: bool = False,
209+
show_error_bar: bool = True,
210+
show_grid: bool = True,
211+
show_legend: bool = True,
184212
) -> Union[plt.Figure, None]:
185213
"""Plots the reflectivity and SLD profiles.
186214
@@ -198,6 +226,16 @@ def plot_ref_sld(
198226
Whether to shade Bayesian confidence intervals. Can be `None`
199227
(if no intervals), `65` to show 65% confidence intervals,
200228
and `95` to show 95% confidence intervals.
229+
linear_x : bool, default: False
230+
Controls whether the x-axis on reflectivity plot uses the linear scale
231+
q4 : bool, default: False
232+
Controls whether Q^4 is plotted on the reflectivity plot
233+
show_error_bar : bool, default: True
234+
Controls whether the error bars are shown
235+
show_grid : bool, default: True
236+
Controls whether the grid is shown
237+
show_legend : bool, default: True
238+
Controls whether the lengend is shown
201239
202240
Returns
203241
-------
@@ -253,7 +291,16 @@ def plot_ref_sld(
253291

254292
figure = plt.subplots(1, 2)[0]
255293

256-
plot_ref_sld_helper(data, figure, confidence_intervals=confidence_intervals)
294+
plot_ref_sld_helper(
295+
data,
296+
figure,
297+
confidence_intervals=confidence_intervals,
298+
linear_x=linear_x,
299+
q4=q4,
300+
show_error_bar=show_error_bar,
301+
show_grid=show_grid,
302+
show_legend=show_legend,
303+
)
257304

258305
if return_fig:
259306
return figure

0 commit comments

Comments
 (0)