-
Notifications
You must be signed in to change notification settings - Fork 1.2k
matplotlib.h: add support for contiguous_range (C++20) and ranges.cpp…
… example
- Loading branch information
commit 3f5b27618cf79c0fbc0219646b298218a3aa9220
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
// | ||
// g++ -std=c++20 -g -Wall -o ranges $(python-config --includes) ranges.cpp $(python-config --ldflags --embed) | ||
// | ||
// g++ -std=c++17 -g -Wall -o ranges $(python-config --includes) ranges.cpp $(python-config --ldflags --embed) | ||
// | ||
#include "../matplotlibcpp.h" | ||
|
||
#include <iostream> | ||
#include <list> | ||
#include <map> | ||
#include <span> | ||
#include <string> | ||
#include <vector> | ||
|
||
#define UNUSED(x) (void)(x) | ||
|
||
using namespace std; | ||
namespace plt = matplotlibcpp; | ||
|
||
int main() | ||
{ | ||
plt::detail::_interpreter::get(); | ||
|
||
|
||
#if __cplusplus >= CPP20 | ||
|
||
// C-style arrays with multiple rows. | ||
|
||
// Care with column-major vs row-major! | ||
// C and Python are row-major, but usually a time series is column-major | ||
// and we want to plot the columns. | ||
// In the example below, these columns are [3,1,4,5] and [5,4,1,3], so | ||
// the data must be stored like this: | ||
time_t t[]={1, 2, 3, 4}; | ||
double data [] = { | ||
3, 5, | ||
1, 4, | ||
4, 1, | ||
5, 3 | ||
}; | ||
|
||
// Use std::span() to convert to a contiguous range (O(1)). | ||
// Data won't be copied, but passed as a pointer to Python. | ||
plt::plot(span(t, 4), span(data, 8)); | ||
plt::grid(true); | ||
plt::title("C-arrays, with multiple columns"); | ||
plt::show(); | ||
|
||
#else | ||
|
||
cerr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << ": " | ||
<< "No support for contiguous ranges." << endl; | ||
|
||
#endif | ||
|
||
// vectors | ||
|
||
// Vectors are also contiguous ranges. | ||
// In C++20, as with span, plot resolves to plot(contiguous_range). | ||
// In C++ < 20 plot resolves to plot(vector). | ||
vector<double> x={1, 2, 3, 4, 5}; | ||
vector<double> y={0, 1, 0, -1, 0}; | ||
plt::plot(x, y); | ||
plt::grid(true); | ||
plt::title("vectors"); | ||
plt::show(); | ||
|
||
|
||
// lists | ||
|
||
// By contrast, lists are non-contiguous (but iterable) containers, so | ||
// plot resolves to plot(iterable). | ||
list<double> u { 1, 2, 3, 4, 5}; | ||
list<double> v { 0, -1, 0, 1, 0}; | ||
plt::plot(u, v, ""); | ||
plt::grid(true); | ||
plt::title("Lists (non-contiguous ranges)"); | ||
plt::show(); | ||
|
||
|
||
// All together now | ||
#if __cplusplus >= CPP20 | ||
|
||
// If span is not last, plot resolves to plot(iterable), which copies data. | ||
// That's because in the dispatch plot() we have plot_impl() && plot() | ||
// (i.e. plot_impl() comes first), and we only have iterable and | ||
// callable plot_impl(). That sends us to the iterable plot_impl(), | ||
// rather than to plot(contiguous_range). | ||
// | ||
// TODO: have 3 tags plot_impl(): iterable, callable and contiguous range. | ||
plt::plot(span(t, 4), span(data, 8), "", x, y, "b", u, v, "r"); | ||
|
||
// This resolves to plot(contiguous_range) and does not copy data. | ||
// plt::plot(x, y, "b", u, v, "r", span(t, 4), span(data, 8)); | ||
#else | ||
plt::plot(x, y, "b", u, v, "r"); | ||
#endif | ||
plt::grid(true); | ||
plt::title("Variadic templates recursion"); | ||
plt::show(); | ||
|
||
plt::detail::_interpreter::kill(); | ||
|
||
return 0; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
fix segfault on exit with python3 #301
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Uh oh!
There was an error while loading. Please reload this page.
fix segfault on exit with python3 #301
Changes from 1 commit
b203af7
13ed23f
6255877
3f5b276
b0f57e5
a08b67a
f1ecd32
153b3aa
231138a
e1657bc
6be6a2a
3302036
86e719d
b87d8be
d772437
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.