Skip to content

Commit 5ab2050

Browse files
authored
Merge pull request #1 from lava/master
Update and sync with lava main master
2 parents 1d23b28 + 0e39f8f commit 5ab2050

File tree

16 files changed

+760
-61
lines changed

16 files changed

+760
-61
lines changed

Makefile

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
examples: minimal basic modern animation nonblock xkcd
1+
examples: minimal basic modern animation nonblock xkcd quiver bar surface
22

33
minimal: examples/minimal.cpp matplotlibcpp.h
44
cd examples && g++ -DWITHOUT_NUMPY minimal.cpp -I/usr/include/python2.7 -lpython2.7 -o minimal -std=c++11
55

66
basic: examples/basic.cpp matplotlibcpp.h
7-
cd examples && g++ basic.cpp -I/usr/include/python2.7 -lpython2.7 -o basic
7+
cd examples && g++ basic.cpp -I/usr/include/python2.7 -lpython2.7 -o basic -std=c++11
88

99
modern: examples/modern.cpp matplotlibcpp.h
1010
cd examples && g++ modern.cpp -I/usr/include/python2.7 -lpython2.7 -o modern -std=c++11
@@ -15,8 +15,17 @@ animation: examples/animation.cpp matplotlibcpp.h
1515
nonblock: examples/nonblock.cpp matplotlibcpp.h
1616
cd examples && g++ nonblock.cpp -I/usr/include/python2.7 -lpython2.7 -o nonblock -std=c++11
1717

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
20+
1821
xkcd: examples/xkcd.cpp matplotlibcpp.h
1922
cd examples && g++ xkcd.cpp -I/usr/include/python2.7 -lpython2.7 -o xkcd -std=c++11
2023

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+
2130
clean:
22-
rm -f examples/{minimal,basic,modern,animation,nonblock,xkcd}
31+
rm -f examples/{minimal,basic,modern,animation,nonblock,xkcd,quiver,bar}

README.md

Lines changed: 81 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,18 @@ int main()
4141
z.at(i) = log(i);
4242
}
4343
44+
// Set the size of output image to 1200x780 pixels
45+
plt::figure_size(1200, 780);
4446
// Plot line from given x and y data. Color is selected automatically.
4547
plt::plot(x, y);
4648
// Plot a red dashed line from given x and y data.
4749
plt::plot(x, w,"r--");
4850
// Plot a line whose name will show up as "log(x)" in the legend.
4951
plt::named_plot("log(x)", x, z);
50-
5152
// Set x-axis to interval [0,1000000]
5253
plt::xlim(0, 1000*1000);
54+
// Add graph title
55+
plt::title("Sample figure");
5356
// Enable legend.
5457
plt::legend();
5558
// Save the image (file format is determined by the extension)
@@ -58,9 +61,11 @@ int main()
5861
```
5962
g++ basic.cpp -I/usr/include/python2.7 -lpython2.7
6063

61-
Result: ![Basic example](./examples/basic.png)
64+
**Result:**
65+
66+
![Basic example](./examples/basic.png)
6267

63-
matplotlib-cpp doesn't require C++11, but will enable some additional syntactic sugar when available:
68+
Alternatively, matplotlib-cpp also supports some C++11-powered syntactic sugar:
6469
```cpp
6570
#include <cmath>
6671
#include "matplotlibcpp.h"
@@ -91,7 +96,9 @@ int main()
9196
```
9297
g++ modern.cpp -std=c++11 -I/usr/include/python2.7 -lpython
9398
94-
Result: ![Modern example](./examples/modern.png)
99+
**Result:**
100+
101+
![Modern example](./examples/modern.png)
95102
96103
Or some *funny-looking xkcd-styled* example:
97104
```cpp
@@ -121,7 +128,66 @@ int main() {
121128

122129
**Result:**
123130

124-
![Minimal example](./examples/xkcd.png)
131+
![xkcd example](./examples/xkcd.png)
132+
133+
When working with vector fields, you might be interested in quiver plots:
134+
```cpp
135+
#include "../matplotlibcpp.h"
136+
137+
namespace plt = matplotlibcpp;
138+
139+
int main()
140+
{
141+
// u and v are respectively the x and y components of the arrows we're plotting
142+
std::vector<int> x, y, u, v;
143+
for (int i = -5; i <= 5; i++) {
144+
for (int j = -5; j <= 5; j++) {
145+
x.push_back(i);
146+
u.push_back(-i);
147+
y.push_back(j);
148+
v.push_back(-j);
149+
}
150+
}
151+
152+
plt::quiver(x, y, u, v);
153+
plt::show();
154+
}
155+
```
156+
g++ quiver.cpp -std=c++11 -I/usr/include/python2.7 -lpython2.7
157+
158+
**Result:**
159+
160+
![quiver example](./examples/quiver.png)
161+
162+
When working with 3d functions, you might be interested in 3d plots:
163+
```cpp
164+
#include "../matplotlibcpp.h"
165+
166+
namespace plt = matplotlibcpp;
167+
168+
int main()
169+
{
170+
std::vector<std::vector<double>> x, y, z;
171+
for (double i = -5; i <= 5; i += 0.25) {
172+
std::vector<double> x_row, y_row, z_row;
173+
for (double j = -5; j <= 5; j += 0.25) {
174+
x_row.push_back(i);
175+
y_row.push_back(j);
176+
z_row.push_back(::std::sin(::std::hypot(i, j)));
177+
}
178+
x.push_back(x_row);
179+
y.push_back(y_row);
180+
z.push_back(z_row);
181+
}
182+
183+
plt::plot_surface(x, y, z);
184+
plt::show();
185+
}
186+
```
187+
188+
**Result:**
189+
190+
![surface example](./examples/surface.png)
125191

126192
Installation
127193
------------
@@ -150,6 +216,16 @@ find_package(PythonLibs 2.7)
150216
target_include_directories(myproject PRIVATE ${PYTHON_INCLUDE_DIRS})
151217
target_link_libraries(myproject ${PYTHON_LIBRARIES})
152218
```
219+
220+
# C++11
221+
222+
Currently, c++11 is required to build matplotlib-cpp. The last working commit that did
223+
not have this requirement was `717e98e752260245407c5329846f5d62605eff08`.
224+
225+
Note that support for c++98 was dropped more or less accidentally, so if you have to work
226+
with an ancient compiler and still want to enjoy the latest additional features, I'd
227+
probably merge a PR that restores support.
228+
153229
# Python 3
154230

155231
This library supports both python2 and python3 (although the python3 support is probably far less tested,

contrib/CMakeLists.txt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,26 @@
1-
cmake_minimum_required(VERSION 3.1)
1+
cmake_minimum_required(VERSION 3.7)
22
project (MatplotlibCPP_Test)
33

44
set(CMAKE_CXX_STANDARD 11)
55
set(CMAKE_CXX_STANDARD_REQUIRED ON)
66

77
include_directories(${PYTHONHOME}/include)
8+
include_directories(${PYTHONHOME}/Lib/site-packages/numpy/core/include)
89
link_directories(${PYTHONHOME}/libs)
910

1011
add_definitions(-DMATPLOTLIBCPP_PYTHON_HEADER=Python.h)
1112

1213
# message(STATUS "*** dump start cmake variables ***")
1314
# get_cmake_property(_variableNames VARIABLES)
1415
# foreach(_variableName ${_variableNames})
15-
# message(STATUS "${_variableName}=${${_variableName}}")
16+
# message(STATUS "${_variableName}=${${_variableName}}")
1617
# endforeach()
1718
# message(STATUS "*** dump end ***")
1819

1920
add_executable(minimal ${CMAKE_CURRENT_SOURCE_DIR}/../examples/minimal.cpp)
2021
add_executable(basic ${CMAKE_CURRENT_SOURCE_DIR}/../examples/basic.cpp)
2122
add_executable(modern ${CMAKE_CURRENT_SOURCE_DIR}/../examples/modern.cpp)
23+
add_executable(animation ${CMAKE_CURRENT_SOURCE_DIR}/../examples/animation.cpp)
24+
add_executable(nonblock ${CMAKE_CURRENT_SOURCE_DIR}/../examples/nonblock.cpp)
25+
add_executable(xkcd ${CMAKE_CURRENT_SOURCE_DIR}/../examples/xkcd.cpp)
26+
add_executable(bar ${CMAKE_CURRENT_SOURCE_DIR}/../examples/bar.cpp)

contrib/README.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,25 @@ contributors are not required to and may be unable to check whether their
88
changes break any of them.
99

1010
## Windows support
11+
Tested on the following environment
12+
* Windows 10 - 64bit
13+
* Anaconda 4.3 (64 bit)
14+
* Python 3.6.0
15+
* CMake 3.9.4
16+
* Visual Studio 2017, 2015, 2013
1117

1218
### Configuring and Building Samples
19+
1. Edit WinBuild.cmd for your environment(Line:5-7)
20+
if NOT DEFINED MSVC_VERSION set MSVC_VERSION=[Your Visual Studio Version(12, 14, 15)]
21+
if NOT DEFINED CMAKE_CONFIG set CMAKE_CONFIG=Release
22+
if NOT DEFINED PYTHONHOME set PYTHONHOME=[Your Python Path]
1323

24+
2. Run WinBuild.cmd to build
1425
```cmd
1526
> cd contrib
1627
> WinBuild.cmd
1728
```
18-
1929
The `WinBuild.cmd` will set up temporal ENV variables and build binaries in (matplotlib root)/examples with the Release configuration.
30+
31+
3. Find exe files in examples/build/Release
32+
Note: platforms folder is necessary to make qt works.

contrib/WinBuild.cmd

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
@echo off
22
@setlocal EnableDelayedExpansion
33

4-
if NOT DEFINED MSVC_VERSION set MSVC_VERSION=14
4+
REM ------Set Your Environment-------------------------------
5+
if NOT DEFINED MSVC_VERSION set MSVC_VERSION=15
56
if NOT DEFINED CMAKE_CONFIG set CMAKE_CONFIG=Release
67
if NOT DEFINED PYTHONHOME set PYTHONHOME=C:/Users/%username%/Anaconda3
8+
REM ---------------------------------------------------------
9+
10+
set KEY_NAME="HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\VisualStudio\SxS\VS7"
11+
set VALUE_NAME=15.0
712

813
if "%MSVC_VERSION%"=="14" (
914
if "%processor_architecture%" == "AMD64" (
@@ -14,13 +19,23 @@ if "%MSVC_VERSION%"=="14" (
1419
) else if "%MSVC_VERSION%"=="12" (
1520
if "%processor_architecture%" == "AMD64" (
1621
set CMAKE_GENERATOR=Visual Studio 12 2013 Win64
17-
1822
) else (
1923
set CMAKE_GENERATOR=Visual Studio 12 2013
2024
)
25+
) else if "%MSVC_VERSION%"=="15" (
26+
if "%processor_architecture%" == "AMD64" (
27+
set CMAKE_GENERATOR=Visual Studio 15 2017 Win64
28+
) else (
29+
set CMAKE_GENERATOR=Visual Studio 15 2017
30+
)
31+
)
32+
if "%MSVC_VERSION%"=="15" (
33+
for /F "usebackq tokens=1,2,*" %%A in (`REG QUERY %KEY_NAME% /v %VALUE_NAME%`) do (
34+
set batch_file=%%CVC\Auxiliary\Build\vcvarsall.bat
35+
)
36+
) else (
37+
set batch_file=!VS%MSVC_VERSION%0COMNTOOLS!..\..\VC\vcvarsall.bat
2138
)
22-
23-
set batch_file=!VS%MSVC_VERSION%0COMNTOOLS!..\..\VC\vcvarsall.bat
2439
call "%batch_file%" %processor_architecture%
2540

2641
pushd ..

examples/bar.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#define _USE_MATH_DEFINES
2+
3+
#include <iostream>
4+
#include <string>
5+
#include "../matplotlibcpp.h"
6+
namespace plt = matplotlibcpp;
7+
8+
int main(int argc, char **argv) {
9+
std::vector<int> test_data;
10+
for (int i = 0; i < 20; i++) {
11+
test_data.push_back(i);
12+
}
13+
14+
plt::bar(test_data);
15+
plt::show();
16+
17+
return (0);
18+
}

examples/bar.png

11.7 KB
Loading

examples/basic.cpp

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,38 @@ namespace plt = matplotlibcpp;
77

88
int main()
99
{
10-
// Prepare data.
11-
int n = 5000;
12-
std::vector<double> x(n), y(n), z(n), w(n,2);
13-
for(int i=0; i<n; ++i) {
14-
x.at(i) = i*i;
15-
y.at(i) = sin(2*M_PI*i/360.0);
16-
z.at(i) = log(i);
17-
}
18-
19-
// Plot line from given x and y data. Color is selected automatically.
20-
plt::plot(x, y);
21-
// Plot a red dashed line from given x and y data.
22-
plt::plot(x, w,"r--");
23-
// Plot a line whose name will show up as "log(x)" in the legend.
24-
plt::named_plot("log(x)", x, z);
25-
26-
// Set x-axis to interval [0,1000000]
27-
plt::xlim(0, 1000*1000);
28-
29-
// Add graph title
30-
plt::title("Sample figure");
31-
// Enable legend.
32-
plt::legend();
33-
// save figure
34-
const char* filename = "./basic.png";
35-
std::cout << "Saving result to " << filename << std::endl;;
36-
plt::save(filename);
10+
// Prepare data.
11+
int n = 5000;
12+
std::vector<double> x(n), y(n), z(n), w(n,2);
13+
for(int i=0; i<n; ++i) {
14+
x.at(i) = i*i;
15+
y.at(i) = sin(2*M_PI*i/360.0);
16+
z.at(i) = log(i);
17+
}
18+
19+
// Set the size of output image = 1200x780 pixels
20+
plt::figure_size(1200, 780);
21+
22+
// Plot line from given x and y data. Color is selected automatically.
23+
plt::plot(x, y);
24+
25+
// Plot a red dashed line from given x and y data.
26+
plt::plot(x, w,"r--");
27+
28+
// Plot a line whose name will show up as "log(x)" in the legend.
29+
plt::named_plot("log(x)", x, z);
30+
31+
// Set x-axis to interval [0,1000000]
32+
plt::xlim(0, 1000*1000);
33+
34+
// Add graph title
35+
plt::title("Sample figure");
36+
37+
// Enable legend.
38+
plt::legend();
39+
40+
// save figure
41+
const char* filename = "./basic.png";
42+
std::cout << "Saving result to " << filename << std::endl;;
43+
plt::save(filename);
3744
}

examples/basic.png

13.2 KB
Loading

examples/quiver.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include "../matplotlibcpp.h"
2+
3+
namespace plt = matplotlibcpp;
4+
5+
int main()
6+
{
7+
// u and v are respectively the x and y components of the arrows we're plotting
8+
std::vector<int> x, y, u, v;
9+
for (int i = -5; i <= 5; i++) {
10+
for (int j = -5; j <= 5; j++) {
11+
x.push_back(i);
12+
u.push_back(-i);
13+
y.push_back(j);
14+
v.push_back(-j);
15+
}
16+
}
17+
18+
plt::quiver(x, y, u, v);
19+
plt::show();
20+
}

0 commit comments

Comments
 (0)