Skip to content

Commit 2d9e537

Browse files
committed
add colorbar
1 parent fda674d commit 2d9e537

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

examples/imshow.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ void waves(const unsigned n) {
1212
}
1313
}
1414
plt::figure();
15-
plt::imshow(X);
15+
plt::imshow(X, {{"cmap", "Spectral"}});
16+
plt::colorbar();
1617
plt::show();
1718
}
1819

matplotlibcpp.h

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ struct _interpreter {
9494
PyObject *s_python_function_bar;
9595
PyObject *s_python_function_subplots_adjust;
9696
PyObject *s_python_function_imshow;
97+
PyObject *s_python_function_colorbar;
9798

9899
/* For now, _interpreter is implemented as a singleton since its currently not
99100
possible to have multiple independent embedded python interpreters without
@@ -229,12 +230,13 @@ struct _interpreter {
229230
s_python_function_subplots_adjust =
230231
PyObject_GetAttrString(pymod, "subplots_adjust");
231232
s_python_function_imshow = PyObject_GetAttrString(pymod, "imshow");
233+
s_python_function_colorbar = PyObject_GetAttrString(pymod, "colorbar");
232234

233235
if (!s_python_function_show || !s_python_function_close ||
234236
!s_python_function_draw || !s_python_function_pause ||
235237
!s_python_function_figure || !s_python_function_fignum_exists ||
236238
!s_python_function_plot || !s_python_function_quiver ||
237-
!s_python_function_contour ||
239+
!s_python_function_contour || !s_python_function_colorbar ||
238240
!s_python_function_semilogx || !s_python_function_semilogy ||
239241
!s_python_function_loglog || !s_python_function_fill ||
240242
!s_python_function_fill_between || !s_python_function_subplot ||
@@ -296,7 +298,8 @@ struct _interpreter {
296298
!PyFunction_Check(s_python_function_suptitle) ||
297299
!PyFunction_Check(s_python_function_bar) ||
298300
!PyFunction_Check(s_python_function_subplots_adjust) ||
299-
!PyFunction_Check(s_python_function_imshow)
301+
!PyFunction_Check(s_python_function_imshow) ||
302+
!PyFunction_Check(s_python_function_colorbar)
300303
) {
301304
throw std::runtime_error(
302305
"Python object is unexpectedly not a PyFunction.");
@@ -686,6 +689,15 @@ void imshow(const Matrix& X, const std::map<std::string, std::string> &keywords
686689
Py_DECREF(res);
687690
}
688691

692+
// @brief Add the colorbar
693+
void colorbar() {
694+
PyObject *res =
695+
PyObject_CallObject(detail::_interpreter::get().s_python_function_colorbar,
696+
detail::_interpreter::get().s_python_empty_tuple);
697+
if (!res)
698+
throw std::runtime_error("Call to colorbar() failed.");
699+
}
700+
689701
// @brief plot_surface for datapoints (x_ij, y_ij, z_ij) with i,j = 0..n
690702
// @param x The x values of the datapoints in a matrix
691703
// @param y The y values of the datapoints in a matrix

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