Plotting with axes in different units

In this example we demonstrate how to plot intensity data with detector axes expressed in different units. It serves as a supporting example to the Accessing simulation results tutorial.

  • The standard Cylinders in DWBA) sample is used to setup the simulation.
  • When the simulation is completed, the Simulation::result() method is used to get a SimulationResult object.
  • Depending on an additional parameter IDetector2D.NBINS, IDetector2D.DEGREES, IDetector2D.QYQZ, it will be plotted with axes defined either in millimeters (default units of RectangularDetector), detector bins, degrees or in $Q$-space.
  • Please note, that the given parameter only affects min/max values of histogram axes (there is no rebinning involved).

Intensity images

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/env python3
"""
In this example we demonstrate how to plot simulation results with
axes in different units (nbins, mm, degs and QyQz).
"""
import bornagain as ba
from bornagain import angstrom, ba_plot as bp, deg, nm
from matplotlib import pyplot as plt
from matplotlib import rcParams


def get_sample():
    from bornagain import std_samples
    return std_samples.cylinders()


def get_simulation(sample):
    beam = ba.Beam(1, 1*angstrom, ba.Direction(0.2*deg, 0))
    n = bp.simargs['n']
    detector_distance = 2000.0  # in mm
    width = 170  # nm
    height = width
    detector = ba.RectangularDetector(n, width, n, height)
    detector.setPerpendicularToSampleX(detector_distance, width/2., 0)

    simulation = ba.ScatteringSimulation(beam, sample, detector)
    return simulation


def plot(result):
    """
    Plots simulation results for different detectors.
    """
    # set plotting parameters
    rcParams['image.aspect'] = 'auto'

    plt.figure(figsize=(10, 10))

    plt.subplot(2, 2, 1)
    # default units for rectangular detector are millimeters

    bp.plot_simres(result,
                     xlabel=r'$x \;({\rm mm})$',
                     ylabel=r'$y \;({\rm mm})$',
                     zlabel=None,
                     with_cb=False)
    plt.title("Default: real-space detector coordinates", loc='left')

    plt.subplot(2, 2, 2)
    bp.plot_simres(result,
                     units=ba.Coords_NBINS,
                     xlabel=r'$X_{\rm bin}$',
                     ylabel=r'$Y_{\rm bin}$',
                     zlabel=None,
                     with_cb=False)
    plt.title("Bin indices", loc='left')

    plt.subplot(2, 2, 3)
    bp.plot_simres(result,
                     units=ba.Coords_DEGREES,
                     xlabel=r'$\varphi_{\rm f} \;(^{\circ})$',
                     ylabel=r'$\alpha_{\rm f} \;(^{\circ})$',
                     zlabel=None,
                     with_cb=False)
    plt.title("Deflection angles", loc='left')

    plt.subplot(2, 2, 4)
    bp.plot_simres(result,
                     units=ba.Coords_QSPACE,
                     xlabel=r'$Q_{y} \;(1/{\rm nm})$',
                     ylabel=r'$Q_{z} \;(1/{\rm nm})$',
                     zlabel=None,
                     with_cb=False)
    plt.title("Q space", loc='left')

    plt.subplots_adjust(
        left=0.07,
        right=0.97,
        top=0.9,
        bottom=0.1,
        hspace=0.35,
        wspace=0.,
    )


if __name__ == '__main__':
    bp.parse_args(sim_n=500)

    simulation = get_simulation(get_sample())
    result = simulation.simulate()

    plot(result)
    bp.show_or_export()
Examples/scatter2d/AxesInDifferentUnits.py