Skip to content

Commit 3c81acc

Browse files
Fix large numbers and add timestamp support (#273)
Fix large numbers and add timestamp support
1 parent e84b6a9 commit 3c81acc

21 files changed

+134
-31
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ out
1616
.idea
1717
codealike.json
1818
.vs
19+
.vscode
1920
CMakeLists.txt.user
2021
CMakeSettings.json
2122
gh-md-toc

source/matplot/axes_objects/bars.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,8 @@ namespace matplot {
143143

144144
std::string bars::data_string() {
145145
std::stringstream ss;
146+
ss.precision(5);
147+
ss << std::fixed;
146148
double m = x_minimum_difference();
147149
double cw = cluster_width();
148150
for (size_t bar_group = 0; bar_group < ys_.size(); ++bar_group) {

source/matplot/axes_objects/box_chart.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ namespace matplot {
127127
}
128128
std::vector<double> unique_groups = unique(x_data_);
129129
std::stringstream ss;
130+
ss.precision(5);
131+
ss << std::fixed;
130132
// for each group
131133
for (size_t i = 0; i < unique_groups.size(); ++i) {
132134
// for each point

source/matplot/axes_objects/circles.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ namespace matplot {
4545

4646
std::string circles::data_string() {
4747
std::stringstream ss;
48+
ss.precision(5);
49+
ss << std::fixed;
4850
for (size_t i = 0; i < x_.size(); ++i) {
4951
auto value_or_default = [](const std::vector<double> &v,
5052
size_t index, double default_value) {

source/matplot/axes_objects/contours.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ namespace matplot {
5050
double contour_max_level = *max_it;
5151

5252
std::stringstream ss;
53+
ss.precision(5);
54+
ss << std::fixed;
5355
if (filled_) {
5456
auto [lower_levels, upper_levels] = get_lowers_and_uppers();
5557
// Command for background filled curve
@@ -413,7 +415,8 @@ namespace matplot {
413415
_levels = levels_;
414416

415417
// Extend minimum beyond zmin (for filled plots)
416-
const bool log = parent_->z_axis().scale() == axis_type::axis_scale::log;
418+
const bool log =
419+
parent_->z_axis().scale() == axis_type::axis_scale::log;
417420
if (extend_ == extend_option::both || extend_ == extend_option::min) {
418421
double lower = log ? 1e-250 : -1e250;
419422
_levels.insert(_levels.begin(), lower);
@@ -475,6 +478,8 @@ namespace matplot {
475478
double zmin = *min_level_it;
476479

477480
std::stringstream ss;
481+
ss.precision(5);
482+
ss << std::fixed;
478483
for (size_t i = 0; i < lines_.size(); ++i) {
479484
if (i != 0) {
480485
ss << ",";
@@ -766,6 +771,8 @@ namespace matplot {
766771
auto [lower_levels, upper_levels] = get_lowers_and_uppers();
767772

768773
std::stringstream ss;
774+
ss.precision(5);
775+
ss << std::fixed;
769776
if (filled_) {
770777
// Plot the line segments
771778
// Create one filled curve for each segment of a contour.
@@ -1024,7 +1031,7 @@ namespace matplot {
10241031
// nans at end of the underlying storage)
10251032
while (j + 1 < lines_[i].first.size() &&
10261033
is_separator(lines_[i].first[j + 1],
1027-
lines_[i].second[j + 1])) {
1034+
lines_[i].second[j + 1])) {
10281035
++j;
10291036
}
10301037
// Include an empty line to indicate this polygon or line

source/matplot/axes_objects/error_bar.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ namespace matplot {
7979
const bool has_x_bar = !x_negative_delta_.empty();
8080
const bool has_xy_bar = has_y_bar && has_x_bar;
8181
std::stringstream ss;
82+
ss.precision(5);
83+
ss << std::fixed;
8284
if (has_xy_bar) {
8385
for (size_t i = 0; i < y_data_.size(); ++i) {
8486
ss << " " << x_data_[i] << " " << y_data_[i] << " "

source/matplot/axes_objects/filled_area.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ namespace matplot {
5858

5959
std::string filled_area::data_string() {
6060
std::stringstream ss;
61+
ss.precision(5);
62+
ss << std::fixed;
6163

6264
std::vector<double> stacked_data;
6365
if (!stacked_) {

source/matplot/axes_objects/histogram.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ namespace matplot {
5555
std::string histogram::plot_string() {
5656
maybe_update_face_color();
5757
std::stringstream ss;
58+
ss.precision(5);
59+
ss << std::fixed;
5860
if (!is_polar()) {
5961
if (!stairs_only_) {
6062
ss << " '-' with boxes fillstyle solid";
@@ -87,6 +89,8 @@ namespace matplot {
8789
std::string histogram::data_string() {
8890
make_sure_data_is_preprocessed();
8991
std::stringstream ss;
92+
ss.precision(5);
93+
ss << std::fixed;
9094
if (!is_polar()) {
9195
for (size_t i = 0; i < values_.size(); ++i) {
9296
// <box center> <box height> <box width>
@@ -517,7 +521,8 @@ namespace matplot {
517521
case binning_algorithm::sqrt:
518522
return sqrt_rule(data, minx, maxx, hard_limits);
519523
}
520-
throw std::logic_error("histogram::histrogram_edges: could not find the binning algorithm");
524+
throw std::logic_error("histogram::histrogram_edges: could not find "
525+
"the binning algorithm");
521526
}
522527

523528
std::vector<size_t>

source/matplot/axes_objects/labels.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ namespace matplot {
9898
font_size_factor = (xrange_increase + yrange_increase) / 2.;
9999
}
100100
std::stringstream ss;
101+
ss.precision(5);
102+
ss << std::fixed;
101103
const bool custom_sizes = !sizes_.empty();
102104
const bool custom_colors = !colors_.empty();
103105
for (size_t i = 0; i < labels_.size(); ++i) {

source/matplot/axes_objects/line.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,8 @@ namespace matplot {
163163
const bool x_is_manual = !x_data_.empty();
164164

165165
std::stringstream ss;
166+
ss.precision(5);
167+
ss << std::fixed;
166168
for (const auto &style : styles_to_plot()) {
167169
if (visible_) {
168170
const bool data_is_for_markers =
@@ -509,7 +511,7 @@ namespace matplot {
509511
void line::run_draw_commands() {
510512
// ask axes to draw the line
511513
maybe_update_line_spec();
512-
parent_->draw_path(x_data_,y_data_,line_spec_.color());
514+
parent_->draw_path(x_data_, y_data_, line_spec_.color());
513515
}
514516

515517
} // namespace matplot

source/matplot/axes_objects/matrix.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,8 @@ namespace matplot {
167167

168168
// stream matrix
169169
std::stringstream ss;
170+
ss.precision(5);
171+
ss << std::fixed;
170172
double x_width_ = x_width();
171173
double y_width_ = y_width();
172174
const auto &[cb_min, cb_max] = parent_->color_box_range();
@@ -273,6 +275,8 @@ namespace matplot {
273275

274276
std::string matrix::image_data_string() {
275277
std::stringstream ss;
278+
ss.precision(5);
279+
ss << std::fixed;
276280
auto [h, w] = size(matrices_[0]);
277281
double x_width_ = x_width();
278282
double y_width_ = y_width();

source/matplot/axes_objects/network.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ namespace matplot {
106106

107107
std::string network::data_string() {
108108
std::stringstream ss;
109+
ss.precision(5);
110+
ss << std::fixed;
109111
bool plot_z_data = z_data_.size() == x_data_.size();
110112
bool line_width_is_variable = !line_widths_.empty() && z_data_.empty();
111113
// plot edges

source/matplot/axes_objects/network.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ namespace matplot {
3030
/// If we receive an axes_handle, we can convert it to a raw
3131
/// pointer because there is no ownership involved here
3232
template <class... Args>
33-
network(const axes_handle &parent, Args&&... args)
33+
network(const axes_handle &parent, Args &&...args)
3434
: network(parent.get(), std::forward<Args>(args)...) {}
3535

3636
virtual ~network() = default;
@@ -83,6 +83,8 @@ namespace matplot {
8383
network &edge_labels(const IterableValues<C> &e_labels) {
8484
std::vector<std::string> str_labels;
8585
std::stringstream ss;
86+
ss.precision(5);
87+
ss << std::fixed;
8688
for (const auto &edge_label : e_labels) {
8789
ss << "{/:Italic " << edge_label << " }";
8890
str_labels.emplace_back(ss.str());
@@ -99,6 +101,8 @@ namespace matplot {
99101
network &node_labels(const IterableValues<C> &e_labels) {
100102
std::vector<std::string> str_labels;
101103
std::stringstream ss;
104+
ss.precision(5);
105+
ss << std::fixed;
102106
for (const auto &edge_label : e_labels) {
103107
ss << edge_label;
104108
str_labels.emplace_back(ss.str());

source/matplot/axes_objects/parallel_lines.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,8 @@ namespace matplot {
170170

171171
const bool color_is_variable = !line_colors_.empty();
172172
std::stringstream ss;
173+
ss.precision(5);
174+
ss << std::fixed;
173175
// for each point
174176
for (size_t i = 0; i < data_[0].size(); ++i) {
175177
// for each dimension

source/matplot/axes_objects/surface.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ namespace matplot {
5858
std::string surface::set_variables_string() {
5959
maybe_update_line_spec();
6060
std::stringstream ss;
61+
ss.precision(5);
62+
ss << std::fixed;
6163

6264
if (surface_in_2d_) {
6365
ss << " set view map\n";
@@ -247,6 +249,8 @@ namespace matplot {
247249

248250
std::string surface::plot_string() {
249251
std::stringstream ss;
252+
ss.precision(5);
253+
ss << std::fixed;
250254
// plot surface
251255
bool is_solid_surface = palette_map_at_bottom_ ||
252256
palette_map_at_surface_ || palette_map_at_top_;
@@ -346,6 +350,8 @@ namespace matplot {
346350

347351
std::string surface::grid_data_string() {
348352
std::stringstream ss;
353+
ss.precision(5);
354+
ss << std::fixed;
349355
const bool contour = (contour_base_ || contour_surface_);
350356
const bool palette_map_3d = palette_map_at_bottom_ ||
351357
palette_map_at_surface_ ||
@@ -469,6 +475,8 @@ namespace matplot {
469475

470476
std::string surface::ribbon_data_string() {
471477
std::stringstream ss;
478+
ss.precision(5);
479+
ss << std::fixed;
472480
auto send_point = [](std::stringstream &ss, double x, double y,
473481
double z, double c) {
474482
ss << " " << x;

source/matplot/axes_objects/vectors.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ namespace matplot {
7272
std::string vectors::plot_string() {
7373
maybe_update_line_spec();
7474
std::stringstream ss;
75+
ss.precision(5);
76+
ss << std::fixed;
7577
ss << " '-' with vectors";
7678
if (!c_data_.empty()) {
7779
ss << " linecolor palette";
@@ -113,6 +115,8 @@ namespace matplot {
113115

114116
std::string vectors::data_string() {
115117
std::stringstream ss;
118+
ss.precision(5);
119+
ss << std::fixed;
116120
if (visible_) {
117121
for (size_t i = 0; i < v_data_.size(); ++i) {
118122
double x_value = x_data_.size() > i ? x_data_[i]

source/matplot/backend/gnuplot.cpp

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88
#else
99
#include <filesystem>
1010
#endif
11+
#include <cstdlib>
1112
#include <iostream>
1213
#include <matplot/util/common.h>
1314
#include <matplot/util/popen.h>
1415
#include <regex>
1516
#include <thread>
16-
#include <cstdlib>
1717

1818
#ifdef MATPLOT_HAS_FBUFSIZE
1919

@@ -205,9 +205,15 @@ namespace matplot::backend {
205205
run_command("set terminal " + terminal_ + " position " +
206206
num2str(position_[0]) + "," + num2str(position_[1]));
207207
}
208-
if (terminal_has_size_option(terminal_)) {
209-
run_command("set terminal " + terminal_ + " size " +
210-
num2str(position_[2]) + "," + num2str(position_[3]));
208+
if (terminal_ == "dumb") {
209+
run_command("set terminal dumb " + num2str(position_[2]) + " " +
210+
num2str(position_[3]));
211+
} else {
212+
if (terminal_has_size_option(terminal_)) {
213+
run_command("set terminal " + terminal_ + " size " +
214+
num2str(position_[2]) + "," +
215+
num2str(position_[3]));
216+
}
211217
}
212218
}
213219

@@ -217,9 +223,15 @@ namespace matplot::backend {
217223
run_command("set terminal " + terminal_ + " position " +
218224
num2str(position_[0]) + "," + num2str(position_[1]));
219225
}
220-
if (terminal_has_size_option(terminal_)) {
221-
run_command("set terminal " + terminal_ + " size " +
222-
num2str(position_[2]) + "," + num2str(position_[3]));
226+
if (terminal_ == "dumb") {
227+
run_command("set terminal dumb " + num2str(position_[2]) + " " +
228+
num2str(position_[3]));
229+
} else {
230+
if (terminal_has_size_option(terminal_)) {
231+
run_command("set terminal " + terminal_ + " size " +
232+
num2str(position_[2]) + "," +
233+
num2str(position_[3]));
234+
}
223235
}
224236
}
225237

@@ -318,7 +330,9 @@ namespace matplot::backend {
318330
}
319331

320332
bool gnuplot::terminal_is_available(std::string_view term) {
321-
std::string msg = run_and_get_output("gnuplot -e \"set terminal " + std::string(term.data()) + "\" 2>&1");
333+
std::string msg =
334+
run_and_get_output("gnuplot -e \"set terminal " +
335+
std::string(term.data()) + "\" 2>&1");
322336
return msg.empty();
323337
}
324338

source/matplot/core/axes_type.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ namespace matplot {
100100
if (any_obj_needs_colormap && !colormap_.empty() &&
101101
!children_.empty()) {
102102
std::stringstream ss;
103+
ss.precision(5);
104+
ss << std::fixed;
103105
// limit the number of colors in the palette
104106
// this is useful for contour plots
105107
if (max_colors_) {
@@ -525,6 +527,8 @@ namespace matplot {
525527

526528
if (cb_axis_.visible()) {
527529
std::stringstream ss;
530+
ss.precision(5);
531+
ss << std::fixed;
528532
ss << "set colorbox";
529533
if (!cb_axis_.reverse()) {
530534
ss << " noinvert";
@@ -3310,6 +3314,8 @@ namespace matplot {
33103314
std::vector<std::string> pie_labels(x.size());
33113315
double theta_sum = 0.; // [0;2pi]
33123316
std::stringstream stream;
3317+
stream.precision(5);
3318+
stream << std::fixed;
33133319
for (size_t i = 0; i < x.size(); ++i) {
33143320
if (labels.size() > i) {
33153321
stream << labels[i];
@@ -4589,7 +4595,7 @@ namespace matplot {
45894595
scale, line_spec);
45904596
}
45914597

4592-
/// Quiver - 2d x,y,u,v with no color mapping
4598+
/// Quiver - 2d x,y,u,v with no color mapping
45934599
vectors_handle axes_type::quiver(const std::vector<std::vector<double>> &x,
45944600
const std::vector<std::vector<double>> &y,
45954601
const std::vector<std::vector<double>> &u,
@@ -4599,7 +4605,7 @@ namespace matplot {
45994605
scale, line_spec);
46004606
}
46014607

4602-
/// Quiver - x,y,u,v
4608+
/// Quiver - x,y,u,v
46034609
vectors_handle axes_type::quiver(const std::vector<double> &x,
46044610
const std::vector<double> &y,
46054611
const std::vector<double> &u,

0 commit comments

Comments
 (0)