Skip to content

Commit

Permalink
Fix large numbers and add timestamp support (#273)
Browse files Browse the repository at this point in the history
Fix large numbers and add timestamp support
  • Loading branch information
TheGreatRambler authored Jul 30, 2022
1 parent e84b6a9 commit 3c81acc
Show file tree
Hide file tree
Showing 21 changed files with 134 additions and 31 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ out
.idea
codealike.json
.vs
.vscode
CMakeLists.txt.user
CMakeSettings.json
gh-md-toc
Expand Down
2 changes: 2 additions & 0 deletions source/matplot/axes_objects/bars.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ namespace matplot {

std::string bars::data_string() {
std::stringstream ss;
ss.precision(5);
ss << std::fixed;
double m = x_minimum_difference();
double cw = cluster_width();
for (size_t bar_group = 0; bar_group < ys_.size(); ++bar_group) {
Expand Down
2 changes: 2 additions & 0 deletions source/matplot/axes_objects/box_chart.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ namespace matplot {
}
std::vector<double> unique_groups = unique(x_data_);
std::stringstream ss;
ss.precision(5);
ss << std::fixed;
// for each group
for (size_t i = 0; i < unique_groups.size(); ++i) {
// for each point
Expand Down
2 changes: 2 additions & 0 deletions source/matplot/axes_objects/circles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ namespace matplot {

std::string circles::data_string() {
std::stringstream ss;
ss.precision(5);
ss << std::fixed;
for (size_t i = 0; i < x_.size(); ++i) {
auto value_or_default = [](const std::vector<double> &v,
size_t index, double default_value) {
Expand Down
11 changes: 9 additions & 2 deletions source/matplot/axes_objects/contours.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ namespace matplot {
double contour_max_level = *max_it;

std::stringstream ss;
ss.precision(5);
ss << std::fixed;
if (filled_) {
auto [lower_levels, upper_levels] = get_lowers_and_uppers();
// Command for background filled curve
Expand Down Expand Up @@ -413,7 +415,8 @@ namespace matplot {
_levels = levels_;

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

std::stringstream ss;
ss.precision(5);
ss << std::fixed;
for (size_t i = 0; i < lines_.size(); ++i) {
if (i != 0) {
ss << ",";
Expand Down Expand Up @@ -766,6 +771,8 @@ namespace matplot {
auto [lower_levels, upper_levels] = get_lowers_and_uppers();

std::stringstream ss;
ss.precision(5);
ss << std::fixed;
if (filled_) {
// Plot the line segments
// Create one filled curve for each segment of a contour.
Expand Down Expand Up @@ -1024,7 +1031,7 @@ namespace matplot {
// nans at end of the underlying storage)
while (j + 1 < lines_[i].first.size() &&
is_separator(lines_[i].first[j + 1],
lines_[i].second[j + 1])) {
lines_[i].second[j + 1])) {
++j;
}
// Include an empty line to indicate this polygon or line
Expand Down
2 changes: 2 additions & 0 deletions source/matplot/axes_objects/error_bar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ namespace matplot {
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(5);
ss << std::fixed;
if (has_xy_bar) {
for (size_t i = 0; i < y_data_.size(); ++i) {
ss << " " << x_data_[i] << " " << y_data_[i] << " "
Expand Down
2 changes: 2 additions & 0 deletions source/matplot/axes_objects/filled_area.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ namespace matplot {

std::string filled_area::data_string() {
std::stringstream ss;
ss.precision(5);
ss << std::fixed;

std::vector<double> stacked_data;
if (!stacked_) {
Expand Down
7 changes: 6 additions & 1 deletion source/matplot/axes_objects/histogram.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ namespace matplot {
std::string histogram::plot_string() {
maybe_update_face_color();
std::stringstream ss;
ss.precision(5);
ss << std::fixed;
if (!is_polar()) {
if (!stairs_only_) {
ss << " '-' with boxes fillstyle solid";
Expand Down Expand Up @@ -87,6 +89,8 @@ namespace matplot {
std::string histogram::data_string() {
make_sure_data_is_preprocessed();
std::stringstream ss;
ss.precision(5);
ss << std::fixed;
if (!is_polar()) {
for (size_t i = 0; i < values_.size(); ++i) {
// <box center> <box height> <box width>
Expand Down Expand Up @@ -517,7 +521,8 @@ namespace matplot {
case binning_algorithm::sqrt:
return sqrt_rule(data, minx, maxx, hard_limits);
}
throw std::logic_error("histogram::histrogram_edges: could not find the binning algorithm");
throw std::logic_error("histogram::histrogram_edges: could not find "
"the binning algorithm");
}

std::vector<size_t>
Expand Down
2 changes: 2 additions & 0 deletions source/matplot/axes_objects/labels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ namespace matplot {
font_size_factor = (xrange_increase + yrange_increase) / 2.;
}
std::stringstream ss;
ss.precision(5);
ss << std::fixed;
const bool custom_sizes = !sizes_.empty();
const bool custom_colors = !colors_.empty();
for (size_t i = 0; i < labels_.size(); ++i) {
Expand Down
4 changes: 3 additions & 1 deletion source/matplot/axes_objects/line.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ namespace matplot {
const bool x_is_manual = !x_data_.empty();

std::stringstream ss;
ss.precision(5);
ss << std::fixed;
for (const auto &style : styles_to_plot()) {
if (visible_) {
const bool data_is_for_markers =
Expand Down Expand Up @@ -509,7 +511,7 @@ namespace matplot {
void line::run_draw_commands() {
// ask axes to draw the line
maybe_update_line_spec();
parent_->draw_path(x_data_,y_data_,line_spec_.color());
parent_->draw_path(x_data_, y_data_, line_spec_.color());
}

} // namespace matplot
4 changes: 4 additions & 0 deletions source/matplot/axes_objects/matrix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,8 @@ namespace matplot {

// stream matrix
std::stringstream ss;
ss.precision(5);
ss << std::fixed;
double x_width_ = x_width();
double y_width_ = y_width();
const auto &[cb_min, cb_max] = parent_->color_box_range();
Expand Down Expand Up @@ -273,6 +275,8 @@ namespace matplot {

std::string matrix::image_data_string() {
std::stringstream ss;
ss.precision(5);
ss << std::fixed;
auto [h, w] = size(matrices_[0]);
double x_width_ = x_width();
double y_width_ = y_width();
Expand Down
2 changes: 2 additions & 0 deletions source/matplot/axes_objects/network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ namespace matplot {

std::string network::data_string() {
std::stringstream ss;
ss.precision(5);
ss << std::fixed;
bool plot_z_data = z_data_.size() == x_data_.size();
bool line_width_is_variable = !line_widths_.empty() && z_data_.empty();
// plot edges
Expand Down
6 changes: 5 additions & 1 deletion source/matplot/axes_objects/network.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace matplot {
/// If we receive an axes_handle, we can convert it to a raw
/// pointer because there is no ownership involved here
template <class... Args>
network(const axes_handle &parent, Args&&... args)
network(const axes_handle &parent, Args &&...args)
: network(parent.get(), std::forward<Args>(args)...) {}

virtual ~network() = default;
Expand Down Expand Up @@ -83,6 +83,8 @@ namespace matplot {
network &edge_labels(const IterableValues<C> &e_labels) {
std::vector<std::string> str_labels;
std::stringstream ss;
ss.precision(5);
ss << std::fixed;
for (const auto &edge_label : e_labels) {
ss << "{/:Italic " << edge_label << " }";
str_labels.emplace_back(ss.str());
Expand All @@ -99,6 +101,8 @@ namespace matplot {
network &node_labels(const IterableValues<C> &e_labels) {
std::vector<std::string> str_labels;
std::stringstream ss;
ss.precision(5);
ss << std::fixed;
for (const auto &edge_label : e_labels) {
ss << edge_label;
str_labels.emplace_back(ss.str());
Expand Down
2 changes: 2 additions & 0 deletions source/matplot/axes_objects/parallel_lines.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,8 @@ namespace matplot {

const bool color_is_variable = !line_colors_.empty();
std::stringstream ss;
ss.precision(5);
ss << std::fixed;
// for each point
for (size_t i = 0; i < data_[0].size(); ++i) {
// for each dimension
Expand Down
8 changes: 8 additions & 0 deletions source/matplot/axes_objects/surface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ namespace matplot {
std::string surface::set_variables_string() {
maybe_update_line_spec();
std::stringstream ss;
ss.precision(5);
ss << std::fixed;

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

std::string surface::plot_string() {
std::stringstream ss;
ss.precision(5);
ss << std::fixed;
// plot surface
bool is_solid_surface = palette_map_at_bottom_ ||
palette_map_at_surface_ || palette_map_at_top_;
Expand Down Expand Up @@ -346,6 +350,8 @@ namespace matplot {

std::string surface::grid_data_string() {
std::stringstream ss;
ss.precision(5);
ss << std::fixed;
const bool contour = (contour_base_ || contour_surface_);
const bool palette_map_3d = palette_map_at_bottom_ ||
palette_map_at_surface_ ||
Expand Down Expand Up @@ -469,6 +475,8 @@ namespace matplot {

std::string surface::ribbon_data_string() {
std::stringstream ss;
ss.precision(5);
ss << std::fixed;
auto send_point = [](std::stringstream &ss, double x, double y,
double z, double c) {
ss << " " << x;
Expand Down
4 changes: 4 additions & 0 deletions source/matplot/axes_objects/vectors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ namespace matplot {
std::string vectors::plot_string() {
maybe_update_line_spec();
std::stringstream ss;
ss.precision(5);
ss << std::fixed;
ss << " '-' with vectors";
if (!c_data_.empty()) {
ss << " linecolor palette";
Expand Down Expand Up @@ -113,6 +115,8 @@ namespace matplot {

std::string vectors::data_string() {
std::stringstream ss;
ss.precision(5);
ss << std::fixed;
if (visible_) {
for (size_t i = 0; i < v_data_.size(); ++i) {
double x_value = x_data_.size() > i ? x_data_[i]
Expand Down
30 changes: 22 additions & 8 deletions source/matplot/backend/gnuplot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
#else
#include <filesystem>
#endif
#include <cstdlib>
#include <iostream>
#include <matplot/util/common.h>
#include <matplot/util/popen.h>
#include <regex>
#include <thread>
#include <cstdlib>

#ifdef MATPLOT_HAS_FBUFSIZE

Expand Down Expand Up @@ -205,9 +205,15 @@ namespace matplot::backend {
run_command("set terminal " + terminal_ + " position " +
num2str(position_[0]) + "," + num2str(position_[1]));
}
if (terminal_has_size_option(terminal_)) {
run_command("set terminal " + terminal_ + " size " +
num2str(position_[2]) + "," + num2str(position_[3]));
if (terminal_ == "dumb") {
run_command("set terminal dumb " + num2str(position_[2]) + " " +
num2str(position_[3]));
} else {
if (terminal_has_size_option(terminal_)) {
run_command("set terminal " + terminal_ + " size " +
num2str(position_[2]) + "," +
num2str(position_[3]));
}
}
}

Expand All @@ -217,9 +223,15 @@ namespace matplot::backend {
run_command("set terminal " + terminal_ + " position " +
num2str(position_[0]) + "," + num2str(position_[1]));
}
if (terminal_has_size_option(terminal_)) {
run_command("set terminal " + terminal_ + " size " +
num2str(position_[2]) + "," + num2str(position_[3]));
if (terminal_ == "dumb") {
run_command("set terminal dumb " + num2str(position_[2]) + " " +
num2str(position_[3]));
} else {
if (terminal_has_size_option(terminal_)) {
run_command("set terminal " + terminal_ + " size " +
num2str(position_[2]) + "," +
num2str(position_[3]));
}
}
}

Expand Down Expand Up @@ -318,7 +330,9 @@ namespace matplot::backend {
}

bool gnuplot::terminal_is_available(std::string_view term) {
std::string msg = run_and_get_output("gnuplot -e \"set terminal " + std::string(term.data()) + "\" 2>&1");
std::string msg =
run_and_get_output("gnuplot -e \"set terminal " +
std::string(term.data()) + "\" 2>&1");
return msg.empty();
}

Expand Down
10 changes: 8 additions & 2 deletions source/matplot/core/axes_type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ namespace matplot {
if (any_obj_needs_colormap && !colormap_.empty() &&
!children_.empty()) {
std::stringstream ss;
ss.precision(5);
ss << std::fixed;
// limit the number of colors in the palette
// this is useful for contour plots
if (max_colors_) {
Expand Down Expand Up @@ -525,6 +527,8 @@ namespace matplot {

if (cb_axis_.visible()) {
std::stringstream ss;
ss.precision(5);
ss << std::fixed;
ss << "set colorbox";
if (!cb_axis_.reverse()) {
ss << " noinvert";
Expand Down Expand Up @@ -3310,6 +3314,8 @@ namespace matplot {
std::vector<std::string> pie_labels(x.size());
double theta_sum = 0.; // [0;2pi]
std::stringstream stream;
stream.precision(5);
stream << std::fixed;
for (size_t i = 0; i < x.size(); ++i) {
if (labels.size() > i) {
stream << labels[i];
Expand Down Expand Up @@ -4589,7 +4595,7 @@ namespace matplot {
scale, line_spec);
}

/// Quiver - 2d x,y,u,v with no color mapping
/// Quiver - 2d x,y,u,v with no color mapping
vectors_handle axes_type::quiver(const std::vector<std::vector<double>> &x,
const std::vector<std::vector<double>> &y,
const std::vector<std::vector<double>> &u,
Expand All @@ -4599,7 +4605,7 @@ namespace matplot {
scale, line_spec);
}

/// Quiver - x,y,u,v
/// Quiver - x,y,u,v
vectors_handle axes_type::quiver(const std::vector<double> &x,
const std::vector<double> &y,
const std::vector<double> &u,
Expand Down
Loading

0 comments on commit 3c81acc

Please sign in to comment.