summaryrefslogtreecommitdiff
path: root/examples/exampleBaseline.py
blob: edd0fc36bde6d944646ed3b98190b256c26d2dae (plain)
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/usr/bin/env python
# coding: utf-8
# /*##########################################################################
#
# Copyright (c) 2017-2018 European Synchrotron Radiation Facility
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
# ###########################################################################*/
"""This example illustrates some usage possible with the baseline parameter
"""

__authors__ = ["H. Payno"]
__license__ = "MIT"
__date__ = "12/09/2019"


from silx.gui import qt
from silx.gui.plot import Plot1D
import numpy
import sys
import argparse


def stacked_histogran(plot, edges, histograms, colors, legend):
    # check that we have the same number of histogram, color and baseline
    current_baseline = numpy.zeros_like(edges)

    for histogram, color, layer_index in zip(histograms, colors, range(len(colors))):
        stacked_histo = histogram + current_baseline
        plot.addHistogram(histogram=stacked_histo,
                          edges=edges,
                          legend='_'.join((legend, str(layer_index))),
                          color=color,
                          baseline=current_baseline,
                          z=len(histograms)-layer_index,
                          fill=True)
        current_baseline = stacked_histo


def get_plot_std(backend):
    x = numpy.arange(0, 10, step=0.1)
    my_sin = numpy.sin(x)
    y = numpy.arange(-4, 6, step=0.1) + my_sin
    mean = numpy.arange(-5, 5, step=0.1) + my_sin
    baseline = numpy.arange(-6, 4, step=0.1) + my_sin
    edges = x[y >= 3.0]
    histo = mean[y >= 3.0] - 1.8

    plot = Plot1D(backend=backend)
    plot.addCurve(x=x, y=y, baseline=baseline, color='grey',
                  legend='std-curve', fill=True)
    plot.addCurve(x=x, y=mean, color='red', legend='mean')
    plot.addHistogram(histogram=histo, edges=edges, color='red',
                      legend='mean2', fill=True)
    return plot


def get_plot_stacked_histogram(backend):
    plot = Plot1D(backend=backend)
    # first histogram
    edges = numpy.arange(-6, 6, step=0.5)
    histo_1 = numpy.random.random(len(edges))
    histo_2 = numpy.random.random(len(edges))
    histo_3 = numpy.random.random(len(edges))
    histo_4 = numpy.random.random(len(edges))
    stacked_histogran(plot=plot,
                      edges=edges,
                      histograms=(histo_1, histo_2, histo_3, histo_4),
                      colors=('blue', 'green', 'red', 'yellow'),
                      legend='first_stacked_histo')

    # second histogram
    edges = numpy.arange(10, 25, step=1.0)
    histo_1 = -numpy.random.random(len(edges))
    histo_2 = -numpy.random.random(len(edges))
    stacked_histogran(plot=plot, histograms=(histo_1, histo_2),
                      edges=edges,
                      colors=('gray', 'black'),
                      legend='second_stacked_histo')

    # last histogram
    edges = [30, 40]
    histograms = [
        [0.2, 0.3],
        [0.0, 1.0],
        [0.1, 0.4],
        [0.2, 0.0],
        [0.6, 0.4],
    ]
    stacked_histogran(plot=plot,
                      histograms=histograms,
                      edges=edges,
                      colors=('blue', 'green', 'red', 'yellow', 'cyan'),
                      legend='third_stacked_histo')

    return plot


def get_plot_mean_baseline(backend):
    plot = Plot1D(backend=backend)
    x = numpy.arange(0, 10, step=0.1)
    y = numpy.sin(x)
    plot.addCurve(x=x, y=y, baseline=0, fill=True)
    plot.setYAxisLogarithmic(True)
    return plot


def get_plot_log(backend):
    plot = Plot1D(backend=backend)
    x = numpy.arange(0, 10, step=0.01)
    y = numpy.exp2(x)
    baseline = numpy.exp(x)
    plot.addCurve(x=x, y=y, baseline=baseline, fill=True)
    plot.setYAxisLogarithmic(True)
    return plot


def main(argv):
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument(
        '--backend',
        dest="backend",
        action="store",
        default=None,
        help='Set plot backend. Should be "matplotlib" (default) or "opengl"')

    options = parser.parse_args(argv[1:])
    assert options.backend in (None, 'matplotlib', 'opengl')
    qapp = qt.QApplication([])

    plot_std = get_plot_std(backend=options.backend)
    plot_std.show()

    plot_mean = get_plot_mean_baseline(backend=options.backend)
    plot_mean.show()

    plot_stacked_histo = get_plot_stacked_histogram(backend=options.backend)
    plot_stacked_histo.show()

    plot_log = get_plot_log(backend=options.backend)
    plot_log.show()

    qapp.exec_()


if __name__ == '__main__':
    main(sys.argv)