8000 Add bar plot and subplots_adjust · lingbo-yu/matplotlib-cpp@94f6c01 · GitHub
[go: up one dir, main page]

Skip to content

Commit 94f6c01

Browse files
LiuWeijunAntihosBenno Evers
authored and
Benno Evers
committed
Add bar plot and subplots_adjust
1 parent 8baa582 commit 94f6c01

File tree

5 files changed

+86
-3
lines changed

5 files changed

+86
-3
lines changed

Makefile

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
examples: minimal basic modern animation nonblock xkcd quiver
1+
examples: minimal basic modern animation nonblock xkcd quiver bar
22

33
minimal: examples/minimal.cpp matplotlibcpp.h
44
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
2121
xkcd: examples/xkcd.cpp matplotlibcpp.h
2222
cd examples && g++ xkcd.cpp -I/usr/include/python2.7 -lpython2.7 -o xkcd -std=c++11
2323

24+
bar: examples/bar.cpp matplotlibcpp.h
25+
cd examples && g++ bar.cpp -I/usr/include/python2.7 -lpython2.7 -o bar -std=c++11
26+
2427
clean:
25-
rm -f examples/{minimal,basic,modern,animation,nonblock,xkcd,quiver}
28+
rm -f examples/{minimal,basic,modern,animation,nonblock,xkcd,quiver,bar}

contrib/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,4 @@ add_executable(modern ${CMAKE_CURRENT_SOURCE_DIR}/../examples/modern.cpp)
2323
add_executable(animation ${CMAKE_CURRENT_SOURCE_DIR}/../examples/animation.cpp)
2424
add_executable(nonblock ${CMAKE_CURRENT_SOURCE_DIR}/../examples/nonblock.cpp)
2525
add_executable(xkcd ${CMAKE_CURRENT_SOURCE_DIR}/../examples/xkcd.cpp)
26+
add_executable(bar ${CMAKE_CURRENT_SOURCE_DIR}/../examples/bar.cpp)

examples/bar.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#define _USE_MATH_DEFINES
2+
3+
#include <iostream>
4+
#include <string>
5+
#include "../matplotlibcpp.h"
6+
namespace plt = matplotlibcpp;
7+
8+
int main(int argc, char **argv) {
9+
std::vector<int> test_data;
10+
for (int i = 0; i < 20; i++) {
11+
test_data.push_back(i);
12+
}
13+
14+
plt::bar(test_data);
15+
plt::show();
16+
17+
return (0);
18+
}

examples/bar.png

11.7 KB
Loading

matplotlibcpp.h

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ struct _interpreter {
6767
PyObject *s_python_function_xkcd;
6868
PyObject *s_python_function_text;
6969
PyObject *s_python_function_suptitle;
70+
PyObject *s_python_function_bar;
71+
PyObject *s_python_function_subplots_adjust;
72+
7073

7174
/* For now, _interpreter is implemented as a singleton since its currently not possible to have
7275
multiple independent embedded python interpreters without patching the python source code
@@ -175,6 +178,8 @@ struct _interpreter {
175178
s_python_function_xkcd = PyObject_GetAttrString(pymod, "xkcd");
176179
s_python_function_text = PyObject_GetAttrString(pymod, "text");
177180
s_python_function_suptitle = PyObject_GetAttrString(pymod, "suptitle");
181+
s_python_function_bar = PyObject_GetAttrString(pymod,"bar");
182+
s_python_function_subplots_adjust = PyObject_GetAttrString(pymod,"subplots_adjust");
178183

179184
if( !s_python_function_show
180185
|| !s_python_function_close
@@ -209,6 +214,8 @@ struct _interpreter {
209214
|| !s_python_function_xkcd
210215
|| !s_python_function_text
211216
|| !s_python_function_suptitle
217+
|| !s_python_function_bar
218+
|| !s_python_function_subplots_adjust
212219
) { throw std::runtime_error("Couldn't find required function!"); }
213220

214221
if ( !PyFunction_Check(s_python_function_show)
@@ -243,6 +250,8 @@ struct _interpreter {
243250
|| !PyFunction_Check(s_python_function_xkcd)
244251
|| !PyFunction_Check(s_python_function_text)
245252
|| !PyFunction_Check(s_python_function_suptitle)
253+
|| !PyFunction_Check(s_python_function_bar)
254+
|| !PyFunction_Check(s_python_function_subplots_adjust)
246255
) { throw std::runtime_error("Python object is unexpectedly not a PyFunction."); }
247256

248257
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
440449
PyDict_SetItemString(kwargs, "color", PyString_FromString(color.c_str()));
441450
PyDict_SetItemString(kwargs, "alpha", PyFloat_FromDouble(alpha));
442451

443-
444452
PyObject* plot_args = PyTuple_New(1);
445453

446454
PyTuple_SetItem(plot_args, 0, yarray);
@@ -482,6 +490,59 @@ bool scatter(const std::vector<NumericX>& x,
482490
return res;
483491
}
484492

493+
template< typename Numeric>
494+
bool bar(const std::vector<Numeric>& y, std::string ec = "black", std::string ls = "-", double lw = 1.0,
495+
const std::map<std::string, std::string>& keywords = {})
496+
{
497+
PyObject* yarray = get_array(y);
498+
499+
std::vector<int> x;
500+
for (int i = 0; i < y.size(); i++)
501+
x.push_back(i);
502+
503+
PyObject* xarray = get_array(x);
504+
505+
PyObject* kwargs = PyDict_New();
506+
507+
PyDict_SetItemString(kwargs, "ec", PyString_FromString(ec.c_str()));
508+
PyDict_SetItemString(kwargs, "ls", PyString_FromString(ls.c_str()));
509+
PyDict_SetItemString(kwargs, "lw", PyFloat_FromDouble(lw));
510+
511+
PyObject* plot_args = PyTuple_New(2);
512+
PyTuple_SetItem(plot_args, 0, xarray);
513+
PyTuple_SetItem(plot_args, 1, yarray);
514+
515+
PyObject* res = PyObject_Call(detail::_interpreter::get().s_python_function_bar, plot_args, kwargs);
516+
517+
Py_DECREF(plot_args);
518+
Py_DECREF(kwargs);
519+
if(res) Py_DECREF(res);
520+
521+
return res;
522+
}
523+
524+
inline bool subplots_adjust(const std::map<std::string, double>& keywords = {})
525+
{
526+
527+
PyObject* kwargs = PyDict_New();
528+
for (std::map<std::string, double>::const_iterator it =
529+
keywords.begin(); it != keywords.end(); ++it) {
530+
PyDict_SetItemString(kwargs, it->first.c_str(),
531+
PyFloat_FromDouble(it->second));
532+
}
533+
534+
535+
PyObject* plot_args = PyTuple_New(0);
536+
537+
PyObject* res = PyObject_Call(detail::_interpreter::get().s_python_function_subplots_adjust, plot_args, kwargs);
538+
539+
Py_DECREF(plot_args);
540+
Py_DECREF(kwargs);
541+
if(res) Py_DECREF(res);
542+
543+
return res;
544+
}
545+
485546
template< typename Numeric>
486547
bool named_hist(std::string label,const std::vector<Numeric>& y, long bins=10, std::string color="b", double alpha=1.0)
487548
{

0 commit comments

Comments
 (0)
0