Prechádzať zdrojové kódy

Add bar plot and subplots_adjust

weijun.liu 6 rokov pred
rodič
commit
94f6c01021
5 zmenil súbory, kde vykonal 86 pridanie a 3 odobranie
  1. 5 2
      Makefile
  2. 1 0
      contrib/CMakeLists.txt
  3. 18 0
      examples/bar.cpp
  4. BIN
      examples/bar.png
  5. 62 1
      matplotlibcpp.h

+ 5 - 2
Makefile

@@ -1,4 +1,4 @@
-examples: minimal basic modern animation nonblock xkcd quiver
+examples: minimal basic modern animation nonblock xkcd quiver bar
 
 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
@@ -21,5 +21,8 @@ quiver: examples/quiver.cpp matplotlibcpp.h
 xkcd: examples/xkcd.cpp matplotlibcpp.h
 	cd examples && g++ xkcd.cpp -I/usr/include/python2.7 -lpython2.7 -o xkcd -std=c++11
 
+bar: examples/bar.cpp matplotlibcpp.h
+	cd examples && g++ bar.cpp -I/usr/include/python2.7 -lpython2.7 -o bar -std=c++11
+
 clean:
-	rm -f examples/{minimal,basic,modern,animation,nonblock,xkcd,quiver}
+	rm -f examples/{minimal,basic,modern,animation,nonblock,xkcd,quiver,bar}

+ 1 - 0
contrib/CMakeLists.txt

@@ -23,3 +23,4 @@ add_executable(modern ${CMAKE_CURRENT_SOURCE_DIR}/../examples/modern.cpp)
 add_executable(animation ${CMAKE_CURRENT_SOURCE_DIR}/../examples/animation.cpp)
 add_executable(nonblock ${CMAKE_CURRENT_SOURCE_DIR}/../examples/nonblock.cpp)
 add_executable(xkcd ${CMAKE_CURRENT_SOURCE_DIR}/../examples/xkcd.cpp)
+add_executable(bar ${CMAKE_CURRENT_SOURCE_DIR}/../examples/bar.cpp)

+ 18 - 0
examples/bar.cpp

@@ -0,0 +1,18 @@
+#define _USE_MATH_DEFINES
+
+#include <iostream>
+#include <string>
+#include "../matplotlibcpp.h"
+namespace plt = matplotlibcpp;
+
+int main(int argc, char **argv) {
+    std::vector<int> test_data;
+    for (int i = 0; i < 20; i++) {
+        test_data.push_back(i);
+    }
+
+    plt::bar(test_data);
+    plt::show();
+
+    return (0);
+}

BIN
examples/bar.png


+ 62 - 1
matplotlibcpp.h

@@ -67,6 +67,9 @@ struct _interpreter {
     PyObject *s_python_function_xkcd;
     PyObject *s_python_function_text;
     PyObject *s_python_function_suptitle;
+    PyObject *s_python_function_bar;
+    PyObject *s_python_function_subplots_adjust;
+
 
     /* For now, _interpreter is implemented as a singleton since its currently not possible to have
        multiple independent embedded python interpreters without patching the python source code
@@ -175,6 +178,8 @@ private:
         s_python_function_xkcd = PyObject_GetAttrString(pymod, "xkcd");
         s_python_function_text = PyObject_GetAttrString(pymod, "text");
         s_python_function_suptitle = PyObject_GetAttrString(pymod, "suptitle");
+        s_python_function_bar = PyObject_GetAttrString(pymod,"bar");
+        s_python_function_subplots_adjust = PyObject_GetAttrString(pymod,"subplots_adjust");
 
         if(    !s_python_function_show
             || !s_python_function_close
@@ -209,6 +214,8 @@ private:
             || !s_python_function_xkcd
             || !s_python_function_text
             || !s_python_function_suptitle
+            || !s_python_function_bar
+            || !s_python_function_subplots_adjust
         ) { throw std::runtime_error("Couldn't find required function!"); }
 
         if (   !PyFunction_Check(s_python_function_show)
@@ -243,6 +250,8 @@ private:
             || !PyFunction_Check(s_python_function_xkcd)
             || !PyFunction_Check(s_python_function_text)
             || !PyFunction_Check(s_python_function_suptitle)
+            || !PyFunction_Check(s_python_function_bar)
+            || !PyFunction_Check(s_python_function_subplots_adjust)
         ) { throw std::runtime_error("Python object is unexpectedly not a PyFunction."); }
 
         s_python_empty_tuple = PyTuple_New(0);
@@ -440,7 +449,6 @@ bool hist(const std::vector<Numeric>& y, long bins=10,std::string color="b", dou
     PyDict_SetItemString(kwargs, "color", PyString_FromString(color.c_str()));
     PyDict_SetItemString(kwargs, "alpha", PyFloat_FromDouble(alpha));
 
-
     PyObject* plot_args = PyTuple_New(1);
 
     PyTuple_SetItem(plot_args, 0, yarray);
@@ -482,6 +490,59 @@ bool scatter(const std::vector<NumericX>& x,
     return res;
 }
 
+template< typename Numeric>
+bool bar(const std::vector<Numeric>& y, std::string ec = "black", std::string ls = "-", double lw = 1.0,
+         const std::map<std::string, std::string>& keywords = {})
+{
+    PyObject* yarray = get_array(y);
+
+    std::vector<int> x;
+    for (int i = 0; i < y.size(); i++)
+        x.push_back(i);
+
+    PyObject* xarray = get_array(x);
+
+    PyObject* kwargs = PyDict_New();
+
+    PyDict_SetItemString(kwargs, "ec", PyString_FromString(ec.c_str()));
+    PyDict_SetItemString(kwargs, "ls", PyString_FromString(ls.c_str()));
+    PyDict_SetItemString(kwargs, "lw", PyFloat_FromDouble(lw));
+
+    PyObject* plot_args = PyTuple_New(2);
+    PyTuple_SetItem(plot_args, 0, xarray);
+    PyTuple_SetItem(plot_args, 1, yarray);
+
+    PyObject* res = PyObject_Call(detail::_interpreter::get().s_python_function_bar, plot_args, kwargs);
+
+    Py_DECREF(plot_args);
+    Py_DECREF(kwargs);
+    if(res) Py_DECREF(res);
+
+    return res;
+}
+
+inline bool subplots_adjust(const std::map<std::string, double>& keywords = {})
+{
+
+    PyObject* kwargs = PyDict_New();
+    for (std::map<std::string, double>::const_iterator it =
+            keywords.begin(); it != keywords.end(); ++it) {
+        PyDict_SetItemString(kwargs, it->first.c_str(),
+                             PyFloat_FromDouble(it->second));
+    }
+
+
+    PyObject* plot_args = PyTuple_New(0);
+
+    PyObject* res = PyObject_Call(detail::_interpreter::get().s_python_function_subplots_adjust, plot_args, kwargs);
+
+    Py_DECREF(plot_args);
+    Py_DECREF(kwargs);
+    if(res) Py_DECREF(res);
+
+    return res;
+}
+
 template< typename Numeric>
 bool named_hist(std::string label,const std::vector<Numeric>& y, long bins=10, std::string color="b", double alpha=1.0)
 {