forked from alandefreitas/matplotplusplus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror_bar.cpp
More file actions
211 lines (186 loc) 路 7.63 KB
/
Copy patherror_bar.cpp
File metadata and controls
211 lines (186 loc) 路 7.63 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
//
// Created by Alan Freitas on 09/07/20.
//
#include <matplot/axes_objects/error_bar.h>
#include <matplot/core/axes_type.h>
#include <matplot/util/common.h>
#include <sstream>
namespace matplot {
error_bar::error_bar(class axes_type *parent) : line(parent) {}
error_bar::error_bar(class axes_type *parent, const std::vector<double> &x,
const std::vector<double> &y,
const std::vector<double> &y_neg_delta,
const std::vector<double> &y_pos_delta,
const std::vector<double> &x_neg_delta,
const std::vector<double> &x_pos_delta,
std::string_view line_spec)
: line(parent, x, y, line_spec), x_negative_delta_(x_neg_delta),
x_positive_delta_(x_pos_delta), y_negative_delta_(y_neg_delta),
y_positive_delta_(y_pos_delta) {}
error_bar::error_bar(class axes_type *parent, const std::vector<double> &x,
const std::vector<double> &y,
const std::vector<double> &error, error_bar::type type,
std::string_view line_spec)
: line(parent, x, y, line_spec),
x_negative_delta_(type != error_bar::type::vertical
? error
: std::vector<double>({})),
x_positive_delta_(type != error_bar::type::vertical
? error
: std::vector<double>({})),
y_negative_delta_(type != error_bar::type::horizontal
? error
: std::vector<double>({})),
y_positive_delta_(type != error_bar::type::horizontal
? error
: std::vector<double>({})) {}
std::string error_bar::plot_string() {
// plot errorbar behind the line
std::string res;
const bool has_y_bar = !y_negative_delta_.empty();
const bool has_x_bar = !x_negative_delta_.empty();
const bool has_xy_bar = has_y_bar && has_x_bar;
if (has_xy_bar) {
res += "'-' with xyerrorbars linecolor rgb \"" +
to_string(line_spec_.color()) + "\" pointsize 0";
} else if (has_y_bar) {
// filled curve error bar is only relevant in yerrorbars
if (!filled_curve_) {
res += "'-' with yerrorbars linecolor rgb \"" +
to_string(line_spec_.color()) + "\" pointsize 0";
} else {
color_array c = line_spec_.color();
c[0] = filled_curve_alpha_;
res += "'-' with filledcurve linecolor rgb \"" + to_string(c) +
"\"";
}
} else if (has_x_bar) {
res += "'-' with xerrorbars linecolor rgb \"" +
to_string(line_spec_.color()) + "\" pointsize 0";
}
// plot line over the errorbar
res += ", " + line::plot_string();
return res;
}
enum axes_object::axes_category error_bar::axes_category() {
// errorbar is always 2d
return axes_object::axes_category::two_dimensional;
}
std::string error_bar::data_string() {
// send data once more for the bar
const bool has_y_bar = !y_negative_delta_.empty();
const bool has_x_bar = !x_negative_delta_.empty();
const bool has_xy_bar = has_y_bar && has_x_bar;
std::stringstream ss;
ss.precision(10);
ss << std::fixed;
if (has_xy_bar) {
for (size_t i = 0; i < y_data_.size(); ++i) {
ss << " " << x_data_[i] << " " << y_data_[i] << " "
<< x_data_[i] - x_negative_delta_[i] << " "
<< x_data_[i] + x_positive_delta_[i] << " "
<< y_data_[i] - y_negative_delta_[i] << " "
<< y_data_[i] + y_positive_delta_[i] << "\n";
}
ss << " e\n";
} else if (has_x_bar) {
for (size_t i = 0; i < y_data_.size(); ++i) {
ss << " " << x_data_[i] << " " << y_data_[i] << " "
<< x_data_[i] - x_negative_delta_[i] << " "
<< x_data_[i] + x_positive_delta_[i] << "\n";
}
ss << " e\n";
} else if (has_y_bar) {
if (!filled_curve_) {
for (size_t i = 0; i < y_data_.size(); ++i) {
ss << " " << x_data_[i] << " " << y_data_[i] << " "
<< y_data_[i] - y_negative_delta_[i] << " "
<< y_data_[i] + y_positive_delta_[i] << "\n";
}
ss << " e\n";
} else {
for (size_t i = 0; i < y_data_.size(); ++i) {
ss << " " << x_data_[i] << " "
<< y_data_[i] + y_positive_delta_[i] << " "
<< y_data_[i] - y_negative_delta_[i] << "\n";
}
ss << " e\n";
}
}
// send data for foreground line as usual
ss << line::data_string();
return ss.str();
}
bool error_bar::requires_colormap() { return line::requires_colormap(); }
std::string error_bar::set_variables_string() {
// set bars {small | large | fullwidth | <size>}
// unset bars
// show bars
std::string res = line::set_variables_string();
if (cap_size_ / 3. != 1.) {
res += "set bars " + num2str(cap_size_ / 3.) + "\n";
}
return res;
}
std::string error_bar::unset_variables_string() {
std::string res = line::unset_variables_string();
if (cap_size_ / 3. != 1.) {
res += "unset bars\n";
}
return res;
}
bool error_bar::filled_curve() const { return filled_curve_; }
class error_bar &error_bar::filled_curve(bool filled_curve) {
filled_curve_ = filled_curve;
touch();
return *this;
}
const std::vector<double> &error_bar::x_negative_delta() const {
return x_negative_delta_;
}
class error_bar &
error_bar::x_negative_delta(const std::vector<double> &x_negative_delta) {
x_negative_delta_ = x_negative_delta;
touch();
return *this;
}
const std::vector<double> &error_bar::x_positive_delta() const {
return x_positive_delta_;
}
class error_bar &
error_bar::x_positive_delta(const std::vector<double> &x_positive_delta) {
x_positive_delta_ = x_positive_delta;
touch();
return *this;
}
const std::vector<double> &error_bar::y_negative_delta() const {
return y_negative_delta_;
}
class error_bar &
error_bar::y_negative_delta(const std::vector<double> &y_negative_delta) {
y_negative_delta_ = y_negative_delta;
touch();
return *this;
}
const std::vector<double> &error_bar::y_positive_delta() const {
return y_positive_delta_;
}
class error_bar &
error_bar::y_positive_delta(const std::vector<double> &y_positive_delta) {
y_positive_delta_ = y_positive_delta;
touch();
return *this;
}
float error_bar::filled_curve_alpha() const { return filled_curve_alpha_; }
class error_bar &error_bar::filled_curve_alpha(float filled_curve_alpha) {
filled_curve_alpha_ = filled_curve_alpha;
touch();
return *this;
}
float error_bar::cap_size() const { return cap_size_; }
class error_bar &error_bar::cap_size(float cap_size) {
cap_size_ = cap_size;
touch();
return *this;
}
} // namespace matplot