Skip to content

Commit a1b3e2a

Browse files
j0hnnybashalandefreitas
authored andcommitted
fix: display polarhistograms with more than 89 bins properly
Change petal resolution to the larger one of (a) 360 points (359 segments) per circle or (b) n_histogram_bins+1 points (n_histogram_bins segments). Ensuring at least one segment per bin petal. Previously polarhistograms had a fixed resolution of 90 points (89 segments) per circle. Specifying more than 89 bins would result in either an empty plot (due to a bug in linspace) or too narrow petals (with this linspace bug fixed).
1 parent 92a9261 commit a1b3e2a

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

source/matplot/axes_objects/histogram.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,10 @@ namespace matplot {
102102

103103
} else {
104104
// resolutions of the petals
105-
constexpr double points_per_circle = 90;
105+
constexpr double points_per_circle = 360;
106106
const double n_histogram_bins = bin_edges_.size() - 1.;
107-
const double points_per_bin = points_per_circle / n_histogram_bins;
107+
// ensure at least one line segment (2 points) per petal.
108+
const size_t points_per_bin = static_cast<size_t>(max(2.0, points_per_circle / n_histogram_bins));
108109
if (!stairs_only_) {
109110
for (size_t i = 0; i < values_.size(); ++i) {
110111
// make a petal for each value
@@ -114,8 +115,7 @@ namespace matplot {
114115
// theta = edge_end rho = 0
115116
ss << " " << bin_edges_[i] << " " << 0 << "\n";
116117
auto arc_between_edges =
117-
linspace(bin_edges_[i], bin_edges_[i + 1],
118-
static_cast<size_t>(ceil(points_per_bin)));
118+
linspace(bin_edges_[i], bin_edges_[i + 1], points_per_bin);
119119
for (size_t j = 0; j < arc_between_edges.size(); ++j) {
120120
ss << " " << arc_between_edges[j] << " "
121121
<< values_[i] << "\n";
@@ -129,8 +129,7 @@ namespace matplot {
129129
// theta = edge_end rho = bin_value
130130
// theta = edge_end rho = next bin value
131131
auto arc_between_edges =
132-
linspace(bin_edges_[i], bin_edges_[i + 1],
133-
static_cast<size_t>(ceil(points_per_bin)));
132+
linspace(bin_edges_[i], bin_edges_[i + 1], points_per_bin);
134133
for (size_t j = 0; j < arc_between_edges.size(); ++j) {
135134
ss << " " << arc_between_edges[j] << " "
136135
<< values_[i] << "\n";
@@ -840,4 +839,4 @@ namespace matplot {
840839
return this->num_bins();
841840
}
842841

843-
} // namespace matplot
842+
} // namespace matplot

0 commit comments

Comments
 (0)