Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,6 @@

# Build
/examples/build/*

# vim temp files
*.sw*
25 changes: 15 additions & 10 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
# Use C++11
CXXFLAGS += -std=c++11
# Use C++11, dont warn on long-to-float conversion
CXXFLAGS += -std=c++11 -Wno-conversion

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

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

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

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

examples: $(EXAMPLE_TARGETS)

docs:
doxygen
moxygen doc/xml --noindex -o doc/api.md

# Assume every *.cpp file is a separate example
$(EXAMPLE_TARGETS): examples/build/%: examples/%.cpp
$(EXAMPLE_TARGETS): examples/build/%: examples/%.cpp matplotlibcpp.h
mkdir -p examples/build
$(CXX) -o $@ $< $(CXXFLAGS) $(LDFLAGS)
$(CXX) -o $@ $< $(EXTRA_FLAGS) $(CXXFLAGS) $(LDFLAGS)

clean:
rm -f ${EXAMPLE_TARGETS}
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,21 @@ matplotlib-cpp.

If you prefer to use CMake as build system, you will want to add something like this to your
CMakeLists.txt:

**Recommended way (since CMake 3.12):**

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).

NumPy is optional here, delete it from cmake script, if you don't need it.

```cmake
find_package(Python2 COMPONENTS Development NumPy)
target_include_directories(myproject PRIVATE ${Python2_INCLUDE_DIRS} ${Python2_NumPy_INCLUDE_DIRS})
target_link_libraries(myproject Python2::Python Python2::NumPy)
```

**Alternative way (for CMake <= 3.11):**

```cmake
find_package(PythonLibs 2.7)
target_include_directories(myproject PRIVATE ${PYTHON_INCLUDE_DIRS})
Expand Down Expand Up @@ -273,3 +288,6 @@ Todo/Issues/Wishlist

* 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"
in "".'

* 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.
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).
32 changes: 32 additions & 0 deletions examples/colorbar.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#define _USE_MATH_DEFINES
#include <cmath>
#include <iostream>
#include "../matplotlibcpp.h"

using namespace std;
namespace plt = matplotlibcpp;

int main()
{
// Prepare data
int ncols = 500, nrows = 300;
std::vector<float> z(ncols * nrows);
for (int j=0; j<nrows; ++j) {
for (int i=0; i<ncols; ++i) {
z.at(ncols * j + i) = std::sin(std::hypot(i - ncols/2, j - nrows/2));
}
}

const float* zptr = &(z[0]);
const int colors = 1;

plt::title("My matrix");
PyObject* mat;
plt::imshow(zptr, nrows, ncols, colors, {}, &mat);
plt::colorbar(mat);

// Show plots
plt::show();
plt::close();
Py_DECREF(mat);
}
30 changes: 30 additions & 0 deletions examples/lines3d.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include "../matplotlibcpp.h"

#include <cmath>

namespace plt = matplotlibcpp;

int main()
{
std::vector<double> x, y, z;
double theta, r;
double z_inc = 4.0/99.0; double theta_inc = (8.0 * M_PI)/99.0;

for (double i = 0; i < 100; i += 1) {
theta = -4.0 * M_PI + theta_inc*i;
z.push_back(-2.0 + z_inc*i);
r = z[i]*z[i] + 1;
x.push_back(r * sin(theta));
y.push_back(r * cos(theta));
}

std::map<std::string, std::string> keywords;
keywords.insert(std::pair<std::string, std::string>("label", "parametric curve") );

plt::plot3(x, y, z, keywords);
plt::xlabel("x label");
plt::ylabel("y label");
plt::set_zlabel("z label"); // set_zlabel rather than just zlabel, in accordance with the Axes3D method
plt::legend();
plt::show();
}
Binary file added examples/lines3d.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading