如何在Python中绘制保留特定条形的高分辨率不等分区间直方图?
Got it, let's break down how to get the best of both worlds here.
Your first code uses bins=range(0,50,2) which gives high resolution by grouping frequency values into 2-unit intervals, but it's merging some values together—including the ones that have those large counts (3081 and 4845). The second code with bins=10 uses wider intervals, which happens to keep those high-count values in their own visible bars but sacrifices the fine-grained detail you want.
Since your frequency values are between 1 and 50 (integers, I assume), the simplest fix is to use 1-unit intervals so every individual frequency value gets its own bar. This keeps maximum resolution while ensuring those high-count bars are fully visible.
Here's the code:
# Optional: First check which frequency values correspond to counts 3081 and 4845 frequency_counts = unique_seq_Dataframe.groupby(by="frequency").count() print(frequency_counts) # Draw the high-resolution histogram with all individual bars unique_seq_Dataframe.frequency.hist(bins=range(0, 51, 1))
Why this works:
range(0,51,1) creates intervals like 0-1, 1-2, ..., 49-50. Each integer frequency value (1 to 50) falls into its own interval, so every bar represents exactly one frequency value's count. You'll see those 3081 and 4845 bars clearly, plus all the fine-grained detail from your first plot.
If you find 50 bars too cluttered, you can also create custom bins to keep the high-count values isolated while using 2-unit intervals for the rest. For example, if the high-count values are 15 and 35 (replace with your actual values), do this:
# Custom bins: keep 15 and 35 in their own intervals, others use 2-unit steps custom_bins = list(range(0, 15, 2)) + [15, 16] + list(range(16, 35, 2)) + [35, 36] + list(range(36, 51, 2)) unique_seq_Dataframe.frequency.hist(bins=custom_bins)
But the first approach is the most straightforward since your data range is small (1-50).
内容的提问来源于stack exchange,提问作者Eilay Koren

