8000 Add fill plot with example · sibaba8888/matplotlib-cpp@d89878b · GitHub
[go: up one dir, main page]

Skip to content

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit d89878b

Browse files
Tobias TschirchBenno Evers
Tobias Tschirch
authored and
Benno Evers
committed
Add fill plot with example
1 parent 0e39f8f commit d89878b

File tree

4 files changed

+78
-4
lines changed

4 files changed

+78
-4
lines changed

Makefile

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
examples: minimal basic modern animation nonblock xkcd quiver bar surface
1+
examples: minimal basic modern animation nonblock xkcd quiver bar surface fill_inbetween fill
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
@@ -27,5 +27,11 @@ bar: examples/bar.cpp matplotlibcpp.h
2727
surface: examples/surface.cpp matplotlibcpp.h
2828
cd examples && g++ surface.cpp -I/usr/include/python2.7 -lpython2.7 -o surface -std=c++11
2929

30+
fill_inbetween: examples/fill_inbetween.cpp matplotlibcpp.h
31+
cd examples && g++ fill_inbetween.cpp -I/usr/include/python2.7 -lpython2.7 -o fill_inbetween -std=c++11
32+
33+
fill: examples/fill.cpp matplotlibcpp.h
34+
cd examples && g++ fill.cpp -I/usr/include/python2.7 -lpython2.7 -o fill -std=c++11
35+
3036
clean:
31-
rm -f examples/{minimal,basic,modern,animation,nonblock,xkcd,quiver,bar}
37+
rm -f examples/{minimal,basic,modern,animation,nonblock,xkcd,quiver,bar,surface,fill_inbetween,fill}

examples/fill.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#define _USE_MATH_DEFINES
2+
#include "../matplotlibcpp.h"
3+
#include <cmath>
4+
5+
using namespace std;
6+
namespace plt = matplotlibcpp;
7+
8+
// Example fill plot taken from:
9+
// https://matplotlib.org/gallery/misc/fill_spiral.html
10+
int main() {
11+
// Prepare data.
12+
vector<double> theta;
13+
for (double d = 0; d < 8 * M_PI; d += 0.1)
14+
theta.push_back(d);
15+
16+
const int a = 1;
17+
const double b = 0.2;
18+
19+
for (double dt = 0; dt < 2 * M_PI; dt += M_PI/2.0) {
20+
vector<double> x1, y1, x2, y2;
21+
for (double th : theta) {
22+
x1.push_back( a*cos(th + dt) * exp(b*th) );
23+
y1.push_back( a*sin(th + dt) * exp(b*th) );
24+
25+
x2.push_back( a*cos(th + dt + M_PI/4.0) * exp(b*th) );
26+
y2.push_back( a*sin(th + dt + M_PI/4.0) * exp(b*th) );
27+
}
28+
29+
x1.insert(x1.end(), x2.rbegin(), x2.rend());
30+
y1.insert(y1.end(), y2.rbegin(), y2.rend());
31+
32+
plt::fill(x1, y1, {});
33+
}
34+
plt::show();
35+
}

examples/fill.png

61.5 KB
Loading

matplotlibcpp.h

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ struct _interpreter {
3939
PyObject *s_python_function_semilogx;
4040
PyObject *s_python_function_semilogy;
4141
PyObject *s_python_function_loglog;
42+
PyObject *s_python_function_fill;
4243
PyObject *s_python_function_fill_between;
4344
PyObject *s_python_function_hist;
4445
PyObject *s_python_function_scatter;
@@ -155,6 +156,7 @@ struct _interpreter {
155156
s_python_function_semilogx = PyObject_GetAttrString(pymod, "semilogx");
156157
s_python_function_semilogy = PyObject_GetAttrString(pymod, "semilogy");
157158
s_python_function_loglog = PyObject_GetAttrString(pymod, "loglog");
159+
s_python_function_fill = PyObject_GetAttrString(pymod, "fill");
158160
s_python_function_fill_between = PyObject_GetAttrString(pymod, "fill_between");
159161
s_python_function_hist = PyObject_GetAttrString(pymod,"hist");
160162
s_python_function_scatter = PyObject_GetAttrString(pymod,"scatter");
@@ -194,6 +196,7 @@ struct _interpreter {
194196
|| !s_python_function_semilogx
195197
|| !s_python_function_semilogy
196198
|| !s_python_function_loglog
199+
|| !s_python_function_fill
197200
|| !s_python_function_fill_between
198201
|| !s_python_function_subplot
199202
|| !s_python_function_legend
@@ -231,6 +234,7 @@ struct _interpreter {
231234
|| !PyFunction_Check(s_python_function_semilogx)
232235
|| !PyFunction_Check(s_python_function_semilogy)
233236
|| !PyFunction_Check(s_python_function_loglog)
237+
|| !PyFunction_Check(s_python_function_fill)
234238
|| !PyFunction_Check(s_python_function_fill_between)
235239
|| !PyFunction_Check(s_python_function_subplot)
236240
|| !PyFunction_Check(s_python_function_legend)
@@ -523,6 +527,36 @@ bool stem(const std::vector<Numeric> &x, const std::vector<Numeric> &y, const st
523527
return res;
524528
}
525529

530+
template< typename Numeric >
531+
bool fill(const std::vector<Numeric>& x, const std::vector<Numeric>& y, const std::map<std::string, std::string>& keywords)
532+
{
533+
assert(x.size() == y.size());
534+
535+
// using numpy arrays
536+
PyObject* xarray = get_array(x);
537+
PyObject* yarray = get_array(y);
538+
539+
// construct positional args
540+
PyObject* args = PyTuple_New(2);
541+
PyTuple_SetItem(args, 0, xarray);
542+
PyTuple_SetItem(args, 1, yarray);
543+
544+
// construct keyword args
545+
PyObject* kwargs = PyDict_New();
546+
for (auto it = keywords.begin(); it != keywords.end(); ++it) {
547+
PyDict_SetItemString(kwargs, it->first.c_str(), PyUnicode_FromString(it->second.c_str()));
548+
}
549+
550+
PyObject* res = PyObject_Call(detail::_interpreter::get().s_python_function_fill, args, kwargs);
551+
552+
Py_DECREF(args);
553+
Py_DECREF(kwargs);
554+
555+
if (res) Py_DECREF(res);
556+
557+
return res;
558+
}
559+
526560
template< typename Numeric >
527561
bool fill_between(const std::vector<Numeric>& x, const std::vector<Numeric>& y1, const std::vector<Numeric>& y2, const std::map<std::string, std::string>& keywords)
528562
{
@@ -542,8 +576,7 @@ bool fill_between(const std::vector<Numeric>& x, const std::vector<Numeric>& y1,
542576

543577
// construct keyword args
544578
PyObject* kwargs = PyDict_New();
545-
for(std::map<std::string, std::string>::const_iterator it = keywords.begin(); it != keywords.end(); ++it)
546-
{
579+
for(std::map<std::string, std::string>::const_iterator it = keywords.begin(); it != keywords.end(); ++it) {
547580
PyDict_SetItemString(kwargs, it->first.c_str(), PyUnicode_FromString(it->second.c_str()));
548581
}
549582

0 commit comments

Comments
 (0)
0