Skip to content

Commit cbb8021

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

File tree

1 file changed

+61
-21
lines changed

1 file changed

+61
-21
lines changed

RATapi/utils/plotting.py

Lines changed: 61 additions & 21 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,37 @@ 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+
131+
if show_error_bar:
132+
# Plot the errorbars
133+
indices_removed = np.flip(np.nonzero(sd_y - sd_e < 0)[0])
134+
sd_x_r, sd_y_r, sd_e_r = map(lambda x: np.delete(x, indices_removed), (sd_x, sd_y, sd_e))
135+
plot_errorbars(ref_plot, sd_x_r, sd_y_r, sd_e_r, False, color)
117136

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)
137+
# Plot one sided errorbars
138+
indices_selected = [x for x in indices_removed if x not in np.nonzero(sd_y < 0)[0]]
139+
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))
140+
plot_errorbars(ref_plot, sd_x_s, sd_y_s, sd_e_s, True, color)
122141

123142
# Plot the slds on plot (1,2)
124143
for j in range(len(sld)):
@@ -156,16 +175,21 @@ def plot_ref_sld_helper(
156175

157176
# Format the axis
158177
ref_plot.set_yscale("log")
159-
ref_plot.set_xscale("log")
178+
if not linear_x:
179+
ref_plot.set_xscale("log")
160180
ref_plot.set_xlabel("$Q_{z} (\u00c5^{-1})$")
161-
ref_plot.set_ylabel("Reflectivity")
162-
ref_plot.legend()
163-
ref_plot.grid()
181+
ref_plot.set_ylabel("Reflectivity")
164182

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

170194
if preserve_zoom:
171195
fig.canvas.toolbar.back()
@@ -181,6 +205,11 @@ def plot_ref_sld(
181205
block: bool = False,
182206
return_fig: bool = False,
183207
bayes: Literal[65, 95, None] = None,
208+
linear_x: bool = False,
209+
q4: bool = False,
210+
show_error_bar: bool = True,
211+
show_grid: bool = True,
212+
show_legend: bool = True,
184213
) -> Union[plt.Figure, None]:
185214
"""Plots the reflectivity and SLD profiles.
186215
@@ -198,7 +227,17 @@ def plot_ref_sld(
198227
Whether to shade Bayesian confidence intervals. Can be `None`
199228
(if no intervals), `65` to show 65% confidence intervals,
200229
and `95` to show 95% confidence intervals.
201-
230+
linear_x : bool, default: False
231+
Controls whether the x-axis on reflectivity plot uses the linear scale
232+
q4 : bool, default: False
233+
Controls whether Q^4 is plotted on the reflectivity plot
234+
show_error_bar : bool, default: True
235+
Controls whether the error bars are shown
236+
show_grid : bool, default: True
237+
Controls whether the grid is shown
238+
show_legend : bool, default: True
239+
Controls whether the lengend is shown
240+
202241
Returns
203242
-------
204243
Figure or None
@@ -253,7 +292,8 @@ def plot_ref_sld(
253292

254293
figure = plt.subplots(1, 2)[0]
255294

256-
plot_ref_sld_helper(data, figure, confidence_intervals=confidence_intervals)
295+
plot_ref_sld_helper(data, figure, confidence_intervals=confidence_intervals, linear_x=linear_x,
296+
q4=q4, show_error_bar=show_error_bar, show_grid=show_grid, show_legend=show_legend)
257297

258298
if return_fig:
259299
return figure

0 commit comments

Comments
 (0)