Note
Go to the end to download the full example code.
Histogram Distribution Analysis¶
Histogram calculation and distribution analysis for time series data using different binning strategies and fringe bin removal.
This example demonstrates the Histogram class with four flexible approaches:
Fixed number of bins — Coarse-grained overview with even bin widths
Fixed bins with fringe removal — Remove edge bins to avoid boundary accumulation
Separate bin for each unique value — Fine-grained analysis without binning assumptions
Unique values with fringe removal — Exclude extreme bins for cleaner central distribution
Fringe bin removal is useful when data accumulates unnaturally at measurement boundaries (min/max limits, detection thresholds), creating spurious peaks that can mask the true distribution pattern.
Best for: Understanding data distributions, identifying genuine peaks, and analyzing time series measurements with various histogram strategies.
Overview: Why Histograms Matter¶
Histograms bin time series data to reveal distribution patterns. The histogram can be calculated in 4 ways:
Fixed number of bins — Divides data range into N equal-width bins
Fixed bins + fringe removal — Same, but excludes edge bins
Unique value bins — One bin per unique value (no assumptions about ranges)
Unique values + fringe removal — Fine-grained analysis without edge artifacts
Why fringe bin removal? Some measurements accumulate at boundaries (detection limits, sensor saturation, absolute threshold) creating false peaks that mask the true distribution. Removing these edge bins reveals the genuine signal.
Load data and prepare time series¶
We’ll analyze NEE (CO2 net ecosystem exchange) flux distribution from the example dataset.
import diive as dv
data_df = dv.load_exampledata_parquet()
# Extract flux series for analysis
series = data_df['NEE_CUT_REF_f'].copy()
print(f"Time series shape: {series.shape}")
print(f"Series name: {series.name}")
print(f"Valid values: {series.notna().sum()}, Missing: {series.isna().sum()}")
> Loaded .parquet file
/home/docs/checkouts/diive/checkouts/latest/diive/configs/exampledata/exampledat
a_PARQUET_CH-DAV_FP2022.5_2013-2022_ID20230206154316_30MIN.parquet (0.051
seconds).
Time series shape: (175296,)
Series name: NEE_CUT_REF_f
Valid values: 175296, Missing: 0
Method 1: Fixed number of bins¶
Calculate histogram with specific number of bins (10 bins). Each bin covers equal width across the data range.
hist1 = dv.analysis.Histogram(
series=series,
method='n_bins',
n_bins=10,
ignore_fringe_bins=None
)
print("\n--- Method 1: 10 fixed-width bins ---")
print(hist1.results)
print(f"\nTop 5 peak bins: {hist1.peakbins}")
--- Method 1: 10 fixed-width bins ---
BIN_START_INCL COUNTS
0 -40.8110 12
1 -34.2009 78
2 -27.5908 436
3 -20.9807 2485
4 -14.3706 15239
5 -7.7605 34297
6 -1.1504 112210
7 5.4597 10126
8 12.0698 374
9 18.6799 39
Top 5 peak bins: [-1.1503999999999976, -7.7605, -14.3706, 5.459699999999998, -20.9807]
Method 2: Fixed bins with fringe bin exclusion¶
Same as Method 1 but exclude the first bin and last 2 bins. This is useful when data accumulates at boundaries due to measurement limits.
hist2 = dv.analysis.Histogram(
series=series,
method='n_bins',
n_bins=10,
ignore_fringe_bins=[1, 2]
)
print("\n--- Method 2: 10 bins, excluding first and last 2 bins ---")
print(hist2.results)
print(f"\nTop 5 peak bins: {hist2.peakbins}")
--- Method 2: 10 bins, excluding first and last 2 bins ---
BIN_START_INCL COUNTS
0 -34.2009 78
1 -27.5908 436
2 -20.9807 2485
3 -14.3706 15239
4 -7.7605 34297
5 -1.1504 112210
6 5.4597 10126
Top 5 peak bins: [-1.1503999999999976, -7.7605, -14.3706, 5.459699999999998, -20.9807]
Method 3: Separate bin for each unique value¶
Create one bin per unique value in the dataset. This provides fine-grained distribution without binning assumptions.
hist3 = dv.analysis.Histogram(
series=series,
method='uniques',
ignore_fringe_bins=None
)
print("\n--- Method 3: One bin per unique value (all bins) ---")
print(f"Number of unique values: {len(hist3.results)}")
print("\nFirst 15 bins:")
print(hist3.results.head(15))
print(f"\nTop 5 peak bins: {hist3.peakbins}")
--- Method 3: One bin per unique value (all bins) ---
Number of unique values: 24928
First 15 bins:
BIN_START_INCL COUNTS
0 -40.811 1
1 -39.817 1
2 -38.653 1
3 -37.557 1
4 -37.542 1
5 -37.142 1
6 -36.317 1
7 -35.460 1
8 -34.947 1
9 -34.747 1
10 -34.730 1
11 -34.288 1
12 -33.968 1
13 -33.681 1
14 -33.348 1
Top 5 peak bins: [1.148, 1.241, 1.929, 1.324, 0.765]
Method 4: Unique values with fringe bin exclusion¶
Same as Method 3 but exclude first 8 and last 20 unique value bins. This removes extreme outliers from the distribution analysis.
hist4 = dv.analysis.Histogram(
series=series,
method='uniques',
ignore_fringe_bins=[8, 20]
)
print("\n--- Method 4: Unique value bins, excluding first 8 and last 20 bins ---")
print(f"Number of bins after exclusion: {len(hist4.results)}")
print(hist4.results)
print(f"\nTop 5 peak bins: {hist4.peakbins}")
--- Method 4: Unique value bins, excluding first 8 and last 20 bins ---
Number of bins after exclusion: 24900
BIN_START_INCL COUNTS
0 -34.947 1
1 -34.747 1
2 -34.730 1
3 -34.288 1
4 -33.968 1
... ... ...
24895 19.722 1
24896 19.836 1
24897 19.855 1
24898 19.910 1
24899 20.216 1
[24900 rows x 2 columns]
Top 5 peak bins: [1.148, 1.241, 1.929, 1.632, 0.765]
Interpreting histogram results¶
Understanding what the bin ranges and peaks mean.
print("\n" + "="*60)
print("INTERPRETING HISTOGRAM RESULTS")
print("="*60)
# For Method 3 (all unique value bins)
peak_val = hist3.peakbins[0]
# Find the bin in results to determine width
peak_idx = hist3.results[hist3.results['BIN_START_INCL'] == peak_val].index[0]
if peak_idx + 1 < len(hist3.results):
next_bin = hist3.results['BIN_START_INCL'].iloc[peak_idx + 1]
bin_width = next_bin - peak_val
else:
bin_width = (hist3.results['BIN_START_INCL'].iloc[1] -
hist3.results['BIN_START_INCL'].iloc[0])
peak_count = hist3.results[hist3.results['BIN_START_INCL'] == peak_val]['COUNTS'].values[0]
print(f"\nMethod 3 Analysis (Unique Values):")
print(f" Peak distribution found at bin {peak_val:.4f}")
print(f" - This bin contains {peak_count} counts")
print(f" - Top 5 peak bins: {hist3.peakbins}")
print(f" - Bin width: {bin_width:.4f}")
print(f" - This means the peak bin covers values from {peak_val:.4f} (inclusive)")
print(f" to {peak_val + bin_width:.4f} (exclusive)")
print(f"\nMethod 4 Analysis (Unique Values with Fringe Removal):")
print(f" Removed first 8 and last 20 bins")
print(f" - Bins remaining: {len(hist4.results)} (from {len(hist3.results)} original)")
print(f" - New peak: {hist4.peakbins[0]:.4f}")
print(f" - This excludes extreme values and provides a cleaner view")
print(f" of the central distribution without boundary effects")
============================================================
INTERPRETING HISTOGRAM RESULTS
============================================================
Method 3 Analysis (Unique Values):
Peak distribution found at bin 1.1480
- This bin contains 178 counts
- Top 5 peak bins: [1.148, 1.241, 1.929, 1.324, 0.765]
- Bin width: 0.0010
- This means the peak bin covers values from 1.1480 (inclusive)
to 1.1490 (exclusive)
Method 4 Analysis (Unique Values with Fringe Removal):
Removed first 8 and last 20 bins
- Bins remaining: 24900 (from 24928 original)
- New peak: 1.1480
- This excludes extreme values and provides a cleaner view
of the central distribution without boundary effects
Compare all methods¶
Summary statistics across the different histogram methods.
print("\n" + "="*60)
print("COMPARISON SUMMARY")
print("="*60)
print(f"Original series - Valid: {series.notna().sum()}, Missing: {series.isna().sum()}")
print(f" Mean: {series.mean():.4f}, Median: {series.median():.4f}")
print(f" Std Dev: {series.std():.4f}")
print(f" Min: {series.min():.4f}, Max: {series.max():.4f}")
print(f"\nMethod 1 (10 fixed-width bins): Peak at {hist1.peakbins[0]:.4f}")
print(f"Method 2 (10 bins, fringe excluded): Peak at {hist2.peakbins[0]:.4f}")
print(f"Method 3 (unique values): {len(hist3.results)} bins, Peak at {hist3.peakbins[0]:.4f}")
print(f"Method 4 (unique values, fringe excluded): {len(hist4.results)} bins, Peak at {hist4.peakbins[0]:.4f}")
print(f"\nWhen to use each method:")
print(f" Method 1-2: Quick overview with coarse binning, robust to outliers")
print(f" Method 3-4: Fine-grained analysis, better for identifying true peaks")
============================================================
COMPARISON SUMMARY
============================================================
Original series - Valid: 175296, Missing: 0
Mean: -0.4689, Median: 0.8680
Std Dev: 5.1285
Min: -40.8110, Max: 25.2900
Method 1 (10 fixed-width bins): Peak at -1.1504
Method 2 (10 bins, fringe excluded): Peak at -1.1504
Method 3 (unique values): 24928 bins, Peak at 1.1480
Method 4 (unique values, fringe excluded): 24900 bins, Peak at 1.1480
When to use each method:
Method 1-2: Quick overview with coarse binning, robust to outliers
Method 3-4: Fine-grained analysis, better for identifying true peaks
Total running time of the script: (0 minutes 0.140 seconds)