1
0

colorbar.cpp 689 B

1234567891011121314151617181920212223242526272829303132
  1. #define _USE_MATH_DEFINES
  2. #include <cmath>
  3. #include <iostream>
  4. #include "../matplotlibcpp.h"
  5. using namespace std;
  6. namespace plt = matplotlibcpp;
  7. int main()
  8. {
  9. // Prepare data
  10. int ncols = 500, nrows = 300;
  11. std::vector<float> z(ncols * nrows);
  12. for (int j=0; j<nrows; ++j) {
  13. for (int i=0; i<ncols; ++i) {
  14. z.at(ncols * j + i) = std::sin(std::hypot(i - ncols/2, j - nrows/2));
  15. }
  16. }
  17. const float* zptr = &(z[0]);
  18. const int colors = 1;
  19. plt::title("My matrix");
  20. PyObject* mat;
  21. plt::imshow(zptr, nrows, ncols, colors, {}, &mat);
  22. plt::colorbar(mat);
  23. // Show plots
  24. plt::show();
  25. plt::close();
  26. Py_DECREF(mat);
  27. }