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 |
| 13 #include <stdio.h> |
| 14 |
| 15 namespace webrtc { |
| 16 namespace plotting { |
| 17 |
| 18 PythonPlot::PythonPlot() {} |
| 19 |
| 20 PythonPlot::~PythonPlot() {} |
| 21 |
| 22 void PythonPlot::draw() { |
| 23 // Write python commands to stdout. Intended program usage is |
| 24 // ./event_log_visualizer event_log160330.dump | python |
| 25 |
| 26 if (!series.empty()) { |
| 27 printf("color_count = %zu\n", series.size()); |
| 28 printf( |
| 29 "hls_colors = [(i*1.0/color_count, 0.25+i*0.5/color_count, 0.8) for i " |
| 30 "in range(color_count)]\n"); |
| 31 printf("rgb_colors = [colorsys.hls_to_rgb(*hls) for hls in hls_colors]\n"); |
| 32 |
| 33 for (size_t i = 0; i < series.size(); i++) { |
| 34 // List x coordinates |
| 35 printf("x%zu = [", i); |
| 36 if (series[i].points.size() > 0) |
| 37 printf("%G", series[i].points[0].x); |
| 38 for (size_t j = 1; j < series[i].points.size(); j++) |
| 39 printf(", %G", series[i].points[j].x); |
| 40 printf("]\n"); |
| 41 |
| 42 // List y coordinates |
| 43 printf("y%zu = [", i); |
| 44 if (series[i].points.size() > 0) |
| 45 printf("%G", series[i].points[0].y); |
| 46 for (size_t j = 1; j < series[i].points.size(); j++) |
| 47 printf(", %G", series[i].points[j].y); |
| 48 printf("]\n"); |
| 49 |
| 50 if (series[i].style == BAR_GRAPH) { |
| 51 // There is a plt.bar function that draws bar plots, |
| 52 // but it is *way* too slow to be useful. |
| 53 printf( |
| 54 "plt.vlines(x%zu, map(lambda t: min(t,0), y%zu), map(lambda t: " |
| 55 "max(t,0), y%zu), color=rgb_colors[%zu], " |
| 56 "label=\'%s\')\n", |
| 57 i, i, i, i, series[i].label.c_str()); |
| 58 } else if (series[i].style == LINE_GRAPH) { |
| 59 printf("plt.plot(x%zu, y%zu, color=rgb_colors[%zu], label=\'%s\')\n", i, |
| 60 i, i, series[i].label.c_str()); |
| 61 } else { |
| 62 printf("raise Exception(\"Unknown graph type\")\n"); |
| 63 } |
| 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 if (!series.empty()) { |
| 73 printf("plt.legend(loc=\'best\', fontsize=\'small\')\n"); |
| 74 } |
| 75 } |
| 76 |
| 77 PythonPlotCollection::PythonPlotCollection() {} |
| 78 |
| 79 PythonPlotCollection::~PythonPlotCollection() {} |
| 80 |
| 81 void PythonPlotCollection::draw() { |
| 82 printf("import matplotlib.pyplot as plt\n"); |
| 83 printf("import colorsys\n"); |
| 84 for (size_t i = 0; i < plots.size(); i++) { |
| 85 printf("plt.figure(%zu)\n", i); |
| 86 plots[i]->draw(); |
| 87 } |
| 88 printf("plt.show()\n"); |
| 89 } |
| 90 |
| 91 Plot* PythonPlotCollection::append_new_plot() { |
| 92 Plot* plot = new PythonPlot(); |
| 93 plots.push_back(std::unique_ptr<Plot>(plot)); |
| 94 return plot; |
| 95 } |
| 96 |
| 97 } // namespace plotting |
| 98 } // namespace webrtc |
OLD | NEW |