Skip to content

Commit 4ea1200

Browse files
hyhuangBenno Evers
authored andcommitted
Add non-blocking mode of show and an example
1 parent ba9343f commit 4ea1200

File tree

2 files changed

+67
-10
lines changed

2 files changed

+67
-10
lines changed

examples/nonblock.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#define _USE_MATH_DEFINES
2+
#include <cmath>
3+
#include "matplotlibcpp.h"
4+
5+
namespace plt = matplotlibcpp;
6+
7+
8+
using namespace matplotlibcpp;
9+
using namespace std;
10+
11+
int main()
12+
{
13+
// Prepare data.
14+
int n = 5000;
15+
std::vector<double> x(n), y(n), z(n), w(n,2);
16+
for(int i=0; i<n; ++i) {
17+
x.at(i) = i*i;
18+
y.at(i) = sin(2*M_PI*i/360.0);
19+
z.at(i) = log(i);
20+
}
21+
22+
// Plot line from given x and y data. Color is selected automatically.
23+
plt::subplot(2,2,1);
24+
plt::plot(x, y);
25+
26+
// Plot a red dashed line from given x and y data.
27+
plt::subplot(2,2,2);
28+
plt::plot(x, w,"r--");
29+
30+
// Plot a line whose name will show up as "log(x)" in the legend.
31+
plt::subplot(2,2,3);
32+
plt::named_plot("log(x)", x, z);
33+
34+
// Set x-axis to interval [0,1000000]
35+
plt::xlim(0, 1000*1000);
36+
37+
// Add graph title
38+
plt::title("Sample figure");
39+
// Enable legend.
40+
plt::legend();
41+
42+
plt::show(false);
43+
44+
cout << "matplotlibcpp::show() is working in an non-blocking mode" << endl;
45+
getchar();
46+
}

matplotlibcpp.h

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -648,16 +648,27 @@ namespace matplotlibcpp {
648648
// if PyDeCRFF, the function doesn't work on Mac OS
649649
}
650650

651-
inline void show()
652-
{
653-
PyObject* res = PyObject_CallObject(
654-
detail::_interpreter::get().s_python_function_show,
655-
detail::_interpreter::get().s_python_empty_tuple);
656-
657-
if (!res) throw std::runtime_error("Call to show() failed.");
658-
659-
Py_DECREF(res);
660-
}
651+
inline void show(const bool block = true)
652+
{
653+
PyObject* res;
654+
if(block)
655+
{
656+
res = PyObject_CallObject(
657+
detail::_interpreter::get().s_python_function_show,
658+
detail::_interpreter::get().s_python_empty_tuple);
659+
}
660+
else
661+
{
662+
PyObject *kwargs = PyDict_New();
663+
PyDict_SetItemString(kwargs, "block", Py_False);
664+
res = PyObject_Call( detail::_interpreter::get().s_python_function_show, detail::_interpreter::get().s_python_empty_tuple, kwargs);
665+
}
666+
667+
668+
if (!res) throw std::runtime_error("Call to show() failed.");
669+
670+
Py_DECREF(res);
671+
}
661672

662673
inline void draw()
663674
{

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