Skip to content

Commit f1af639

Browse files
tkphdlava
authored andcommitted
implement colorbar; requires returned PyObject of mappable object
catch call without mappable object stray { add keywords (shrink is particularly useful)
1 parent f23347f commit f1af639

File tree

3 files changed

+67
-9
lines changed

3 files changed

+67
-9
lines changed

Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ WITHOUT_NUMPY := $(findstring $(CXXFLAGS), WITHOUT_NUMPY)
1414

1515
# Examples requiring numpy support to compile
1616
EXAMPLES_NUMPY := surface
17-
EXAMPLES := minimal basic modern animation nonblock xkcd quiver bar fill_inbetween fill update subplot2grid \
17+
EXAMPLES := minimal basic modern animation nonblock xkcd quiver bar \
18+
fill_inbetween fill update subplot2grid colorbar \
1819
$(if WITHOUT_NUMPY,,$(EXAMPLES_NUMPY))
1920

2021
# Prefix every example with 'examples/build/'

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

matplotlibcpp.h

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ struct _interpreter {
8787
PyObject *s_python_function_text;
8888
PyObject *s_python_function_suptitle;
8989
PyObject *s_python_function_bar;
90+
PyObject *s_python_function_colorbar;
9091
PyObject *s_python_function_subplots_adjust;
9192

9293

@@ -205,7 +206,7 @@ struct _interpreter {
205206
s_python_function_ylabel = safe_import(pymod, "ylabel");
206207
s_python_function_xticks = safe_import(pymod, "xticks");
207208
s_python_function_yticks = safe_import(pymod, "yticks");
208-
s_python_function_tick_params = safe_import(pymod, "tick_params");
209+
s_python_function_tick_params = safe_import(pymod, "tick_params");
209210
s_python_function_grid = safe_import(pymod, "grid");
210211
s_python_function_xlim = safe_import(pymod, "xlim");
211212
s_python_function_ion = safe_import(pymod, "ion");
@@ -220,11 +221,11 @@ struct _interpreter {
220221
s_python_function_text = safe_import(pymod, "text");
221222
s_python_function_suptitle = safe_import(pymod, "suptitle");
222223
s_python_function_bar = safe_import(pymod,"bar");
224+
s_python_function_colorbar = PyObject_GetAttrString(pymod, "colorbar");
223225
s_python_function_subplots_adjust = safe_import(pymod,"subplots_adjust");
224226
#ifndef WITHOUT_NUMPY
225227
s_python_function_imshow = safe_import(pymod, "imshow");
226228
#endif
227-
228229
s_python_empty_tuple = PyTuple_New(0);
229230
}
230231

@@ -589,7 +590,7 @@ bool hist(const std::vector<Numeric>& y, long bins=10,std::string color="b",
589590

590591
#ifndef WITHOUT_NUMPY
591592
namespace internal {
592-
inline void imshow(void *ptr, const NPY_TYPES type, const int rows, const int columns, const int colors, const std::map<std::string, std::string> &keywords)
593+
inline void imshow(void *ptr, const NPY_TYPES type, const int rows, const int columns, const int colors, const std::map<std::string, std::string> &keywords, PyObject** out)
593594
{
594595
assert(type == NPY_UINT8 || type == NPY_FLOAT);
595596
assert(colors == 1 || colors == 3 || colors == 4);
@@ -613,18 +614,21 @@ bool hist(const std::vector<Numeric>& y, long bins=10,std::string color="b",
613614
Py_DECREF(kwargs);
614615
if (!res)
615616
throw std::runtime_error("Call to imshow() failed");
616-
Py_DECREF(res);
617+
if (out)
618+
*out = res;
619+
else
620+
Py_DECREF(res);
617621
}
618622
}
619623

620-
inline void imshow(const unsigned char *ptr, const int rows, const int columns, const int colors, const std::map<std::string, std::string> &keywords = {})
624+
inline void imshow(const unsigned char *ptr, const int rows, const int columns, const int colors, const std::map<std::string, std::string> &keywords = {}, PyObject** out = nullptr)
621625
{
622-
internal::imshow((void *) ptr, NPY_UINT8, rows, columns, colors, keywords);
626+
internal::imshow((void *) ptr, NPY_UINT8, rows, columns, colors, keywords, out);
623627
}
624628

625-
inline void imshow(const float *ptr, const int rows, const int columns, const int colors, const std::map<std::string, std::string> &keywords = {})
629+
inline void imshow(const float *ptr, const int rows, const int columns, const int colors, const std::map<std::string, std::string> &keywords = {}, PyObject** out = nullptr)
626630
{
627-
internal::imshow((void *) ptr, NPY_FLOAT, rows, columns, colors, keywords);
631+
internal::imshow((void *) ptr, NPY_FLOAT, rows, columns, colors, keywords, out);
628632
}
629633

630634
#ifdef WITH_OPENCV
@@ -1136,6 +1140,27 @@ void text(Numeric x, Numeric y, const std::string& s = "")
11361140
Py_DECREF(res);
11371141
}
11381142

1143+
void colorbar(PyObject* mappable = NULL, const std::map<std::string, float>& keywords = {})
1144+
{
1145+
if (mappable == NULL)
1146+
throw std::runtime_error("Must call colorbar with PyObject* returned from an image, contour, surface, etc.");
1147+
PyObject* args = PyTuple_New(1);
1148+
PyTuple_SetItem(args, 0, mappable);
1149+
1150+
PyObject* kwargs = PyDict_New();
1151+
for(std::map<std::string, float>::const_iterator it = keywords.begin(); it != keywords.end(); ++it)
1152+
{
1153+
PyDict_SetItemString(kwargs, it->first.c_str(), PyFloat_FromDouble(it->second));
1154+
}
1155+
1156+
PyObject* res = PyObject_Call(detail::_interpreter::get().s_python_function_colorbar, args, kwargs);
1157+
if(!res) throw std::runtime_error("Call to colorbar() failed.");
1158+
1159+
Py_DECREF(args);
1160+
Py_DECREF(kwargs);
1161+
Py_DECREF(res);
1162+
}
1163+
11391164

11401165
inline long figure(long number = -1)
11411166
{

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