浏览代码

Implements bar slightly more closely to matplotlib

Florian Eich 5 年之前
父节点
当前提交
f4ad842e70
共有 1 个文件被更改,包括 43 次插入22 次删除
  1. 43 22
      matplotlibcpp.h

+ 43 - 22
matplotlibcpp.h

@@ -675,35 +675,56 @@ 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,
-         __attribute__((unused)) const std::map<std::string, std::string>& keywords = {})
-{
-    PyObject* yarray = get_array(y);
-
-    std::vector<int> x;
-    for (std::size_t i = 0; i < y.size(); i++)
-        x.push_back(i);
+template <typename Numeric>
+bool bar(const std::vector<Numeric> &               x,
+         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 * xarray = get_array(x);
+  PyObject * yarray = get_array(y);
+
+  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));
+
+  for (std::map<std::string, std::string>::const_iterator it =
+         keywords.begin();
+       it != keywords.end();
+       ++it) {
+    PyDict_SetItemString(
+      kwargs, it->first.c_str(), PyUnicode_FromString(it->second.c_str()));
+  }
 
-    PyObject* xarray = get_array(x);
+  PyObject * plot_args = PyTuple_New(2);
+  PyTuple_SetItem(plot_args, 0, xarray);
+  PyTuple_SetItem(plot_args, 1, yarray);
 
-    PyObject* kwargs = PyDict_New();
+  PyObject * res = PyObject_Call(
+    detail::_interpreter::get().s_python_function_bar, plot_args, kwargs);
 
-    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));
+  Py_DECREF(plot_args);
+  Py_DECREF(kwargs);
+  if (res) Py_DECREF(res);
 
-    PyObject* plot_args = PyTuple_New(2);
-    PyTuple_SetItem(plot_args, 0, xarray);
-    PyTuple_SetItem(plot_args, 1, yarray);
+  return res;
+}
 
-    PyObject* res = PyObject_Call(detail::_interpreter::get().s_python_function_bar, plot_args, kwargs);
+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 = {}) {
+  using T = typename std::remove_reference<decltype(y)>::type::value_type;
 
-    Py_DECREF(plot_args);
-    Py_DECREF(kwargs);
-    if(res) Py_DECREF(res);
+  std::vector<T> x;
+  for (std::size_t i = 0; i < y.size(); i++) { x.push_back(i); }
 
-    return res;
+  return bar(x, y, ec, ls, lw, keywords);
 }
 
 inline bool subplots_adjust(const std::map<std::string, double>& keywords = {})