1
0

README 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. matplotlib-cpp
  2. ==============
  3. This is matplotlib-cpp, probably the simplest C++ plotting library.
  4. It is built to resemble the plotting API used by Matlab and matplotlib.
  5. Usage
  6. -----
  7. Complete minimal example:
  8. #include "matplotlibcpp.h"
  9. namespace plt = matplotlibcpp;
  10. int main() {
  11. std::vector<double> v {1,2,3,4};
  12. plt::plot(v);
  13. plt::show();
  14. }
  15. // g++ minimal.cpp -std=c++11 -lpython2.7
  16. Result: ![Minimal example](./examples/minimal.png)
  17. A more comprehensive example:
  18. #include "matplotlibcpp.h"
  19. #include <cmath>
  20. namespace plt = matplotlibcpp;
  21. int main()
  22. {
  23. // Prepare data.
  24. int n = 5000;
  25. std::vector<double> x(n), y(n), z(n), w(n,2);
  26. for(int i=0; i<n; ++i) {
  27. x.at(i) = i*i;
  28. y.at(i) = sin(2*M_PI*i/360.0);
  29. z.at(i) = log(i);
  30. }
  31. // Plot line from given x and y data. Color is selected automatically.
  32. plt::plot(x, y);
  33. // Plot a red dashed line from given x and y data.
  34. plt::plot(x, w,"r--");
  35. // Plot a line whose name will show up as "log(x)" in the legend.
  36. plt::named_plot("log(x)", x, z);
  37. // Set x-axis to interval [0,1000000]
  38. plt::xlim(0, 1000*1000);
  39. // Enable legend.
  40. plt::legend();
  41. // Show plot
  42. plt::show();
  43. }
  44. Result: ![Basic example](./examples/basic.png)
  45. Installation
  46. ------------
  47. matplotlib-cpp works by wrapping the popular python plotting library matplotlib. (matplotlib.org)
  48. This means you have to have a working python installation, including development headers.
  49. On Ubuntu:
  50. sudo aptitude install python-matplotlib python2.7-dev
  51. The C++-part of the library consists of the single header file matplotlibcpp.h which can be placed
  52. anywhere.
  53. Since a python interpreter is opened internally, it is necessary to link against libpython2.7 in order to use
  54. matplotlib-cpp.
  55. (There should be no problems using python3 instead of python2.7, if desired)
  56. Todo/Issues/Wishlist
  57. --------------------
  58. * It would be nice to have a more object-oriented design with a Plot class which would allow
  59. multiple independent plots per program.
  60. * Right now, only a small subset of matplotlibs functionality is exposed. Stuff like xlabel()/ylabel() etc. should
  61. be easy to add.