Skip to content

Fix Gnuplot::histogram #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 6, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 31 additions & 13 deletions gplot++.h
Original file line number Diff line number Diff line change
Expand Up @@ -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`,
Expand Down Expand Up @@ -72,7 +74,7 @@
#include <unistd.h>
#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);
Expand Down Expand Up @@ -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<size_t> bins(nbins);
for (const auto &val : values) {
int index = static_cast<int>((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<size_t> 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<int>((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<double>(values.size()));
}

std::stringstream of;
for (size_t i{}; i < nbins; ++i) {
Expand Down