imshow.cpp 662 B

1234567891011121314151617181920212223242526272829
  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. plt::imshow(zptr, nrows, ncols, colors);
  21. // Show plots
  22. plt::save("imshow.png");
  23. std::cout << "Result saved to 'imshow.png'.\n";
  24. }