Skip to content

Commit c7d0640

Browse files
committed
plot_surface generalized to typename Matrix instead of nested vectors
1 parent cb8f135 commit c7d0640

File tree

2 files changed

+70
-13
lines changed

2 files changed

+70
-13
lines changed

examples/surface.cpp

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,37 @@
1+
#include <cmath>
12
#include "../matplotlibcpp.h"
3+
namespace plt = matplotlibcpp;
24

3-
#include <cmath>
5+
#ifdef WITH_EIGEN
6+
#include <Eigen/Dense>
7+
#endif
48

5-
namespace plt = matplotlibcpp;
9+
typedef std::vector<std::vector<double>> nested_vectors;
610

7-
int main()
8-
{
9-
std::vector<std::vector<double>> x, y, z;
11+
template <typename Matrix>
12+
void get_data(Matrix& x, Matrix& y, Matrix& z) {
13+
// check the sizes
14+
const unsigned ncols = x.cols(),
15+
nrows = x.rows();
16+
assert(ncols == y.cols() && ncols == z.cols());
17+
assert(nrows == y.rows() && nrows == z.rows());
18+
19+
// write data
20+
const double a = -5,
21+
b = 5;
22+
const double col_incr = (b - a) / ncols,
23+
row_incr = (b - a) / nrows;
24+
for (unsigned i = 0; i < nrows; ++i) {
25+
for (unsigned j = 0; j < ncols; ++j) {
26+
x(i, j) = i;
27+
y(i, j) = j;
28+
z(i, j) = ::std::sin(::std::hypot(a + i * col_incr, a + j * row_incr));
29+
}
30+
}
31+
}
32+
33+
template <>
34+
void get_data(nested_vectors& x, nested_vectors& y, nested_vectors& z) {
1035
for (double i = -5; i <= 5; i += 0.25) {
1136
std::vector<double> x_row, y_row, z_row;
1237
for (double j = -5; j <= 5; j += 0.25) {
@@ -18,7 +43,20 @@ int main()
1843
y.push_back(y_row);
1944
z.push_back(z_row);
2045
}
46+
}
2147

48+
49+
int main() {
50+
std::vector<std::vector<double>> x, y, z;
51+
get_data(x, y, z);
2252
plt::plot_surface(x, y, z);
2353
plt::show();
54+
55+
#ifdef WITH_EIGEN
56+
const unsigned n = 100; // resolution of hypot function
57+
Eigen::MatrixXd X(n,n), Y(n,n), Z(n,n);
58+
get_data(X, Y, Z);
59+
plt::plot_surface(X, Y, Z);
60+
plt::show();
61+
#endif
2462
}

matplotlibcpp.h

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
//
22
// Wishlist:
33
// * (WIP) Make work for Eigen Vectors and Matrices
4-
// * add submodule for our own functions such as spy
54
// * export functions in different files for better structure
65
// * make plot(y) work with x of unsigned type, or get to the bottom of that
76
// problem at least
7+
// * errorbar for xerr and yerr
8+
// * errorbar for yerr of shape (2, N)
89
//
910
// Changed:
1011
// * Implement a better way for named_plot, maybe just as additional
@@ -445,7 +446,8 @@ PyObject *get_2darray(const Matrix &A) {
445446

446447
#else // fallback if we don't have numpy: copy every element of the given vector
447448

448-
template <typename Vector> PyObject *get_array(const Vector &v) {
449+
template <typename Vector>
450+
PyObject *get_array(const Vector &v) {
449451
detail::_interpreter::get();
450452
PyObject *list = PyList_New(v.size());
451453
for (size_t i = 0; i < v.size(); ++i) {
@@ -460,8 +462,8 @@ namespace detail {
460462
// @brief Since most of the plot commands require the exact same usage apart
461463
// from the call to the correct Python function, we encapsulate this
462464
// @param pyfunc The matplotlib function to be called with the given arguments
463-
// @param x The x vector, must support std::vector methods
464-
// @param y The y vector, must support std::vector methods
465+
// @param x The x vector
466+
// @param y The y vector
465467
// @param s The formatting string for colour, marker and linestyle
466468
// @param keywords Additional keywords, such as label
467469
// @return true if plot was successful, false otherwise
@@ -499,19 +501,28 @@ bool plot_base(PyObject *const pyfunc, const VectorX &x, const VectorY &y,
499501

500502
} // namespace detail
501503

504+
// @brief standard plot function supporting the args (x, y, s, keywords)
505+
// @param x The x vector
506+
// @param y The y vector
507+
// @param s The formatting string
508+
// @param keywords Additional keywords
509+
// @return true, if successful, false otherwise
502510
template <typename VectorX, typename VectorY>
503511
bool plot(const VectorX &x, const VectorY &y, const std::string &s = "",
504512
const std::map<std::string, std::string> &keywords = {}) {
505513
return detail::plot_base(detail::_interpreter::get().s_python_function_plot,
506514
x, y, s, keywords);
507515
}
508516

517+
// @brief standard plot function without formatting string, needed if
518+
// keywords are given but formatting string is not
509519
template <typename VectorX, typename VectorY>
510520
bool plot(const VectorX &x, const VectorY &y,
511521
const std::map<std::string, std::string> &keywords) {
512522
return plot(x, y, "", keywords);
513523
}
514524

525+
// @brief standard plot function if x data is not specified
515526
template <typename VectorY = std::vector<double>>
516527
bool plot(const VectorY &y, const std::string &format = "",
517528
const std::map<std::string, std::string> &keywords = {}) {
@@ -524,6 +535,8 @@ bool plot(const VectorY &y, const std::string &format = "",
524535
return plot(x, y, format);
525536
}
526537

538+
// @brief standard plot function if x data is not specified and the formatting
539+
// string is missing
527540
template <typename VectorY = std::vector<double>>
528541
bool plot(const VectorY &y,
529542
const std::map<std::string, std::string> &keywords) {
@@ -534,6 +547,7 @@ bool plot(const VectorY &y,
534547
return plot(x, y, "", keywords);
535548
}
536549

550+
// @brief loglog plot function, see `plot` for more detail
537551
template <typename VectorX, typename VectorY>
538552
bool loglog(const VectorX &x, const VectorY &y, const std::string &s = "",
539553
const std::map<std::string, std::string> &keywords = {}) {
@@ -567,6 +581,7 @@ bool loglog(const VectorY &y,
567581
return loglog(x, y, "", keywords);
568582
}
569583

584+
// @brief semilogx plot function, see `plot` for more detail
570585
template <typename VectorX, typename VectorY>
571586
bool semilogx(const VectorX &x, const VectorY &y, const std::string &s = "",
572587
const std::map<std::string, std::string> &keywords = {}) {
@@ -601,6 +616,7 @@ bool semilogx(const VectorY &y,
601616
return semilogx(x, y, "", keywords);
602617
}
603618

619+
// @brief semilogy plot function, see `plot` for more detail
604620
template <typename VectorX, typename VectorY>
605621
bool semilogy(const VectorX &x, const VectorY &y, const std::string &s = "",
606622
const std::map<std::string, std::string> &keywords = {}) {
@@ -635,10 +651,13 @@ bool semilogy(const VectorY &y,
635651
return semilogy(x, y, "", keywords);
636652
}
637653

638-
template <typename Numeric>
639-
void plot_surface(const std::vector<::std::vector<Numeric>> &x,
640-
const std::vector<::std::vector<Numeric>> &y,
641-
const std::vector<::std::vector<Numeric>> &z,
654+
// @brief plot_surface for datapoints (x_ij, y_ij, z_ij) with i,j = 0..n
655+
// @param x The x values of the datapoints in a matrix
656+
// @param y The y values of the datapoints in a matrix
657+
// @param z The function value of the datapoints in a matrix
658+
// @param keywords Additional keywords
659+
template <typename Matrix>
660+
void plot_surface(const Matrix &x, const Matrix& y, const Matrix& z,
642661
const std::map<std::string, std::string> &keywords =
643662
std::map<std::string, std::string>()) {
644663
// We lazily load the modules here the first time this function is called

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