Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 374dcd0

Browse files
JBPenningtonlava
authored andcommitted
Added arrow, cla, margins, contour
1 parent 1079121 commit 374dcd0

File tree

1 file changed

+116
-0
lines changed

1 file changed

+116
-0
lines changed

matplotlibcpp.h

+116
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ namespace detail {
4545
static std::string s_backend;
4646

4747
struct _interpreter {
48+
PyObject* s_python_function_arrow;
4849
PyObject *s_python_function_show;
4950
PyObject *s_python_function_close;
5051
PyObject *s_python_function_draw;
@@ -54,6 +55,7 @@ struct _interpreter {
5455
PyObject *s_python_function_fignum_exists;
5556
PyObject *s_python_function_plot;
5657
PyObject *s_python_function_quiver;
58+
PyObject* s_python_function_contour;
5759
PyObject *s_python_function_semilogx;
5860
PyObject *s_python_function_semilogy;
5961
PyObject *s_python_function_loglog;
@@ -79,8 +81,10 @@ struct _interpreter {
7981
PyObject *s_python_function_gca;
8082
PyObject *s_python_function_xticks;
8183
PyObject *s_python_function_yticks;
84+
PyObject* s_python_function_margins;
8285
PyObject *s_python_function_tick_params;
8386
PyObject *s_python_function_grid;
87+
PyObject* s_python_function_cla;
8488
PyObject *s_python_function_clf;
8589
PyObject *s_python_function_errorbar;
8690
PyObject *s_python_function_annotate;
@@ -186,6 +190,7 @@ struct _interpreter {
186190
Py_DECREF(pylabname);
187191
if (!pylabmod) { throw std::runtime_error("Error loading module pylab!"); }
188192

193+
s_python_function_arrow = safe_import(pymod, "arrow");
189194
s_python_function_show = safe_import(pymod, "show");
190195
s_python_function_close = safe_import(pymod, "close");
191196
s_python_function_draw = safe_import(pymod, "draw");
@@ -194,6 +199,7 @@ struct _interpreter {
194199
s_python_function_fignum_exists = safe_import(pymod, "fignum_exists");
195200
s_python_function_plot = safe_import(pymod, "plot");
196201
s_python_function_quiver = safe_import(pymod, "quiver");
202+
s_python_function_contour = safe_import(pymod, "contour");
197203
s_python_function_semilogx = safe_import(pymod, "semilogx");
198204
s_python_function_semilogy = safe_import(pymod, "semilogy");
199205
s_python_function_loglog = safe_import(pymod, "loglog");
@@ -215,13 +221,15 @@ struct _interpreter {
215221
s_python_function_gca = safe_import(pymod, "gca");
216222
s_python_function_xticks = safe_import(pymod, "xticks");
217223
s_python_function_yticks = safe_import(pymod, "yticks");
224+
s_python_function_margins = safe_import(pymod, "margins");
218225
s_python_function_tick_params = safe_import(pymod, "tick_params");
219226
s_python_function_grid = safe_import(pymod, "grid");
220227
s_python_function_xlim = safe_import(pymod, "xlim");
221228
s_python_function_ion = safe_import(pymod, "ion");
222229
s_python_function_ginput = safe_import(pymod, "ginput");
223230
s_python_function_save = safe_import(pylabmod, "savefig");
224231
s_python_function_annotate = safe_import(pymod,"annotate");
232+
s_python_function_cla = safe_import(pymod, "cla");
225233
s_python_function_clf = safe_import(pymod, "clf");
226234
s_python_function_errorbar = safe_import(pymod, "errorbar");
227235
s_python_function_tight_layout = safe_import(pymod, "tight_layout");
@@ -709,6 +717,37 @@ bool fill_between(const std::vector<Numeric>& x, const std::vector<Numeric>& y1,
709717
return res;
710718
}
711719

720+
template <typename Numeric>
721+
bool arrow(Numeric x, Numeric y, Numeric end_x, Numeric end_y, const std::string& fc = "r",
722+
const std::string ec = "k", Numeric head_length = 0.25, Numeric head_width = 0.1625) {
723+
PyObject* obj_x = PyFloat_FromDouble(x);
724+
PyObject* obj_y = PyFloat_FromDouble(y);
725+
PyObject* obj_end_x = PyFloat_FromDouble(end_x);
726+
PyObject* obj_end_y = PyFloat_FromDouble(end_y);
727+
728+
PyObject* kwargs = PyDict_New();
729+
PyDict_SetItemString(kwargs, "fc", PyString_FromString(fc.c_str()));
730+
PyDict_SetItemString(kwargs, "ec", PyString_FromString(ec.c_str()));
731+
PyDict_SetItemString(kwargs, "head_width", PyFloat_FromDouble(head_width));
732+
PyDict_SetItemString(kwargs, "head_length", PyFloat_FromDouble(head_length));
733+
734+
PyObject* plot_args = PyTuple_New(4);
735+
PyTuple_SetItem(plot_args, 0, obj_x);
736+
PyTuple_SetItem(plot_args, 1, obj_y);
737+
PyTuple_SetItem(plot_args, 2, obj_end_x);
738+
PyTuple_SetItem(plot_args, 3, obj_end_y);
739+
740+
PyObject* res =
741+
PyObject_Call(detail::_interpreter::get().s_python_function_arrow, plot_args, kwargs);
742+
743+
Py_DECREF(plot_args);
744+
Py_DECREF(kwargs);
745+
if (res)
746+
Py_DECREF(res);
747+
748+
return res;
749+
}
750+
712751
template< typename Numeric>
713752
bool hist(const std::vector<Numeric>& y, long bins=10,std::string color="b",
714753
double alpha=1.0, bool cumulative=false)
@@ -1040,6 +1079,39 @@ bool plot(const std::vector<NumericX>& x, const std::vector<NumericY>& y, const
10401079
return res;
10411080
}
10421081

1082+
template <typename NumericX, typename NumericY, typename NumericZ>
1083+
bool contour(const std::vector<NumericX>& x, const std::vector<NumericY>& y,
1084+
const std::vector<NumericZ>& z,
1085+
const std::map<std::string, std::string>& keywords = {}) {
1086+
assert(x.size() == y.size() && x.size() == z.size());
1087+
1088+
PyObject* xarray = get_array(x);
1089+
PyObject* yarray = get_array(y);
1090+
PyObject* zarray = get_array(z);
1091+
1092+
PyObject* plot_args = PyTuple_New(3);
1093+
PyTuple_SetItem(plot_args, 0, xarray);
1094+
PyTuple_SetItem(plot_args, 1, yarray);
1095+
PyTuple_SetItem(plot_args, 2, zarray);
1096+
1097+
// construct keyword args
1098+
PyObject* kwargs = PyDict_New();
1099+
for (std::map<std::string, std::string>::const_iterator it = keywords.begin();
1100+
it != keywords.end(); ++it) {
1101+
PyDict_SetItemString(kwargs, it->first.c_str(), PyUnicode_FromString(it->second.c_str()));
1102+
}
1103+
1104+
PyObject* res =
1105+
PyObject_Call(detail::_interpreter::get().s_python_function_contour, plot_args, kwargs);
1106+
1107+
Py_DECREF(kwargs);
1108+
Py_DECREF(plot_args);
1109+
if (res)
1110+
Py_DECREF(res);
1111+
1112+
return res;
1113+
}
1114+
10431115
template<typename NumericX, typename NumericY, typename NumericU, typename NumericW>
10441116
bool quiver(const std::vector<NumericX>& x, const std::vector<NumericY>& y, const std::vector<NumericU>& u, const std::vector<NumericW>& w, const std::map<std::string, std::string>& keywords = {})
10451117
{
@@ -1669,6 +1741,38 @@ inline void yticks(const std::vector<Numeric> &ticks, const std::map<std::string
16691741
yticks(ticks, {}, keywords);
16701742
}
16711743

1744+
template <typename Numeric> inline void margins(Numeric margin)
1745+
{
1746+
// construct positional args
1747+
PyObject* args = PyTuple_New(1);
1748+
PyTuple_SetItem(args, 0, PyFloat_FromDouble(margin));
1749+
1750+
PyObject* res =
1751+
PyObject_CallObject(detail::_interpreter::get().s_python_function_margins, args);
1752+
if (!res)
1753+
throw std::runtime_error("Call to margins() failed.");
1754+
1755+
Py_DECREF(args);
1756+
Py_DECREF(res);
1757+
}
1758+
1759+
template <typename Numeric> inline void margins(Numeric margin_x, Numeric margin_y)
1760+
{
1761+
// construct positional args
1762+
PyObject* args = PyTuple_New(2);
1763+
PyTuple_SetItem(args, 0, PyFloat_FromDouble(margin_x));
1764+
PyTuple_SetItem(args, 1, PyFloat_FromDouble(margin_y));
1765+
1766+
PyObject* res =
1767+
PyObject_CallObject(detail::_interpreter::get().s_python_function_margins, args);
1768+
if (!res)
1769+
throw std::runtime_error("Call to margins() failed.");
1770+
1771+
Py_DECREF(args);
1772+
Py_DECREF(res);
1773+
}
1774+
1775+
16721776
inline void tick_params(const std::map<std::string, std::string>& keywords, const std::string axis = "both")
16731777
{
16741778
detail::_interpreter::get();
@@ -2069,6 +2173,18 @@ inline void clf() {
20692173
Py_DECREF(res);
20702174
}
20712175

2176+
inline void cla() {
2177+
detail::_interpreter::get();
2178+
2179+
PyObject* res = PyObject_CallObject(detail::_interpreter::get().s_python_function_cla,
2180+
detail::_interpreter::get().s_python_empty_tuple);
2181+
2182+
if (!res)
2183+
throw std::runtime_error("Call to cla() failed.");
2184+
2185+
Py_DECREF(res);
2186+
}
2187+
20722188
inline void ion() {
20732189
detail::_interpreter::get();
20742190

0 commit comments

Comments
 (0)