Skip to content

Commit 2faab93

Browse files
committed
errorbarplot for eigen
1 parent a5cef23 commit 2faab93

File tree

2 files changed

+47
-3
lines changed

2 files changed

+47
-3
lines changed

examples/errorbar.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include <cmath>
2+
#include "../matplotlibcpp.h"
3+
namespace plt = matplotlibcpp;
4+
5+
#ifdef WITH_EIGEN
6+
#include <Eigen/Dense>
7+
#endif
8+
9+
template <typename Vector>
10+
void get_data(Vector& x, Vector& y, Vector& err) {
11+
// get number of data points
12+
const unsigned n = x.size();
13+
assert(y.size() == n && err.size() == n);
14+
15+
// compute data
16+
for (unsigned i = 0; i < n; ++i) {
17+
*(x.data() + i) = 1.0 * i / n;
18+
*(y.data() + i) = sin(2.0 * M_PI * i / n);
19+
*(err.data() + i) = exp(-2.0 * i / n);
20+
}
21+
}
22+
23+
template <typename Vector>
24+
void plot(unsigned n) {
25+
Vector x(n), y(n), err(n);
26+
get_data(x, y, err);
27+
28+
plt::figure();
29+
plt::errorbar(x, y, err);
30+
plt::show();
31+
}
32+
33+
int main() {
34+
35+
plot<std::vector<double>>(10);
36+
37+
#ifdef WITH_EIGEN
38+
#include <iostream>
39+
std::cout << "Hello!\n";
40+
plot<Eigen::VectorXd>(10);
41+
#endif
42+
43+
return 0;
44+
}

matplotlibcpp.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1072,9 +1072,9 @@ bool stem(const std::vector<NumericX> &x, const std::vector<NumericY> &y,
10721072
return res;
10731073
}
10741074

1075-
template <typename NumericX, typename NumericY>
1076-
bool errorbar(const std::vector<NumericX> &x, const std::vector<NumericY> &y,
1077-
const std::vector<NumericX> &yerr,
1075+
template <typename VectorX, typename VectorY>
1076+
bool errorbar(const VectorX &x, const VectorY &y,
1077+
const VectorY &yerr,
10781078
const std::map<std::string, std::string> &keywords = {}) {
10791079
assert(x.size() == y.size());
10801080

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