Skip to content

Commit f4ad842

Browse files
cdbrkfxrptlava
authored andcommitted
Implements bar slightly more closely to matplotlib
1 parent 7fa134e commit f4ad842

File tree

1 file changed

+43
-22
lines changed

1 file changed

+43
-22
lines changed

matplotlibcpp.h

Lines changed: 43 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -675,35 +675,56 @@ bool scatter(const std::vector<NumericX>& x,
675675
return res;
676676
}
677677

678-
template< typename Numeric>
679-
bool bar(const std::vector<Numeric>& y, std::string ec = "black", std::string ls = "-", double lw = 1.0,
680-
__attribute__((unused)) const std::map<std::string, std::string>& keywords = {})
681-
{
682-
PyObject* yarray = get_array(y);
683-
684-
std::vector<int> x;
685-
for (std::size_t i = 0; i < y.size(); i++)
686-
x.push_back(i);
678+
template <typename Numeric>
679+
bool bar(const std::vector<Numeric> & x,
680+
const std::vector<Numeric> & y,
681+
std::string ec = "black",
682+
std::string ls = "-",
683+
double lw = 1.0,
684+
const std::map<std::string, std::string> & keywords = {}) {
685+
PyObject * xarray = get_array(x);
686+
PyObject * yarray = get_array(y);
687+
688+
PyObject * kwargs = PyDict_New();
689+
690+
PyDict_SetItemString(kwargs, "ec", PyString_FromString(ec.c_str()));
691+
PyDict_SetItemString(kwargs, "ls", PyString_FromString(ls.c_str()));
692+
PyDict_SetItemString(kwargs, "lw", PyFloat_FromDouble(lw));
693+
694+
for (std::map<std::string, std::string>::const_iterator it =
695+
keywords.begin();
696+
it != keywords.end();
697+
++it) {
698+
PyDict_SetItemString(
699+
kwargs, it->first.c_str(), PyUnicode_FromString(it->second.c_str()));
700+
}
687701

688-
PyObject* xarray = get_array(x);
702+
PyObject * plot_args = PyTuple_New(2);
703+
PyTuple_SetItem(plot_args, 0, xarray);
704+
PyTuple_SetItem(plot_args, 1, yarray);
689705

690-
PyObject* kwargs = PyDict_New();
706+
PyObject * res = PyObject_Call(
707+
detail::_interpreter::get().s_python_function_bar, plot_args, kwargs);
691708

692-
PyDict_SetItemString(kwargs, "ec", PyString_FromString(ec.c_str()));
693-
PyDict_SetItemString(kwargs, "ls", PyString_FromString(ls.c_str()));
694-
PyDict_SetItemString(kwargs, "lw", PyFloat_FromDouble(lw));
709+
Py_DECREF(plot_args);
710+
Py_DECREF(kwargs);
711+
if (res) Py_DECREF(res);
695712

696-
PyObject* plot_args = PyTuple_New(2);
697-
PyTuple_SetItem(plot_args, 0, xarray);
698-
PyTuple_SetItem(plot_args, 1, yarray);
713+
return res;
714+
}
699715

700-
PyObject* res = PyObject_Call(detail::_interpreter::get().s_python_function_bar, plot_args, kwargs);
716+
template <typename Numeric>
717+
bool bar(const std::vector<Numeric> & y,
718+
std::string ec = "black",
719+
std::string ls = "-",
720+
double lw = 1.0,
721+
const std::map<std::string, std::string> & keywords = {}) {
722+
using T = typename std::remove_reference<decltype(y)>::type::value_type;
701723

702-
Py_DECREF(plot_args);
703-
Py_DECREF(kwargs);
704-
if(res) Py_DECREF(res);
724+
std::vector<T> x;
725+
for (std::size_t i = 0; i < y.size(); i++) { x.push_back(i); }
705726

706-
return res;
727+
return bar(x, y, ec, ls, lw, keywords);
707728
}
708729

709730
inline bool subplots_adjust(const std::map<std::string, double>& keywords = {})

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy