Skip to content
This repository was archived by the owner on Oct 28, 2023. It is now read-only.

Commit 231633f

Browse files
committed
add rc(group, kwargs**) support and update contrib/
1 parent cd4394d commit 231633f

File tree

5 files changed

+57
-14
lines changed

5 files changed

+57
-14
lines changed

contrib/CMakeLists.txt

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,32 @@
11
cmake_minimum_required(VERSION 3.7)
22
project (MatplotlibCPP_Test)
33

4-
set(CMAKE_CXX_STANDARD 11)
4+
set(CMAKE_CXX_STANDARD 17)
55
set(CMAKE_CXX_STANDARD_REQUIRED ON)
66

7-
include_directories(${PYTHONHOME}/include)
8-
include_directories(${PYTHONHOME}/Lib/site-packages/numpy/core/include)
9-
link_directories(${PYTHONHOME}/libs)
10-
11-
add_definitions(-DMATPLOTLIBCPP_PYTHON_HEADER=Python.h)
12-
13-
# message(STATUS "*** dump start cmake variables ***")
14-
# get_cmake_property(_variableNames VARIABLES)
15-
# foreach(_variableName ${_variableNames})
16-
# message(STATUS "${_variableName}=${${_variableName}}")
17-
# endforeach()
18-
# message(STATUS "*** dump end ***")
7+
find_package(Python3 COMPONENTS Development NumPy)
8+
include_directories(${Python3_INCLUDE_DIRS} ${Python3_NumPy_INCLUDE_DIRS})
199

2010
add_executable(minimal ${CMAKE_CURRENT_SOURCE_DIR}/../examples/minimal.cpp)
11+
target_link_libraries(minimal ${Python3_LIBRARIES})
12+
2113
add_executable(basic ${CMAKE_CURRENT_SOURCE_DIR}/../examples/basic.cpp)
14+
target_link_libraries(basic ${Python3_LIBRARIES})
15+
2216
add_executable(modern ${CMAKE_CURRENT_SOURCE_DIR}/../examples/modern.cpp)
17+
target_link_libraries(modern ${Python3_LIBRARIES})
18+
2319
add_executable(animation ${CMAKE_CURRENT_SOURCE_DIR}/../examples/animation.cpp)
20+
target_link_libraries(animation ${Python3_LIBRARIES})
21+
2422
add_executable(nonblock ${CMAKE_CURRENT_SOURCE_DIR}/../examples/nonblock.cpp)
23+
target_link_libraries(nonblock ${Python3_LIBRARIES})
24+
2525
add_executable(xkcd ${CMAKE_CURRENT_SOURCE_DIR}/../examples/xkcd.cpp)
26+
target_link_libraries(xkcd ${Python3_LIBRARIES})
27+
2628
add_executable(bar ${CMAKE_CURRENT_SOURCE_DIR}/../examples/bar.cpp)
29+
target_link_libraries(bar ${Python3_LIBRARIES})
30+
31+
add_executable(dip ${CMAKE_CURRENT_SOURCE_DIR}/../examples/dip.cpp)
32+
target_link_libraries(dip ${Python3_LIBRARIES})

examples/dip.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include "../matplotlibcpp.h"
2+
3+
namespace plt = matplotlibcpp;
4+
5+
int main()
6+
{
7+
plt::rc("figure", {{"dpi", "300"}, {"figsize", "8, 5"}});
8+
plt::plot({1,3,2,4});
9+
plt::save("dip_300_10_5.png");
10+
plt::show(); // must call a show(true), otherwise no work
11+
12+
plt::rc("figure", {{"dpi", "100"}, {"figsize", "4, 2"}});
13+
plt::plot({1,3,2,4,5});
14+
plt::save("dip_100_4_2.png");
15+
plt::show();
16+
}

examples/dip_100_4_2.png

8.38 KB
Loading

examples/dip_300_10_5.png

84.4 KB
Loading

matplotlibcpp.h

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ namespace matplotlibcpp
9292
PyObject *s_python_function_suptitle;
9393
PyObject *s_python_function_bar;
9494
PyObject *s_python_function_subplots_adjust;
95-
95+
PyObject *s_python_function_rc;
9696

9797
/* For now, _interpreter is implemented as a singleton since its currently not possible to have
9898
multiple independent embedded python interpreters without patching the python source code
@@ -235,6 +235,7 @@ namespace matplotlibcpp
235235
s_python_function_suptitle = safe_import(pymod, "suptitle");
236236
s_python_function_bar = safe_import(pymod, "bar");
237237
s_python_function_subplots_adjust = safe_import(pymod, "subplots_adjust");
238+
s_python_function_rc = safe_import(matplotlib, "rc");
238239
#ifndef WITHOUT_NUMPY
239240
s_python_function_imshow = safe_import(pymod, "imshow");
240241
#endif
@@ -1715,6 +1716,26 @@ namespace matplotlibcpp
17151716
return out;
17161717
}
17171718

1719+
void rc(const std::string& group, const std::vector<detail::Keyword> &keywords = {})
1720+
{
1721+
detail::_interpreter::get();
1722+
1723+
PyObject *group_name = PyString_FromString(group.c_str());
1724+
1725+
PyObject *args = PyTuple_New(1);
1726+
PyTuple_SetItem(args, 0, group_name);
1727+
1728+
PyObject* kwargs = detail::ConstructKeywordArgs(keywords);
1729+
PyObject *res = PyObject_Call(detail::_interpreter::get().s_python_function_rc, args, kwargs );
1730+
1731+
if (!res) throw std::runtime_error("Call to rc() failed.");
1732+
1733+
Py_DECREF(res);
1734+
// Py_DECREF(args); must comment this args, otherwise not work.
1735+
Py_DECREF(group_name);
1736+
Py_DECREF(kwargs);
1737+
}
1738+
17181739
// Actually, is there any reason not to call this automatically for every plot?
17191740
inline void tight_layout()
17201741
{

0 commit comments

Comments
 (0)