-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMPLPlot.h
More file actions
168 lines (149 loc) · 3.74 KB
/
Copy pathMPLPlot.h
File metadata and controls
168 lines (149 loc) · 3.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#ifndef MPLPLOT_H
#define MPLPLOT_H
#include <string>
#include <map>
#include <vector>
std::string format(const char* fmt, ...)
{
int size = 512;
char* buffer = 0;
buffer = new char[size];
va_list vl;
va_start(vl,fmt);
int nsize = vsnprintf(buffer,size,fmt,vl);
if(size<=nsize){//fail delete buffer and try again
delete buffer; buffer = 0;
buffer = new char[nsize+1];//+1 for /0
nsize = vsnprintf(buffer,size,fmt,vl);
}
std::string ret(buffer);
va_end(vl);
delete buffer;
return ret;
}
std::string vector2string(std::vector<double> x)
{
std::string s;
int i = 0;
for (std::vector<double>::iterator it = x.begin(); it != x.end(); it++)
{
if (i != 0) s += ", ";
s += format("%0.16g",*it);
i++;
}
return s;
}
/*! A C++ equivalent of a dictionary to hold values for the plotting functions
*/
class Dictionary
{
public:
Dictionary(void){};
std::map<std::string, std::string> string_values;
std::map<std::string, double> double_values;
/// Add a floating point value to the dictionary
void add(std::string key, double value){
double_values.insert(std::pair<std::string, double>(key, value));
};
/// Add a string to the dictionary
void add(std::string key, std::string value){
string_values.insert(std::pair<std::string,std::string>(key,value));
};
/// print in the form for the call to plot function in matplotlib.pyplot.plot function
/// k1 = v1, k2 = v2, k3 = v3 ..... with v3 properly quote escaped
std::string print_call()
{
std::string s;
int i = 0;
std::string el;
for (std::map<std::string, double>::iterator it = double_values.begin(); it != double_values.end(); it ++)
{
el += ", " + (*it).first + " = " + format("%0.12g",(*it).second);
s += el;
i++;
}
el = "";
for (std::map<std::string, std::string>::iterator it = string_values.begin(); it != string_values.end(); it ++)
{
el += ", " + (*it).first + " = '" + (*it).second + "'" ;
s += el;
i++;
}
// Determine what to return (empty if there is nothing here)
if (i > 0)
{
return s;
}
else
{
return std::string("");
}
}
};
/*! A class to contain the information for a call to plot() function
*/
class PlotCall
{
private:
bool dict_set;
public:
Dictionary dict;
std::vector<double> x,y;
/// Save internal variables for this call to plot function, including the keyword argument function as a Dictionary instance
PlotCall(std::vector<double> x, std::vector<double> y, Dictionary *dict = NULL)
{
if (dict != NULL)
{
this->dict = *dict;
dict_set = true;
}
else
{
dict_set = false;
}
this->x = x;
this->y = y;
};
/// return the string for this call, including the preceding import matplotlib.pyplot as plt and the following plt.show()
std::string tostring()
{
std::string s;
s += "x = [" + vector2string(x) + "]\n";
s += "y = [" + vector2string(y) + "]\n";
if (&dict != NULL)
{
s += "plt.plot(x, y" + dict.print_call()+")\n";
}
return s;
}
};
class PyPlotter
{
private:
std::vector<PlotCall> PlotCalls;
public:
void plot(std::vector<double> x, std::vector<double> y, Dictionary *dict = NULL)
{
PlotCalls.push_back(PlotCall(x,y,dict));
};
void additional_code(std::string){};
std::string print_calls()
{
std::string s;
for (std::vector<PlotCall>::iterator it = PlotCalls.begin(); it!=PlotCalls.end(); it++)
{
s += (*it).tostring();
}
return s;
};
void show()
{
Py_Initialize();
std::string s = "import matplotlib.pyplot as plt\n";
s += this->print_calls() ;
s += "plt.show()\n"
PyRun_SimpleString(s.c_str());
Py_Finalize();
};
};
#endif