|
28 | 28 | *
|
29 | 29 | * Version history
|
30 | 30 | *
|
| 31 | + * - 0.9.1 (2025/02/06): bug fixes in method `histogram` |
| 32 | + * |
31 | 33 | * - 0.9.0 (2024/11/19): new method `redirect_to_animated_gif`
|
32 | 34 | *
|
33 | 35 | * - 0.8.0 (2024/10/15): new methods `add_point_xerr`,
|
|
72 | 74 | #include <unistd.h>
|
73 | 75 | #endif
|
74 | 76 |
|
75 |
| -const unsigned GNUPLOTPP_VERSION = 0x000900; |
| 77 | +const unsigned GNUPLOTPP_VERSION = 0x000901; |
76 | 78 | const unsigned GNUPLOTPP_MAJOR_VERSION = (GNUPLOTPP_VERSION & 0xFF0000) >> 16;
|
77 | 79 | const unsigned GNUPLOTPP_MINOR_VERSION = (GNUPLOTPP_VERSION & 0x00FF00) >> 8;
|
78 | 80 | const unsigned GNUPLOTPP_PATCH_VERSION = (GNUPLOTPP_VERSION & 0xFF);
|
@@ -498,18 +500,34 @@ class Gnuplot {
|
498 | 500 | assert(!is_3dplot);
|
499 | 501 | }
|
500 | 502 |
|
501 |
| - double min = *std::min_element(values.begin(), values.end()); |
502 |
| - double max = *std::max_element(values.begin(), values.end()); |
503 |
| - double binwidth = (max - min) / nbins; |
504 |
| - |
505 |
| - std::vector<size_t> bins(nbins); |
506 |
| - for (const auto &val : values) { |
507 |
| - int index = static_cast<int>((val - min) / binwidth); |
508 |
| - if (index >= int(nbins)) |
509 |
| - --index; |
510 |
| - |
511 |
| - bins.at(index)++; |
512 |
| - } |
| 503 | + auto min_iter = std::min_element(values.begin(), values.end()); |
| 504 | + auto max_iter = std::max_element(values.begin(), values.end()); |
| 505 | + double min, max, binwidth; |
| 506 | + |
| 507 | + std::vector<size_t> bins{}; |
| 508 | + |
| 509 | + // Check if all the elements are the same |
| 510 | + if (min_iter != max_iter) { |
| 511 | + min = *min_iter; |
| 512 | + max = *max_iter; |
| 513 | + binwidth = (max - min) / nbins; |
| 514 | + |
| 515 | + bins.resize(nbins); |
| 516 | + for (const auto &val : values) { |
| 517 | + int index = static_cast<int>((val - min) / binwidth); |
| 518 | + if (index >= int(nbins)) |
| 519 | + --index; |
| 520 | + |
| 521 | + bins.at(index)++; |
| 522 | + } |
| 523 | + } else { |
| 524 | + // Just one bin… |
| 525 | + |
| 526 | + min = max = *min_iter; |
| 527 | + binwidth = 1.0; |
| 528 | + nbins = 1; |
| 529 | + bins.push_back(static_cast<double>(values.size())); |
| 530 | + } |
513 | 531 |
|
514 | 532 | std::stringstream of;
|
515 | 533 | for (size_t i{}; i < nbins; ++i) {
|
|
0 commit comments