AxesInDifferentUnits.py 2.52 KB
Newer Older
1
#!/usr/bin/env python3
2
3
"""
In this example we demonstrate how to plot simulation results with
4
axes in different units (nbins, mm, degs and QyQz).
5
"""
6
import bornagain as ba
Wuttke, Joachim's avatar
Wuttke, Joachim committed
7
from bornagain import angstrom, ba_plot as bp, deg, nm
8
from matplotlib import pyplot as plt
9
from matplotlib import rcParams
10
11
12


def get_sample():
13
14
    from bornagain import std_samples
    return std_samples.cylinders()
15
16


17
def get_simulation(sample):
Wuttke, Joachim's avatar
Wuttke, Joachim committed
18
    beam = ba.UnitBeam(1*angstrom, 0.2*deg)
Wuttke, Joachim's avatar
Wuttke, Joachim committed
19
    n = bp.simargs['n']
20
    detector_distance = 2000.0  # in mm
21
    width = 170  # nm
Wuttke, Joachim's avatar
Wuttke, Joachim committed
22
23
    height = width
    detector = ba.RectangularDetector(n, width, n, height)
Wuttke, Joachim's avatar
Wuttke, Joachim committed
24
    detector.setPerpendicularToSampleX(detector_distance, width/2., 0)
25

26
    simulation = ba.ScatteringSimulation(beam, sample, detector)
27
28
29
    return simulation


30
def plot(result):
31
32
33
    """
    Plots simulation results for different detectors.
    """
34
35
    # set plotting parameters
    rcParams['image.aspect'] = 'auto'
36

Wuttke, Joachim's avatar
Wuttke, Joachim committed
37
    plt.figure(figsize=(10, 10))
38
39
40

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

Wuttke, Joachim's avatar
Wuttke, Joachim committed
42
    bp.plot_simres(result,
Wuttke, Joachim's avatar
Wuttke, Joachim committed
43
44
45
46
                   xlabel=r'$x \;({\rm mm})$',
                   ylabel=r'$y \;({\rm mm})$',
                   zlabel=None,
                   with_cb=False)
Wuttke, Joachim's avatar
Wuttke, Joachim committed
47
    plt.title("Default: real-space detector coordinates", loc='left')
48
49

    plt.subplot(2, 2, 2)
Wuttke, Joachim's avatar
Wuttke, Joachim committed
50
    bp.plot_simres(result,
Wuttke, Joachim's avatar
Wuttke, Joachim committed
51
52
53
54
55
                   units=ba.Coords_NBINS,
                   xlabel=r'$X_{\rm bin}$',
                   ylabel=r'$Y_{\rm bin}$',
                   zlabel=None,
                   with_cb=False)
Wuttke, Joachim's avatar
Wuttke, Joachim committed
56
    plt.title("Bin indices", loc='left')
57
58

    plt.subplot(2, 2, 3)
Wuttke, Joachim's avatar
Wuttke, Joachim committed
59
    bp.plot_simres(result,
Wuttke, Joachim's avatar
Wuttke, Joachim committed
60
61
62
63
64
                   units=ba.Coords_DEGREES,
                   xlabel=r'$\varphi_{\rm f} \;(^{\circ})$',
                   ylabel=r'$\alpha_{\rm f} \;(^{\circ})$',
                   zlabel=None,
                   with_cb=False)
Wuttke, Joachim's avatar
Wuttke, Joachim committed
65
    plt.title("Deflection angles", loc='left')
66
67

    plt.subplot(2, 2, 4)
Wuttke, Joachim's avatar
Wuttke, Joachim committed
68
    bp.plot_simres(result,
Wuttke, Joachim's avatar
Wuttke, Joachim committed
69
70
71
72
73
                   units=ba.Coords_QSPACE,
                   xlabel=r'$Q_{y} \;(1/{\rm nm})$',
                   ylabel=r'$Q_{z} \;(1/{\rm nm})$',
                   zlabel=None,
                   with_cb=False)
Wuttke, Joachim's avatar
Wuttke, Joachim committed
74
    plt.title("Q space", loc='left')
75

76
77
78
79
80
81
82
83
    plt.subplots_adjust(
        left=0.07,
        right=0.97,
        top=0.9,
        bottom=0.1,
        hspace=0.35,
        wspace=0.,
    )
84
85
86


if __name__ == '__main__':
Wuttke, Joachim's avatar
Wuttke, Joachim committed
87
    bp.parse_args(sim_n=500)
88
89
90
91

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

92
    plot(result)
Wuttke, Joachim's avatar
Wuttke, Joachim committed
93
    bp.show_or_export()