From 6770a06ec29c8bbfac8412422bb615d8d9757b77 Mon Sep 17 00:00:00 2001 From: Maurizio Tomasi Date: Thu, 6 Feb 2025 08:32:00 +0100 Subject: [PATCH] Fix Gnuplot::histogram --- gplot++.h | 44 +++++++++++++++++++++++++++++++------------- 1 file changed, 31 insertions(+), 13 deletions(-) diff --git a/gplot++.h b/gplot++.h index 2954d75..bf718df 100644 --- a/gplot++.h +++ b/gplot++.h @@ -28,6 +28,8 @@ * * Version history * + * - 0.9.1 (2025/02/06): bug fixes in method `histogram` + * * - 0.9.0 (2024/11/19): new method `redirect_to_animated_gif` * * - 0.8.0 (2024/10/15): new methods `add_point_xerr`, @@ -72,7 +74,7 @@ #include #endif -const unsigned GNUPLOTPP_VERSION = 0x000900; +const unsigned GNUPLOTPP_VERSION = 0x000901; const unsigned GNUPLOTPP_MAJOR_VERSION = (GNUPLOTPP_VERSION & 0xFF0000) >> 16; const unsigned GNUPLOTPP_MINOR_VERSION = (GNUPLOTPP_VERSION & 0x00FF00) >> 8; const unsigned GNUPLOTPP_PATCH_VERSION = (GNUPLOTPP_VERSION & 0xFF); @@ -498,18 +500,34 @@ class Gnuplot { assert(!is_3dplot); } - double min = *std::min_element(values.begin(), values.end()); - double max = *std::max_element(values.begin(), values.end()); - double binwidth = (max - min) / nbins; - - std::vector bins(nbins); - for (const auto &val : values) { - int index = static_cast((val - min) / binwidth); - if (index >= int(nbins)) - --index; - - bins.at(index)++; - } + auto min_iter = std::min_element(values.begin(), values.end()); + auto max_iter = std::max_element(values.begin(), values.end()); + double min, max, binwidth; + + std::vector bins{}; + + // Check if all the elements are the same + if (min_iter != max_iter) { + min = *min_iter; + max = *max_iter; + binwidth = (max - min) / nbins; + + bins.resize(nbins); + for (const auto &val : values) { + int index = static_cast((val - min) / binwidth); + if (index >= int(nbins)) + --index; + + bins.at(index)++; + } + } else { + // Just one bin… + + min = max = *min_iter; + binwidth = 1.0; + nbins = 1; + bins.push_back(static_cast(values.size())); + } std::stringstream of; for (size_t i{}; i < nbins; ++i) {