OLD | NEW |
---|---|
(Empty) | |
1 /* | |
2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. | |
3 * | |
4 * Use of this source code is governed by a BSD-style license | |
5 * that can be found in the LICENSE file in the root of the source | |
6 * tree. An additional intellectual property rights grant can be found | |
7 * in the file PATENTS. All contributing project authors may | |
8 * be found in the AUTHORS file in the root of the source tree. | |
9 */ | |
10 | |
11 #include "webrtc/tools/event_log_visualizer/plot_python.h" | |
12 #include <stdio.h> | |
13 | |
14 // namespace {} // namespace | |
15 | |
16 namespace webrtc { | |
17 namespace plotting { | |
18 | |
19 PythonPlot::PythonPlot() {} | |
20 | |
21 PythonPlot::~PythonPlot() {} | |
22 | |
23 void PythonPlot::draw() { | |
24 // Write python commands to stdout. Intended program usage is | |
25 // ./event_log_visualizer event_log160330.dump | python | |
26 | |
27 // TODO(terelius): Check non-zero number of data series. | |
28 printf("color_count = %zu\n", series.size()); | |
29 printf( | |
30 "hls_colors = [(i*1.0/color_count, 0.25+i*0.5/color_count, 0.8) for i in " | |
31 "range(color_count)]\n"); | |
32 printf("rgb_colors = [colorsys.hls_to_rgb(*hls) for hls in hls_colors]\n"); | |
33 | |
34 for (size_t i = 0; i < series.size(); i++) { | |
35 // List x coordinates | |
36 printf("x%zu = [", i); | |
37 if (series[i].points.size() > 0) | |
38 printf("%G", series[i].points[0].x); | |
39 for (size_t j = 1; j < series[i].points.size(); j++) | |
40 printf(", %G", series[i].points[j].x); | |
41 printf("]\n"); | |
42 | |
43 // List y coordinates | |
44 printf("y%zu = [", i); | |
45 if (series[i].points.size() > 0) | |
46 printf("%G", series[i].points[0].y); | |
47 for (size_t j = 1; j < series[i].points.size(); j++) | |
48 printf(", %G", series[i].points[j].y); | |
49 printf("]\n"); | |
50 | |
51 if (series[i].style == BAR_GRAPH) { | |
52 // There is a plt.bar function that draws bar plots, | |
53 // but it is *way* too slow to be useful. | |
54 printf( | |
55 "plt.vlines(x%zu, map(lambda x: min(x,0), y%zu), map(lambda x: " | |
56 "max(x,0), y%zu), color=rgb_colors[%zu], " | |
57 "label=\'%s\')\n", | |
58 i, i, i, i, series[i].label.c_str()); | |
59 } else if (series[i].style == LINE_GRAPH) { | |
60 printf("plt.plot(x%zu, y%zu, color=rgb_colors[%zu], label=\'%s\')\n", i, | |
61 i, i, series[i].label.c_str()); | |
62 } else { | |
63 printf("raise Exception(\"Unknown graph type\")\n"); | |
64 } | |
65 } | |
66 | |
67 printf("plt.xlim(%f, %f)\n", xaxis_min, xaxis_max); | |
68 printf("plt.ylim(%f, %f)\n", yaxis_min, yaxis_max); | |
69 printf("plt.xlabel(\'%s\')\n", xaxis_label.c_str()); | |
70 printf("plt.ylabel(\'%s\')\n", yaxis_label.c_str()); | |
71 printf("plt.title(\'%s\')\n", title.c_str()); | |
72 printf("plt.legend(loc=\'best\', fontsize=\'small\')\n"); | |
73 } | |
74 | |
75 PythonPlotCollection::PythonPlotCollection() {} | |
76 | |
77 PythonPlotCollection::~PythonPlotCollection() {} | |
78 | |
79 void PythonPlotCollection::draw() { | |
80 printf("import matplotlib.pyplot as plt\n"); | |
81 printf("import colorsys\n"); | |
82 for (size_t i = 0; i < plots.size(); i++) { | |
83 printf("plt.figure(%zu)\n", i); | |
84 plots[i]->draw(); | |
85 } | |
86 printf("plt.show()\n"); | |
87 } | |
88 | |
89 void PythonPlotCollection::append_plot() { | |
90 plots.push_back(std::unique_ptr<Plot>(static_cast<Plot*>(new PythonPlot()))); | |
aleloi
2016/06/08 11:44:20
The explicit cast is not needed here. PythonPlot*
terelius
2016/06/14 13:18:49
Rewritten in accordance with other comments.
| |
91 } | |
92 | |
93 } // namespace plotting | |
94 } // namespace webrtc | |
OLD | NEW |