]>
Commit | Line | Data |
---|---|---|
92f5a8d4 TL |
1 | // Copyright 2015-2018 Hans Dembinski |
2 | // | |
3 | // Distributed under the Boost Software License, Version 1.0. | |
4 | // (See accompanying file LICENSE_1_0.txt | |
5 | // or copy at http://www.boost.org/LICENSE_1_0.txt) | |
6 | ||
7 | //[ guide_histogram_streaming | |
8 | ||
9 | #include <boost/histogram.hpp> | |
10 | #include <boost/histogram/ostream.hpp> | |
11 | #include <cassert> | |
12 | #include <iostream> | |
13 | #include <sstream> | |
14 | #include <string> | |
15 | ||
16 | int main() { | |
17 | using namespace boost::histogram; | |
18 | ||
19 | std::ostringstream os; | |
20 | ||
1e59de90 TL |
21 | // width of histogram can be set like this; if it is not set, the library attempts to |
22 | // determine the terminal width and choses the histogram width accordingly | |
23 | os.width(78); | |
24 | ||
92f5a8d4 TL |
25 | auto h1 = make_histogram(axis::regular<>(5, -1.0, 1.0, "axis 1")); |
26 | h1.at(0) = 2; | |
27 | h1.at(1) = 4; | |
28 | h1.at(2) = 3; | |
29 | h1.at(4) = 1; | |
30 | ||
31 | // 1D histograms are rendered as an ASCII drawing | |
32 | os << h1; | |
33 | ||
34 | auto h2 = make_histogram(axis::regular<>(2, -1.0, 1.0, "axis 1"), | |
35 | axis::category<std::string>({"red", "blue"}, "axis 2")); | |
36 | ||
37 | // higher dimensional histograms just have their cell counts listed | |
38 | os << h2; | |
39 | ||
40 | std::cout << os.str() << std::endl; | |
41 | ||
42 | assert( | |
43 | os.str() == | |
44 | "histogram(regular(5, -1, 1, metadata=\"axis 1\", options=underflow | overflow))\n" | |
1e59de90 TL |
45 | " ┌─────────────────────────────────────────────────────────────┐\n" |
46 | "[-inf, -1) 0 │ │\n" | |
47 | "[ -1, -0.6) 2 │██████████████████████████████ │\n" | |
48 | "[-0.6, -0.2) 4 │████████████████████████████████████████████████████████████ │\n" | |
49 | "[-0.2, 0.2) 3 │█████████████████████████████████████████████ │\n" | |
50 | "[ 0.2, 0.6) 0 │ │\n" | |
51 | "[ 0.6, 1) 1 │███████████████ │\n" | |
52 | "[ 1, inf) 0 │ │\n" | |
53 | " └─────────────────────────────────────────────────────────────┘\n" | |
92f5a8d4 TL |
54 | "histogram(\n" |
55 | " regular(2, -1, 1, metadata=\"axis 1\", options=underflow | overflow)\n" | |
56 | " category(\"red\", \"blue\", metadata=\"axis 2\", options=overflow)\n" | |
57 | " (-1 0): 0 ( 0 0): 0 ( 1 0): 0 ( 2 0): 0 (-1 1): 0 ( 0 1): 0\n" | |
58 | " ( 1 1): 0 ( 2 1): 0 (-1 2): 0 ( 0 2): 0 ( 1 2): 0 ( 2 2): 0\n" | |
59 | ")"); | |
60 | } | |
61 | ||
62 | //] |