8000 Merge pull request #1 from lava/master · sadads1337/matplotlib-cpp@9d185c6 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9d185c6

Browse files
authored
Merge pull request #1 from lava/master
Update
2 parents d687405 + 2843ebb commit 9d185c6

File tree

7 files changed

+498
-130
lines changed

7 files changed

+498
-130
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,6 @@
3333

3434
# Build
3535
/examples/build/*
36+
37+
# vim temp files
38+
*.sw*

Makefile

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
1-
# Use C++11
2-
CXXFLAGS += -std=c++11
1+
# Use C++11, dont warn on long-to-float conversion
2+
CXXFLAGS += -std=c++11 -Wno-conversion
33

44
# Default to using system's default version of python
55
PYTHON_BIN ?= python
66
PYTHON_CONFIG := $(PYTHON_BIN)-config
77
PYTHON_INCLUDE ?= $(shell $(PYTHON_CONFIG) --includes)
8-
CXXFLAGS += $(PYTHON_INCLUDE)
8+
EXTRA_FLAGS := $(PYTHON_INCLUDE)
99
LDFLAGS += $(shell $(PYTHON_CONFIG) --libs)
1010

1111
# Either finds numpy or set -DWITHOUT_NUMPY
12-
CXXFLAGS += $(shell $(PYTHON_BIN) $(CURDIR)/numpy_flags.py)
13-
WITHOUT_NUMPY := $(findstring $(CXXFLAGS), WITHOUT_NUMPY)
12+
EXTRA_FLAGS += $(shell $(PYTHON_BIN) $(CURDIR)/numpy_flags.py)
13+
WITHOUT_NUMPY := $(findstring $(EXTRA_FLAGS), WITHOUT_NUMPY)
1414

1515
# Examples requiring numpy support to compile
16-
EXAMPLES_NUMPY := surface
17-
EXAMPLES := minimal basic modern animation nonblock xkcd quiver bar fill_inbetween fill update subplot2grid \
18-
$(if WITHOUT_NUMPY,,$(EXAMPLES_NUMPY))
16+
EXAMPLES_NUMPY := surface colorbar
17+
EXAMPLES := minimal basic modern animation nonblock xkcd quiver bar \
18+
fill_inbetween fill update subplot2grid lines3d \
19+
$(if $(WITHOUT_NUMPY),,$(EXAMPLES_NUMPY))
1920

2021
# Prefix every example with 'examples/build/'
2122
EXAMPLE_TARGETS := $(patsubst %,examples/build/%,$(EXAMPLES))
@@ -24,10 +25,14 @@ EXAMPLE_TARGETS := $(patsubst %,examples/build/%,$(EXAMPLES))
2425

2526
examples: $(EXAMPLE_TARGETS)
2627

28+
docs:
29+
doxygen
30+
moxygen doc/xml --noindex -o doc/api.md
31+
2732
# Assume every *.cpp file is a separate example
28-
$(EXAMPLE_TARGETS): examples/build/%: examples/%.cpp
33+
$(EXAMPLE_TARGETS): examples/build/%: examples/%.cpp matplotlibcpp.h
2934
mkdir -p examples/build
30-
$(CXX) -o $@ $< $(CXXFLAGS) $(LDFLAGS)
35+
$(CXX) -o $@ $< $(EXTRA_FLAGS) $(CXXFLAGS) $(LDFLAGS)
3136

3237
clean:
3338
rm -f ${EXAMPLE_TARGETS}

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,21 @@ matplotlib-cpp.
211211

212212
If you prefer to use CMake as build system, you will want to add something like this to your
213213
CMakeLists.txt:
214+
215+
**Recommended way (since CMake 3.12):**
216+
217+
It's easy to use cmake official [docs](https://cmake.org/cmake/help/git-stage/module/FindPython2.html#module:FindPython2) to find Python 2(or 3) interpreter, compiler and development environment (include directories and libraries).
218+
219+
NumPy is optional here, delete it from cmake script, if you don't need it.
220+
221+
```cmake
222+
find_package(Python2 COMPONENTS Development NumPy)
223+
target_include_directories(myproject PRIVATE ${Python2_INCLUDE_DIRS} ${Python2_NumPy_INCLUDE_DIRS})
224+
target_link_libraries(myproject Python2::Python Python2::NumPy)
225+
```
226+
227+
**Alternative way (for CMake <= 3.11):**
228+
214229
```cmake
215230
find_package(PythonLibs 2.7)
216231
target_include_directories(myproject PRIVATE ${PYTHON_INCLUDE_DIRS})
@@ -273,3 +288,6 @@ Todo/Issues/Wishlist
273288

274289
* If you use Anaconda on Windows, you might need to set PYTHONHOME to Anaconda home directory and QT_QPA_PLATFORM_PLUGIN_PATH to %PYTHONHOME%Library/plugins/platforms. The latter is for especially when you get the error which says 'This application failed to start because it could not find or load the Qt platform plugin "windows"
275290
in "".'
291+
292+
* MacOS: `Unable to import matplotlib.pyplot`. Cause: In mac os image rendering back end of matplotlib (what-is-a-backend to render using the API of Cocoa by default). There is Qt4Agg and GTKAgg and as a back-end is not the default. Set the back end of macosx that is differ compare with other windows or linux os.
293+
Solution is discribed [here](https://stackoverflow.com/questions/21784641/installation-issue-with-matplotlib-python?noredirect=1&lq=1), additional information can be found there too(see links in answers).

examples/colorbar.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#define _USE_MATH_DEFINES
2+
#include <cmath>
3+
#include <iostream>
4+
#include "../matplotlibcpp.h"
5+
6+
using namespace std;
7+
namespace plt = matplotlibcpp;
8+
9+
int main()
10+
{
11+
// Prepare data
12+
int ncols = 500, nrows = 300;
13+
std::vector<float> z(ncols * nrows);
14+
for (int j=0; j<nrows; ++j) {
15+
for (int i=0; i<ncols; ++i) {
16+
z.at(ncols * j + i) = std::sin(std::hypot(i - ncols/2, j - nrows/2));
17+
}
18+
}
19+
20+
const float* zptr = &(z[0]);
21+
const int colors = 1;
22+
23+
plt::title("My matrix");
24+
PyObject* mat;
25+
plt::imshow(zptr, nrows, ncols, colors, {}, &mat);
26+
plt::colorbar(mat);
27+
28+
// Show plots
29+
plt::show();
30+
plt::close();
31+
Py_DECREF(mat);
32+
}

examples/lines3d.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include "../matplotlibcpp.h"
2+
3+
#include <cmath>
4+
5+
namespace plt = matplotlibcpp;
6+
7+
int main()
8+
{
9+
std::vector<double> x, y, z;
10+
double theta, r;
11+
double z_inc = 4.0/99.0; double theta_inc = (8.0 * M_PI)/99.0;
12+
13+
for (double i = 0; i < 100; i += 1) {
14+
theta = -4.0 * M_PI + theta_inc*i;
15+
z.push_back(-2.0 + z_inc*i);
16+
r = z[i]*z[i] + 1;
17+
x.push_back(r * sin(theta));
18+
y.push_back(r * cos(theta));
19+
}
20+
21+
std::map<std::string, std::string> keywords;
22+
keywords.insert(std::pair<std::string, std::string>("label", "parametric curve") );
23+
24+
plt::plot3(x, y, z, keywords);
25+
plt::xlabel("x label");
26+
plt::ylabel("y label");
27+
plt::set_zlabel("z label"); // set_zlabel rather than just zlabel, in accordance with the Axes3D method
28+
plt::legend();
29+
plt::show();
30+
}

examples/lines3d.png

74.7 KB
Loading

0 commit comments

Comments
 (0)
0