-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBarPlot.cpp
More file actions
148 lines (131 loc) · 4.21 KB
/
BarPlot.cpp
File metadata and controls
148 lines (131 loc) · 4.21 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
#include "BarPlot.h"
#include "Exceptions.h"
#include "Helper.h"
#include "Log.h"
#include "BasicStatistics.h"
#include <QStringList>
#include <limits>
#include <QProcess>
#include <QStandardPaths>
#include "Settings.h"
BarPlot::BarPlot()
{
}
void BarPlot::setValues(const QList<int>& values, const QList<QString>& labels, const QList<QString>& colors)
{
for(int i=0;i<values.count();++i)
{
bars_.append(double(values[i]));
labels_.append(labels[i]);
colors_.append(colors);
}
}
void BarPlot::setValues(const QList<double>& values, const QList<QString>& labels, const QList<QString>& colors)
{
for(int i=0;i<values.count();++i)
{
bars_.append(values[i]);
labels_.append(labels[i]);
colors_.append(colors);
}
}
void BarPlot::addColorLegend(QString color, QString desc)
{
color_legend_.insert(color, desc);
}
void BarPlot::store(QString filename)
{
//create python script
QStringList script;
script.append("from numpy import nan");
script.append("import matplotlib as mpl");
script.append("mpl.use('Agg')");
script.append("import matplotlib.pyplot as plt");
script.append("import matplotlib.patches as mpatches");
script.append("plt.figure(figsize=(10, 4), dpi=100)");
if(ylabel_!="") script.append("plt.ylabel('" + ylabel_ + "')");
if(xlabel_!="") script.append("plt.xlabel('" + xlabel_ + "')");
if(BasicStatistics::isValidFloat(xmin_) && BasicStatistics::isValidFloat(xmax_))
{
script.append("plt.xlim(" + QString::number(xmin_) + "," + QString::number(xmax_) + ")");
}
if(BasicStatistics::isValidFloat(ymin_) && BasicStatistics::isValidFloat(ymax_))
{
script.append("plt.ylim(" + QString::number(ymin_) + "," + QString::number(ymax_) + ")");
}
//data
QString xvaluestring = "";
QString yvaluestring = "";
xvaluestring += "[" + QString::number(0);
yvaluestring += "[" + QString::number(bars_[0]);
for(int i=1;i<bars_.count();++i)
{
xvaluestring += ","+QString::number(i);
yvaluestring += ","+QString::number(bars_[i]);
}
xvaluestring += "]";
yvaluestring += "]";
//labels
QString labelstring = "";
if(!labels_.empty())
{
labelstring += "['" + labels_[0]+"'";
for(int i=1;i<bars_.count();++i)
{
labelstring += ",'" + labels_[i]+"'";
}
labelstring += "]";
}
//colors
QString colorstring = "";
if(!colors_.empty())
{
colorstring += "['" + colors_.at(0)+"'";
for(int i=1;i<bars_.count();++i)
{
colorstring += ",'" + colors_.at(i)+"'";
}
colorstring += "]";
}
script.append("barlist=plt.bar(" + xvaluestring + "," + yvaluestring + ",align='center'" + (colorstring.isEmpty() ? "" : ",color=" + colorstring ) + ", edgecolor='none')");
script.append("plt.xticks(" + xvaluestring + (labelstring.isEmpty() ? "" : "," + labelstring) + ", size='xx-small',rotation=90,horizontalalignment='center')");
script.append("plt.yticks(size=10)");
script.append("plt.tick_params(axis='x',which='both',length=0,bottom='off',top='off')");
script.append("plt.tick_params(axis='y',which='both',left='off',right='off')");
//legend
QString c = "";
QString d = "";
foreach(const QString& desc, color_legend_)
{
QString col = color_legend_.key(desc);
c += "mpatches.Patch(color='" + col + "'),";
d += "'" + desc + "',";
}
script.append("plt.legend((" + c + "),(" + d + "),fontsize=10, bbox_to_anchor=(1.025,1), loc=2, borderaxespad=0.,frameon=0)");
//file handling
script.append("plt.savefig('" + filename.replace("\\", "/") + "', bbox_inches=\'tight\', dpi=100)");
//check if python is installed
QString python_exe = Settings::path("python_exe", true);
if (python_exe=="") python_exe = QStandardPaths::findExecutable("python3");
if (python_exe!="")
{
QString scriptfile = Helper::tempFileName(".py");
Helper::storeTextFile(scriptfile, script);
//execute scipt
QProcess process;
process.setProcessChannelMode(QProcess::MergedChannels);
process.start(python_exe, QStringList() << scriptfile);
bool success = process.waitForFinished(-1);
QByteArray output = process.readAll();
if (!success || process.exitCode()>0)
{
THROW(ProgrammingException, "Could not execute python script:\n" + scriptfile + "\n Error message is: " + output);
}
//remove temporary file
QFile::remove(scriptfile);
}
else
{
Log::warn("Python executable not found in PATH - skipping plot generation!");
}
}