8000 Add float value support in keyword args. · 1z2s3e4v/matplotlib-cpp@c20513c · GitHub
[go: up one dir, main page]

Skip to content

Commit c20513c

Browse files
committed
Add float value support in keyword args.
1 parent e2292d5 commit c20513c

File tree

2 files changed

+31
-11
lines changed

2 files changed

+31
-11
lines changed

examples/quiver.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ namespace plt = matplotlibcpp;
77

88
int main() {
99
// u and v are respectively the x and y components of the arrows we're plotting
10-
int n0 = 11;
11-
int n1 = 11;
12-
int n2 = 11;
10+
int n0 = 5;
11+
int n1 = 5;
12+
int n2 = 5;
1313

1414
int *x, *y, *z, *u, *v, *w;
1515
x = new int[n0 * n1 * n2];
@@ -23,17 +23,17 @@ int main() {
2323
for (int j = 0; j < n1; j++) {
2424
for (int k = 0; k < n2; k++) {
2525
int idx = k + n2 * (j + n1 * i);
26-
x[idx] = k - 5;
27-
y[idx] = j - 5;
28-
z[idx] = i - 5;
29-
u[idx] = k - 5;
30-
v[idx] = j - 5;
31-
w[idx] = i - 5;
26+
x[idx] = k - 2;
27+
y[idx] = j - 2;
28+
z[idx] = i - 2;
29+
u[idx] = k - 2;
30+
v[idx] = j - 2;
31+
w[idx] = i - 2;
3232
}
3333
}
3434
}
3535

36-
std::map<std::string, std::string> kwargs = {{"normalize", "True"}};
36+
std::map<std::string, std::string> kwargs = {{"normalize", "True"}, {"length", "0.3"}};
3737
plt::quiver(x, y, z, u, v, w, n0, n1, n2, kwargs);
3838
plt::show();
3939

matplotlibcpp.h

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <cstdint> // <cstdint> requires c++11 support
1515
#include <functional>
1616
#include <string> // std::stod
17+
#include <sstream>
1718

1819
#ifndef WITHOUT_NUMPY
1920
# define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
@@ -1457,6 +1458,14 @@ bool contour(const std::vector<NumericX>& x, const std::vector<NumericY>& y,
14571458
return res;
14581459
}
14591460

1461+
bool isFloat(std::string s) {
1462+
std::istringstream iss(s);
1463+
float f;
1464+
iss >> std::noskipws >> f; // noskipws considers leading whitespace invalid
1465+
// Check the entire string was consumed and if either failbit or badbit is set
1466+
return iss.eof() && !iss.fail();
1467+
}
1468+
14601469
template<typename NumericX, typename NumericY, typename NumericU, typename NumericW>
14611470
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 = {})
14621471
{
@@ -1513,7 +1522,12 @@ bool quiver(const NumericX* x, const NumericY* y, const NumericU* u, const Numer
15131522
PyObject* kwargs = PyDict_New();
15141523
for(std::map<std::string, std::string>::const_iterator it = keywords.begin(); it != keywords.end(); ++it)
15151524
{
1516-
PyDict_SetItemString(kwargs, it->first.c_str(), PyUnicode_FromString(it->second.c_str()));
1525+
if(isFloat(it->second)) {
1526+
PyObject *float_str = PyUnicode_FromString(it->second.c_str());
1527+
PyDict_SetItemString(kwargs, it->first.c_str(), PyFloat_FromString(float_str));
1528+
} else {
1529+
PyDict_SetItemString(kwargs, it->first.c_str(), PyUnicode_FromString(it->second.c_str()));
1530+
}
15171531
}
15181532

15191533
PyObject* res = PyObject_Call(
@@ -1656,7 +1670,13 @@ bool quiver(const NumericX* x, const NumericY* y, const NumericZ* z, const Numer
16561670
PyObject* kwargs = PyDict_New();
16571671
for(std::map<std::string, std::string>::const_iterator it = keywords.begin(); it != keywords.end(); ++it)
16581672
{
1673+
if(isFloat(it->second)) {
1674+
PyObject *float_str = PyUnicode_FromString(it->second.c_str());
1675+
PyDict_SetItemString(kwargs, it->first.c_str(), PyFloat_FromString(float_str));
1676+
}
1677+
else {
16591678
PyDict_SetItemString(kwargs, it->first.c_str(), PyUnicode_FromString(it->second.c_str()));
1679+
}
16601680
}
16611681

16621682
//get figure gca to enable 3d projection

0 commit comments

Comments
 (0)
0