Procházet zdrojové kódy

Added basic example for imshow.

This addresses comments on #79.
Trevor Keller před 6 roky
rodič
revize
bc7e4576b2
2 změnil soubory, kde provedl 35 přidání a 3 odebrání
  1. 6 3
      Makefile
  2. 29 0
      examples/imshow.cpp

+ 6 - 3
Makefile

@@ -1,4 +1,4 @@
-examples: minimal basic modern animation nonblock xkcd quiver bar surface fill_inbetween fill update
+examples: minimal basic modern animation nonblock xkcd quiver bar surface fill_inbetween fill update imshow
 
 minimal: examples/minimal.cpp matplotlibcpp.h
 	cd examples && g++ -DWITHOUT_NUMPY minimal.cpp -I/usr/include/python2.7 -lpython2.7 -o minimal -std=c++11
@@ -32,9 +32,12 @@ fill_inbetween: examples/fill_inbetween.cpp matplotlibcpp.h
 
 fill: examples/fill.cpp matplotlibcpp.h
 	cd examples && g++ fill.cpp -I/usr/include/python2.7 -lpython2.7 -o fill -std=c++11
-	
+
 update: examples/update.cpp matplotlibcpp.h
 	cd examples && g++ update.cpp -I/usr/include/python2.7 -lpython2.7 -o update -std=c++11
 
+imshow: examples/imshow.cpp matplotlibcpp.h
+	cd examples && g++ imshow.cpp -I/usr/include/python2.7 -lpython2.7 -o imshow -std=c++11
+
 clean:
-	rm -f examples/{minimal,basic,modern,animation,nonblock,xkcd,quiver,bar,surface,fill_inbetween,fill,update}
+	rm -f examples/{minimal,basic,modern,animation,nonblock,xkcd,quiver,bar,surface,fill_inbetween,fill,update,imshow}

+ 29 - 0
examples/imshow.cpp

@@ -0,0 +1,29 @@
+#define _USE_MATH_DEFINES
+#include <cmath>
+#include <iostream>
+#include "../matplotlibcpp.h"
+
+using namespace std;
+namespace plt = matplotlibcpp;
+
+int main()
+{
+    // Prepare data
+    int ncols = 500, nrows = 300;
+    std::vector<float> z(ncols * nrows);
+    for (int j=0; j<nrows; ++j) {
+        for (int i=0; i<ncols; ++i) {
+            z.at(ncols * j + i) = std::sin(std::hypot(i - ncols/2, j - nrows/2));
+        }
+    }
+
+    const float* zptr = &(z[0]);
+    const int colors = 1;
+
+    plt::title("My matrix");
+    plt::imshow(zptr, nrows, ncols, colors);
+
+    // Show plots
+    plt::save("imshow.png");
+    std::cout << "Result saved to 'imshow.png'.\n";
+}