Skip to content

Commit bde7f22

Browse files
authored
Merge pull request #1 from lava/master
update
2 parents 94c0215 + 60dcd64 commit bde7f22

File tree

14 files changed

+1004
-240
lines changed

14 files changed

+1004
-240
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*

.travis.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
language: minimal
2+
dist: trusty
3+
services:
4+
- docker
5+
script:
6+
- make -C contrib docker_build

Makefile

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,41 @@
1-
examples: minimal basic modern animation nonblock xkcd quiver bar surface fill_inbetween fill update
1+
# Use C++11, dont warn on long-to-float conversion
2+
CXXFLAGS += -std=c++11 -Wno-conversion
23

3-
minimal: examples/minimal.cpp matplotlibcpp.h
4-
cd examples && g++ -DWITHOUT_NUMPY minimal.cpp -I/usr/include/python2.7 -lpython2.7 -o minimal -std=c++11
4+
# Default to using system's default version of python
5+
PYTHON_BIN ?= python3
6+
PYTHON_CONFIG := $(PYTHON_BIN)-config
7+
PYTHON_INCLUDE ?= $(shell $(PYTHON_CONFIG) --includes)
8+
EXTRA_FLAGS := $(PYTHON_INCLUDE)
9+
# NOTE: Since python3.8, the correct invocation is `python3-config --libs --embed`.
10+
# So of course the proper way to get python libs for embedding now is to
11+
# invoke that, check if it crashes, and fall back to just `--libs` if it does.
12+
LDFLAGS += $(shell if $(PYTHON_CONFIG) --libs --embed >/dev/null; then $(PYTHON_CONFIG) --libs --embed; else $(PYTHON_CONFIG) --libs; fi)
513

6-
basic: examples/basic.cpp matplotlibcpp.h
7-
cd examples && g++ basic.cpp -I/usr/include/python2.7 -lpython2.7 -o basic -std=c++11
14+
# Either finds numpy or set -DWITHOUT_NUMPY
15+
EXTRA_FLAGS += $(shell $(PYTHON_BIN) $(CURDIR)/numpy_flags.py)
16+
WITHOUT_NUMPY := $(findstring $(EXTRA_FLAGS), WITHOUT_NUMPY)
817

9-
modern: examples/modern.cpp matplotlibcpp.h
10-
cd examples && g++ modern.cpp -I/usr/include/python2.7 -lpython2.7 -o modern -std=c++11
18+
# Examples requiring numpy support to compile
19+
EXAMPLES_NUMPY := surface colorbar
20+
EXAMPLES := minimal basic modern animation nonblock xkcd quiver bar \
21+
fill_inbetween fill update subplot2grid lines3d \
22+
$(if $(WITHOUT_NUMPY),,$(EXAMPLES_NUMPY))
1123

12-
animation: examples/animation.cpp matplotlibcpp.h
13-
cd examples && g++ animation.cpp -I/usr/include/python2.7 -lpython2.7 -o animation -std=c++11
24+
# Prefix every example with 'examples/build/'
25+
EXAMPLE_TARGETS := $(patsubst %,examples/build/%,$(EXAMPLES))
1426

15-
nonblock: examples/nonblock.cpp matplotlibcpp.h
16-
cd examples && g++ nonblock.cpp -I/usr/include/python2.7 -lpython2.7 -o nonblock -std=c++11
27+
.PHONY: examples
1728

18-
quiver: examples/quiver.cpp matplotlibcpp.h
19-
cd examples && g++ quiver.cpp -I/usr/include/python2.7 -lpython2.7 -o quiver -std=c++11
29+
examples: $(EXAMPLE_TARGETS)
2030

21-
xkcd: examples/xkcd.cpp matplotlibcpp.h
22-
cd examples && g++ xkcd.cpp -I/usr/include/python2.7 -lpython2.7 -o xkcd -std=c++11
31+
docs:
32+
doxygen
33+
moxygen doc/xml --noindex -o doc/api.md
2334

24-
bar: examples/bar.cpp matplotlibcpp.h
25-
cd examples && g++ bar.cpp -I/usr/include/python2.7 -lpython2.7 -o bar -std=c++11
26-
27-
surface: examples/surface.cpp matplotlibcpp.h
28-
cd examples && g++ surface.cpp -I/usr/include/python2.7 -lpython2.7 -o surface -std=c++11
29-
30-
fill_inbetween: examples/fill_inbetween.cpp matplotlibcpp.h
31-
cd examples && g++ fill_inbetween.cpp -I/usr/include/python2.7 -lpython2.7 -o fill_inbetween -std=c++11
32-
33-
fill: examples/fill.cpp matplotlibcpp.h
34-
cd examples && g++ fill.cpp -I/usr/include/python2.7 -lpython2.7 -o fill -std=c++11
35-
36-
update: examples/update.cpp matplotlibcpp.h
37-
cd examples && g++ update.cpp -I/usr/include/python2.7 -lpython2.7 -o update -std=c++11
35+
# Assume every *.cpp file is a separate example
36+
$(EXAMPLE_TARGETS): examples/build/%: examples/%.cpp matplotlibcpp.h
37+
mkdir -p examples/build
38+
$(CXX) -o $@ $< $(EXTRA_FLAGS) $(CXXFLAGS) $(LDFLAGS)
3839

3940
clean:
40-
rm -f examples/{minimal,basic,modern,animation,nonblock,xkcd,quiver,bar,surface,fill_inbetween,fill,update}
41+
rm -f ${EXAMPLE_TARGETS}

README.md

Lines changed: 47 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ A more comprehensive example:
3030
3131
namespace plt = matplotlibcpp;
3232
33-
int main()
33+
int main()
3434
{
3535
// Prepare data.
3636
int n = 5000;
@@ -73,26 +73,26 @@ Alternatively, matplotlib-cpp also supports some C++11-powered syntactic sugar:
7373
using namespace std;
7474
namespace plt = matplotlibcpp;
7575

76-
int main()
77-
{
76+
int main()
77+
{
7878
// Prepare data.
7979
int n = 5000; // number of data points
80-
vector<double> x(n),y(n);
80+
vector<double> x(n),y(n);
8181
for(int i=0; i<n; ++i) {
8282
double t = 2*M_PI*i/n;
8383
x.at(i) = 16*sin(t)*sin(t)*sin(t);
8484
y.at(i) = 13*cos(t) - 5*cos(2*t) - 2*cos(3*t) - cos(4*t);
8585
}
8686

87-
// plot() takes an arbitrary number of (x,y,format)-triples.
87+
// plot() takes an arbitrary number of (x,y,format)-triples.
8888
// x must be iterable (that is, anything providing begin(x) and end(x)),
89-
// y must either be callable (providing operator() const) or iterable.
89+
// y must either be callable (providing operator() const) or iterable.
9090
plt::plot(x, y, "r-", x, [](double d) { return 12.5+abs(sin(d)); }, "k-");
9191

9292

9393
// show plots
9494
plt::show();
95-
}
95+
}
9696
```
9797
g++ modern.cpp -std=c++11 -I/usr/include/python2.7 -lpython
9898
@@ -199,24 +199,56 @@ On Ubuntu:
199199
sudo apt-get install python-matplotlib python-numpy python2.7-dev
200200

201201
If, for some reason, you're unable to get a working installation of numpy on your system,
202-
you can add the define `WITHOUT_NUMPY` to erase this dependency.
202+
you can define the macro `WITHOUT_NUMPY` before including the header file to erase this
203+
dependency.
203204

204205
The C++-part of the library consists of the single header file `matplotlibcpp.h` which can be placed
205206
anywhere.
206207

207-
Since a python interpreter is opened internally, it is necessary to link against `libpython2.7` in order to use
208-
matplotlib-cpp.
208+
Since a python interpreter is opened internally, it is necessary to link against `libpython` in order
209+
to user matplotlib-cpp. Most versions should work, although `libpython2.7` and `libpython3.6` are
210+
probably the most regularly testedr.
211+
209212

210213
# CMake
211214

212215
If you prefer to use CMake as build system, you will want to add something like this to your
213216
CMakeLists.txt:
217+
218+
**Recommended way (since CMake 3.12):**
219+
220+
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).
221+
222+
NumPy is optional here, delete it from cmake script, if you don't need it.
223+
224+
```cmake
225+
find_package(Python2 COMPONENTS Development NumPy)
226+
target_include_directories(myproject PRIVATE ${Python2_INCLUDE_DIRS} ${Python2_NumPy_INCLUDE_DIRS})
227+
target_link_libraries(myproject Python2::Python Python2::NumPy)
228+
```
229+
230+
**Alternative way (for CMake <= 3.11):**
231+
214232
```cmake
215233
find_package(PythonLibs 2.7)
216234
target_include_directories(myproject PRIVATE ${PYTHON_INCLUDE_DIRS})
217235
target_link_libraries(myproject ${PYTHON_LIBRARIES})
218236
```
219237

238+
239+
# Vcpkg
240+
241+
You can download and install matplotlib-cpp using the [vcpkg](https://github.com/Microsoft/vcpkg) dependency manager:
242+
243+
git clone https://github.com/Microsoft/vcpkg.git
244+
cd vcpkg
245+
./bootstrap-vcpkg.sh
246+
./vcpkg integrate install
247+
vcpkg install matplotlib-cpp
248+
249+
The matplotlib-cpp port in vcpkg is kept up to date by Microsoft team members and community contributors. If the version is out of date, please [create an issue or pull request](https://github.com/Microsoft/vcpkg) on the vcpkg repository.
250+
251+
220252
# C++11
221253

222254
Currently, c++11 is required to build matplotlib-cpp. The last working commit that did
@@ -241,10 +273,10 @@ The same technique can be used for linking against a custom build of python
241273

242274
Why?
243275
----
244-
I initially started this library during my diploma thesis. The usual approach of
276+
I initially started this library during my diploma thesis. The usual approach of
245277
writing data from the c++ algorithm to a file and afterwards parsing and plotting
246278
it in python using matplotlib proved insufficient: Keeping the algorithm
247-
and plotting code in sync requires a lot of effort when the C++ code frequently and substantially
279+
and plotting code in sync requires a lot of effort when the C++ code frequently and substantially
248280
changes. Additionally, the python yaml parser was not able to cope with files that
249281
exceed a few hundred megabytes in size.
250282

@@ -273,3 +305,6 @@ Todo/Issues/Wishlist
273305

274306
* 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"
275307
in "".'
308+
309+
* 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.
310+
Solution is described [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).

contrib/Dockerfile

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
FROM debian:10 AS builder
2+
RUN apt-get update \
3+
&& apt-get install --yes --no-install-recommends \
4+
g++ \
5+
libpython3-dev \
6+
make \
7+
python3 \
8+
python3-dev \
9+
python3-numpy
10+
11+
ADD Makefile matplotlibcpp.h numpy_flags.py /opt/
12+
ADD examples/*.cpp /opt/examples/
13+
RUN cd /opt \
14+
&& make PYTHON_BIN=python3 \
15+
&& ls examples/build
16+
17+
FROM debian:10
18+
RUN apt-get update \
19+
&& apt-get install --yes --no-install-recommends \
20+
libpython3-dev \
21+
python3-matplotlib \
22+
python3-numpy
23+
24+
COPY --from=builder /opt/examples/build /opt/
25+
RUN cd /opt \
26+
&& ls \
27+
&& ./basic

contrib/Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
all: docker_build
2+
3+
docker_build:
4+
cd .. && \
5+
docker build . -f contrib/Dockerfile -t matplotlibcpp && \
6+
cd contrib

examples/.gitignore

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
animation
2+
bar
3+
basic
4+
fill
5+
fill_inbetween
6+
imshow
7+
minimal
8+
modern
9+
nonblock
10+
quiver
11+
subplot
12+
surface
13+
update
14+
xkcd

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/imshow.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
plt::imshow(zptr, nrows, ncols, colors);
25+
26+
// Show plots
27+
plt::save("imshow.png");
28+
std::cout << "Result saved to 'imshow.png'.\n";
29+
}

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+
}

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