Skip to content

Commit 9c58c6c

Browse files
committed
update modern and minimal usages, fix them in matplotlibcpp.h
1 parent 46ae9f8 commit 9c58c6c

File tree

4 files changed

+26
-23
lines changed

4 files changed

+26
-23
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ definitions = -std=c++11
1717
eigen_include = -I /usr/local/include/eigen3
1818

1919
# Executable names for examples (w/o Eigen)
20-
example_execs = minimal basic animation nonblock xkcd quiver bar surface subplot fill_inbetween fill update
20+
example_execs = minimal modern basic animation nonblock xkcd quiver bar surface subplot fill_inbetween fill update
2121

2222
# Executable names for examples using Eigen
2323
eigen_execs = eigen loglog semilogx semilogy small

examples/minimal.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
namespace plt = matplotlibcpp;
55

66
int main() {
7-
std::vector<double> y = {1, 3, 2, 4};
7+
// plot(y) - the x-coordinates are implicitly set to [0,1,...,n)
8+
// plt::plot({1, 2, 3, 4});
9+
std::vector<double> y = {1, 2, 3, 4};
810
plt::plot(y);
911
plt::savefig("minimal.pdf");
1012
}

examples/modern.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
11
#define _USE_MATH_DEFINES
22
#include "../matplotlibcpp.h"
33
#include <cmath>
4+
#include <vector>
45

56
namespace plt = matplotlibcpp;
67

78
int main() {
8-
// plot(y) - the x-coordinates are implicitly set to [0,1,...,n)
9-
// plt::plot({1,2,3,4});
10-
119
// Prepare data for parametric plot.
1210
int n = 5000; // number of data points
13-
Eigen::VectorXd x(n), y(n), z(n);
11+
std::vector<double> x(n), y(n), z(n);
1412
for (int i = 0; i < n; ++i) {
1513
double t = 2 * M_PI * i / n;
16-
x(i) = 16 * sin(t) * sin(t) * sin(t);
17-
y(i) = 13 * cos(t) - 5 * cos(2 * t) - 2 * cos(3 * t) - cos(4 * t);
18-
z(i) = 12.5 + abs(sin(x(i)));
14+
x.at(i) = 16 * sin(t) * sin(t) * sin(t);
15+
y.at(i) = 13 * cos(t) - 5 * cos(2 * t) - 2 * cos(3 * t) - cos(4 * t);
16+
z.at(i) = 12.5 + abs(sin(x.at(i)));
1917
}
2018

2119
// plot() takes an arbitrary number of (x,y,format)-triples.

matplotlibcpp.h

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
//
2-
// Changes:
3-
// * Make work for Eigen Vectors and Matrices
2+
// Wishlist:
3+
// * (WIP) Make work for Eigen Vectors and Matrices
4+
// * add submodule for our own functions such as spy
5+
// * export functions in different files for better structure
6+
// * make plot(y) work with x of unsigned type, or get to the bottom of that
7+
// problem at least
8+
//
9+
// Changed:
410
// * Implement a better way for named_plot, maybe just as additional
511
// method with extra keyword
612
// * add location keyword for legend
7-
// * add submodule for our own functions such as spy
813
//
914

1015
#pragma once
@@ -446,8 +451,7 @@ namespace detail {
446451
// @param s The formatting string for colour, marker and linestyle
447452
// @param keywords Additional keywords, such as label
448453
// @return true if plot was successful, false otherwise
449-
template <typename VectorX = std::vector<double>,
450-
typename VectorY = std::vector<double>>
454+
template <typename VectorX, typename VectorY>
451455
bool plot_base(PyObject *const pyfunc, const VectorX &x, const VectorY &y,
452456
const std::string &s = "",
453457
const std::map<std::string, std::string> &keywords = {}) {
@@ -497,11 +501,10 @@ bool plot(const VectorX &x, const VectorY &y,
497501
template <typename VectorY = std::vector<double>>
498502
bool plot(const VectorY &y, const std::string &format = "",
499503
const std::map<std::string, std::string> &keywords = {}) {
500-
// TODO can this be <size_t> or do we need <typename Vector::value_type>?
501-
// before the conversion of this function from vector<Numeric> to Vector
502-
// the created vector x was of the same type as y
503-
std::vector<std::size_t> x(y.size());
504-
for (std::size_t i = 0; i < x.size(); ++i)
504+
// Note: cannot be an unsigned type for some reason, yields an overflow
505+
// problem..
506+
std::vector<int> x(y.size());
507+
for (int i = 0; i < x.size(); ++i)
505508
x.at(i) = i;
506509

507510
return plot(x, y, format);
@@ -510,8 +513,8 @@ bool plot(const VectorY &y, const std::string &format = "",
510513
template <typename VectorY = std::vector<double>>
511514
bool plot(const VectorY &y,
512515
const std::map<std::string, std::string> &keywords) {
513-
std::vector<std::size_t> x(y.size());
514-
for (std::size_t i = 0; i < x.size(); ++i)
516+
std::vector<int> x(y.size());
517+
for (int i = 0; i < x.size(); ++i)
515518
x.at(i) = i;
516519

517520
return plot(x, y, "", keywords);
@@ -1666,7 +1669,6 @@ ginput(const int numClicks = 1,
16661669
return out;
16671670
}
16681671

1669-
// Actually, is there any reason not to call this automatically for every plot?
16701672
inline void tight_layout() {
16711673
PyObject *res = PyObject_CallObject(
16721674
detail::_interpreter::get().s_python_function_tight_layout,
@@ -1678,15 +1680,16 @@ inline void tight_layout() {
16781680
Py_DECREF(res);
16791681
}
16801682

1681-
#if 0
16821683
// recursion stop for the below
16831684
template <typename... Args> bool plot() { return true; }
16841685

1686+
// enable plotting of multiple triples (x, y, format)
16851687
template <typename A, typename B, typename... Args>
16861688
bool plot(const A &a, const B &b, const std::string &format, Args... args) {
16871689
return plot(a, b, format) && plot(args...);
16881690
}
16891691

1692+
#if 0
16901693
/*
16911694
* This group of plot() functions is needed to support initializer lists, i.e.
16921695
* calling plot( {1,2,3,4} )

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