CoolProp  4.2.5
An open-source fluid property and humid air property database
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
MPLPlot.h
Go to the documentation of this file.
1 #ifndef MPLPLOT_H
2 #define MPLPLOT_H
3 
4 #include <Python.h>
5 #include <string>
6 #include <map>
7 #include <vector>
8 #include "MatrixMath.h"
9 
10 
14 {
15 public:
16  Dictionary(void){};
17  std::map<std::string, std::string> string_values;
18  std::map<std::string, double> double_values;
19 
21  void add(std::string key, double value){
22  double_values.insert(std::pair<std::string, double>(key, value));
23  };
24 
26  void add(std::string key, std::string value){
27  string_values.insert(std::pair<std::string,std::string>(key,value));
28  };
29 
32  std::string print_call()
33  {
34  std::string s;
35 
36  int i = 0;
37  std::string el;
38  for (std::map<std::string, double>::iterator it = double_values.begin(); it != double_values.end(); it ++)
39  {
40  el = ", " + (*it).first + " = " + format("%0.12g",(*it).second);
41  s += el;
42  i++;
43  }
44  el = "";
45  for (std::map<std::string, std::string>::iterator it = string_values.begin(); it != string_values.end(); it ++)
46  {
47  el = ", " + (*it).first + " = '" + (*it).second + "'" ;
48  s += el;
49  i++;
50  }
51 
52  // Determine what to return (empty if there is nothing here)
53  if (i > 0)
54  {
55  return s;
56  }
57  else
58  {
59  return std::string("");
60  }
61  }
62 
63 };
64 
67 class PlotCall
68 {
69 private:
70  bool dict_set;
71 public:
73  std::vector<double> x,y;
74 
76  PlotCall(std::vector<double> x, std::vector<double> y, Dictionary *dict = NULL)
77  {
78  if (dict != NULL)
79  {
80  this->dict = *dict;
81  dict_set = true;
82  }
83  else
84  {
85  dict_set = false;
86  }
87 
88  this->x = x;
89  this->y = y;
90  };
91 
93  std::string tostring()
94  {
95  std::string s;
96  s += "x = " + vec_to_string(x,"%0.16g") + "\n";
97  s += "y = " + vec_to_string(y,"%0.16g") + "\n";
98  if (&dict != NULL)
99  {
100  s += "plt.plot(x, y" + dict.print_call()+")\n";
101  }
102  return s;
103  }
104 };
105 
107 {
108 private:
109  std::vector<PlotCall> PlotCalls;
110 public:
111  void plot(std::vector<double> x, std::vector<double> y, Dictionary *dict = NULL)
112  {
113  PlotCalls.push_back(PlotCall(x,y,dict));
114  };
115  void additional_code(std::string){};
116 
117  std::string print_calls()
118  {
119  std::string s;
120  for (std::vector<PlotCall>::iterator it = PlotCalls.begin(); it!=PlotCalls.end(); it++)
121  {
122  s += (*it).tostring();
123  }
124  return s;
125  };
126  void show()
127  {
128  std::string s = "import matplotlib\nmatplotlib.use('TkAgg')\nimport matplotlib.pyplot as plt\n";
129  s += this->print_calls();
130  s += "plt.savefig('AA.png')\n";
131  s += "plt.show()\n";
132  if (PyRun_SimpleString(s.c_str()) != 0)
133  {
134  FILE *fp;
135  fp = fopen("errored_plot.py","w");
136  fprintf(fp,"%s",s.c_str());
137  fclose(fp);
138  std::cout << format("plot failed. Written log to errored_plot.py\n");
139  };
140  };
141 };
142 
143 #endif
std::string print_calls()
Definition: MPLPlot.h:117
Dictionary dict
Definition: MPLPlot.h:72
std::vector< double > y
Definition: MPLPlot.h:73
void additional_code(std::string)
Definition: MPLPlot.h:115
void add(std::string key, double value)
Add a floating point value to the dictionary.
Definition: MPLPlot.h:21
std::vector< double > x
Definition: MPLPlot.h:73
std::string format(const char *fmt,...)
std::vector< double > x(ncmax, 0)
std::string print_call()
Definition: MPLPlot.h:32
void add(std::string key, std::string value)
Add a string to the dictionary.
Definition: MPLPlot.h:26
std::map< std::string, double > double_values
Definition: MPLPlot.h:18
std::string vec_to_string(double const &a)
Definition: MatrixMath.cpp:359
void show()
Definition: MPLPlot.h:126
PlotCall(std::vector< double > x, std::vector< double > y, Dictionary *dict=NULL)
Save internal variables for this call to plot function, including the keyword argument function as a ...
Definition: MPLPlot.h:76
std::string tostring()
return the string for this call, including the preceding import matplotlib.pyplot as plt and the foll...
Definition: MPLPlot.h:93
std::map< std::string, std::string > string_values
Definition: MPLPlot.h:16
Dictionary(void)
Definition: MPLPlot.h:16
void plot(std::vector< double > x, std::vector< double > y, Dictionary *dict=NULL)
Definition: MPLPlot.h:111