Skip to content

Commit 331029f

Browse files
alexdewarlava
authored andcommitted
Add support for xticks and yticks
1 parent 717e98e commit 331029f

File tree

1 file changed

+98
-0
lines changed

1 file changed

+98
-0
lines changed

matplotlibcpp.h

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ struct _interpreter {
5151
PyObject *s_python_function_axis;
5252
PyObject *s_python_function_xlabel;
5353
PyObject *s_python_function_ylabel;
54+
PyObject *s_python_function_xticks;
55+
PyObject *s_python_function_yticks;
5456
PyObject *s_python_function_grid;
5557
PyObject *s_python_function_clf;
5658
PyObject *s_python_function_errorbar;
@@ -149,6 +151,8 @@ struct _interpreter {
149151
s_python_function_axis = PyObject_GetAttrString(pymod, "axis");
150152
s_python_function_xlabel = PyObject_GetAttrString(pymod, "xlabel");
151153
s_python_function_ylabel = PyObject_GetAttrString(pymod, "ylabel");
154+
s_python_function_xticks = PyObject_GetAttrString(pymod, "xticks");
155+
s_python_function_yticks = PyObject_GetAttrString(pymod, "yticks");
152156
s_python_function_grid = PyObject_GetAttrString(pymod, "grid");
153157
s_python_function_xlim = PyObject_GetAttrString(pymod, "xlim");
154158
s_python_function_ion = PyObject_GetAttrString(pymod, "ion");
@@ -849,6 +853,100 @@ inline double* ylim()
849853
return arr;
850854
}
851855

856+
template<typename Numeric>
857+
inline void xticks(const std::vector<Numeric> &ticks, const std::vector<std::string> &labels = {}, const std::map<std::string, std::string>& keywords = {})
858+
{
859+
assert(labels.size() == 0 || ticks.size() == labels.size());
860+
861+
// using numpy array
862+
PyObject* ticksarray = get_array(ticks);
863+
864+
PyObject* args;
865+
if(labels.size() == 0) {
866+
// construct positional args
867+
args = PyTuple_New(1);
868+
PyTuple_SetItem(args, 0, ticksarray);
869+
} else {
870+
// make tuple of tick labels
871+
PyObject* labelstuple = PyTuple_New(labels.size());
872+
for (size_t i = 0; i < labels.size(); i++)
873+
PyTuple_SetItem(labelstuple, i, PyUnicode_FromString(labels[i].c_str()));
874+
875+
// construct positional args
876+
args = PyTuple_New(2);
877+
PyTuple_SetItem(args, 0, ticksarray);
878+
PyTuple_SetItem(args, 1, labelstuple);
879+
}
880+
881+
// construct keyword args
882+
PyObject* kwargs = PyDict_New();
883+
for(std::map<std::string, std::string>::const_iterator it = keywords.begin(); it != keywords.end(); ++it)
884+
{
885+
PyDict_SetItemString(kwargs, it->first.c_str(), PyString_FromString(it->second.c_str()));
886+
}
887+
888+
PyObject* res = PyObject_Call(detail::_interpreter::get().s_python_function_xticks, args, kwargs);
889+
890+
Py_DECREF(args);
891+
Py_DECREF(kwargs);
892+
if(!res) throw std::runtime_error("Call to xticks() failed");
893+
894+
Py_DECREF(res);
895+
}
896+
897+
template<typename Numeric>
898+
inline void xticks(const std::vector<Numeric> &ticks, const std::map<std::string, std::string>& keywords)
899+
{
900+
xticks(ticks, {}, keywords);
901+
}
902+
903+
template<typename Numeric>
904+
inline void yticks(const std::vector<Numeric> &ticks, const std::vector<std::string> &labels = {}, const std::map<std::string, std::string>& keywords = {})
905+
{
906+
assert(labels.size() == 0 || ticks.size() == labels.size());
907+
908+
// using numpy array
909+
PyObject* ticksarray = get_array(ticks);
910+
911+
PyObject* args;
912+
if(labels.size() == 0) {
913+
// construct positional args
914+
args = PyTuple_New(1);
915+
PyTuple_SetItem(args, 0, ticksarray);
916+
} else {
917+
// make tuple of tick labels
918+
PyObject* labelstuple = PyTuple_New(labels.size());
919+
for (size_t i = 0; i < labels.size(); i++)
920+
PyTuple_SetItem(labelstuple, i, PyUnicode_FromString(labels[i].c_str()));
921+
922+
// construct positional args
923+
args = PyTuple_New(2);
924+
PyTuple_SetItem(args, 0, ticksarray);
925+
PyTuple_SetItem(args, 1, labelstuple);
926+
}
927+
928+
// construct keyword args
929+
PyObject* kwargs = PyDict_New();
930+
for(std::map<std::string, std::string>::const_iterator it = keywords.begin(); it != keywords.end(); ++it)
931+
{
932+
PyDict_SetItemString(kwargs, it->first.c_str(), PyString_FromString(it->second.c_str()));
933+
}
934+
935+
PyObject* res = PyObject_Call(detail::_interpreter::get().s_python_function_yticks, args, kwargs);
936+
937+
Py_DECREF(args);
938+
Py_DECREF(kwargs);
939+
if(!res) throw std::runtime_error("Call to yticks() failed");
940+
941+
Py_DECREF(res);
942+
}
943+
944+
template<typename Numeric>
945+
inline void yticks(const std::vector<Numeric> &ticks, const std::map<std::string, std::string>& keywords)
946+
{
947+
yticks(ticks, {}, keywords);
948+
}
949+
852950
inline void subplot(long nrows, long ncols, long plot_number)
853951
{
854952
// construct positional args

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