Skip to content
OpenChart

How to choose a histogram bin size

The same 51 state incomes drawn with three bin widths look like three different distributions. Two rules of thumb help you pick a width.

$50k$70k$90k$110kMedian household income, 202405101520States per bin30 of 50 states have a median income between $70,000 and $90,000Number of states by median household income in $10,000 bins, 50 states and DC$50k$70k$90k$110kMedian household income, 202405101520States per bin30 of 50 states have a median income between $70,000 and $90,000Number of states by median household income in $10,000 bins, 50 states and DC
State median household income in $10,000 bins. Source: US Census Bureau, American Community Survey 1-year estimates.

Made with OpenChart. How to build this bar chart

Do most states have about the same median household income, or are they spread out? A histogram of the 2024 values from the Census Bureau’s American Community Survey can say either, and the width of its bins decides which. A histogram sorts values into bins and draws a bar for each bin’s count, so the same values can look like a lump, a smooth curve or random spikes.

The data covers the 50 states and DC. The lowest is Mississippi at $59,127 and the highest is DC at $109,707.

Too few bins

With wide bins everything lands in two or three bars, and the chart shows only a hump with no detail inside it.

$50k$75k$100k$125kMedian household income, 20240102030States per binWith $25,000 bins, the 50 states and DC fall into 3 barsNumber of states by median household income in $25,000 bins, 50 states and DC$50k$75k$100k$125kMedian household income, 20240102030States per binWith $25,000 bins, the 50 states and DC fall into 3 barsNumber of states by median household income in $25,000 bins, 50 states and DC
State median household income in $25,000 bins. Source: US Census Bureau, American Community Survey 1-year estimates.

Made with OpenChart. How to build this bar chart

{
  "mark": "bar",
  "data": [
    {
      "state": "Alabama",
      "income": 66659
    },
    {
      "state": "Alaska",
      "income": 95665
    },
    {
      "state": "Arizona",
      "income": 81486
    },
    {
      "state": "Arkansas",
      "income": 62106
    },
    {
      "state": "California",
      "income": 100149
    },
    {
      "state": "Colorado",
      "income": 97113
    },
    // … 45 more rows; Copy includes them
  ],
  "transform": [
    {
      "bin": {
        "step": 25000,
        "extent": [
          50000,
          125000
        ]
      },
      "field": "income",
      "as": [
        "from",
        "to"
      ]
    },
    {
      "aggregate": [
        {
          "op": "count",
          "field": "state",
          "as": "states"
        }
      ],
      "groupby": [
        "from",
        "to"
      ]
    }
  ],
  "encoding": {
    "x": {
      "field": "from",
      "type": "quantitative",
      "scale": {
        "domain": [
          40000,
          125000
        ],
        "nice": false
      },
      "axis": {
        "title": "Median household income, 2024",
        "format": "$~s",
        "values": [
          50000,
          75000,
          100000,
          125000
        ]
      }
    },
    "x2": {
      "field": "to",
      "type": "quantitative"
    },
    "y": {
      "field": "states",
      "type": "quantitative",
      "axis": {
        "title": "States per bin",
        "tickCount": 4
      }
    }
  },
  "theme": {
    "colors": {
      "categorical": [
        {
          "light": "#0e7490",
          "dark": "#22a3c4"
        }
      ]
    }
  },
  "chrome": {
    "title": "With $25,000 bins, the 50 states and DC fall into 3 bars",
    "subtitle": "Number of states by median household income in $25,000 bins, 50 states and DC"
  }
}
import { Chart } from "@opendata-ai/openchart-react";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 51 rows; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": "bar",
  "transform": [
    {
      "bin": {
        "step": 25000,
        "extent": [
          50000,
          125000
        ]
      },
      "field": "income",
      "as": [
        "from",
        "to"
      ]
    },
    {
      "aggregate": [
        {
          "op": "count",
          "field": "state",
          "as": "states"
        }
      ],
      "groupby": [
        "from",
        "to"
      ]
    }
  ],
  "encoding": {
    "x": {
      "field": "from",
      "type": "quantitative",
      "scale": {
        "domain": [
          40000,
          125000
        ],
        "nice": false
      },
      "axis": {
        "title": "Median household income, 2024",
        "format": "$~s",
        "values": [
          50000,
          75000,
          100000,
          125000
        ]
      }
    },
    "x2": {
      "field": "to",
      "type": "quantitative"
    },
    "y": {
      "field": "states",
      "type": "quantitative",
      "axis": {
        "title": "States per bin",
        "tickCount": 4
      }
    }
  },
  "theme": {
    "colors": {
      "categorical": [
        {
          "light": "#0e7490",
          "dark": "#22a3c4"
        }
      ]
    }
  },
  "chrome": {
    "title": "With $25,000 bins, the 50 states and DC fall into 3 bars",
    "subtitle": "Number of states by median household income in $25,000 bins, 50 states and DC"
  }
};

export function Example() {
  return <Chart spec={spec} />;
}
<script setup lang="ts">
import { Chart } from "@opendata-ai/openchart-vue";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 51 rows; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": "bar",
  "transform": [
    {
      "bin": {
        "step": 25000,
        "extent": [
          50000,
          125000
        ]
      },
      "field": "income",
      "as": [
        "from",
        "to"
      ]
    },
    {
      "aggregate": [
        {
          "op": "count",
          "field": "state",
          "as": "states"
        }
      ],
      "groupby": [
        "from",
        "to"
      ]
    }
  ],
  "encoding": {
    "x": {
      "field": "from",
      "type": "quantitative",
      "scale": {
        "domain": [
          40000,
          125000
        ],
        "nice": false
      },
      "axis": {
        "title": "Median household income, 2024",
        "format": "$~s",
        "values": [
          50000,
          75000,
          100000,
          125000
        ]
      }
    },
    "x2": {
      "field": "to",
      "type": "quantitative"
    },
    "y": {
      "field": "states",
      "type": "quantitative",
      "axis": {
        "title": "States per bin",
        "tickCount": 4
      }
    }
  },
  "theme": {
    "colors": {
      "categorical": [
        {
          "light": "#0e7490",
          "dark": "#22a3c4"
        }
      ]
    }
  },
  "chrome": {
    "title": "With $25,000 bins, the 50 states and DC fall into 3 bars",
    "subtitle": "Number of states by median household income in $25,000 bins, 50 states and DC"
  }
};
</script>

<template>
  <Chart :spec="spec" />
</template>
<script lang="ts">
import { Chart } from "@opendata-ai/openchart-svelte";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 51 rows; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": "bar",
  "transform": [
    {
      "bin": {
        "step": 25000,
        "extent": [
          50000,
          125000
        ]
      },
      "field": "income",
      "as": [
        "from",
        "to"
      ]
    },
    {
      "aggregate": [
        {
          "op": "count",
          "field": "state",
          "as": "states"
        }
      ],
      "groupby": [
        "from",
        "to"
      ]
    }
  ],
  "encoding": {
    "x": {
      "field": "from",
      "type": "quantitative",
      "scale": {
        "domain": [
          40000,
          125000
        ],
        "nice": false
      },
      "axis": {
        "title": "Median household income, 2024",
        "format": "$~s",
        "values": [
          50000,
          75000,
          100000,
          125000
        ]
      }
    },
    "x2": {
      "field": "to",
      "type": "quantitative"
    },
    "y": {
      "field": "states",
      "type": "quantitative",
      "axis": {
        "title": "States per bin",
        "tickCount": 4
      }
    }
  },
  "theme": {
    "colors": {
      "categorical": [
        {
          "light": "#0e7490",
          "dark": "#22a3c4"
        }
      ]
    }
  },
  "chrome": {
    "title": "With $25,000 bins, the 50 states and DC fall into 3 bars",
    "subtitle": "Number of states by median household income in $25,000 bins, 50 states and DC"
  }
};
</script>

<Chart {spec} />
import { createChart } from "@opendata-ai/openchart-vanilla";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 51 rows; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": "bar",
  "transform": [
    {
      "bin": {
        "step": 25000,
        "extent": [
          50000,
          125000
        ]
      },
      "field": "income",
      "as": [
        "from",
        "to"
      ]
    },
    {
      "aggregate": [
        {
          "op": "count",
          "field": "state",
          "as": "states"
        }
      ],
      "groupby": [
        "from",
        "to"
      ]
    }
  ],
  "encoding": {
    "x": {
      "field": "from",
      "type": "quantitative",
      "scale": {
        "domain": [
          40000,
          125000
        ],
        "nice": false
      },
      "axis": {
        "title": "Median household income, 2024",
        "format": "$~s",
        "values": [
          50000,
          75000,
          100000,
          125000
        ]
      }
    },
    "x2": {
      "field": "to",
      "type": "quantitative"
    },
    "y": {
      "field": "states",
      "type": "quantitative",
      "axis": {
        "title": "States per bin",
        "tickCount": 4
      }
    }
  },
  "theme": {
    "colors": {
      "categorical": [
        {
          "light": "#0e7490",
          "dark": "#22a3c4"
        }
      ]
    }
  },
  "chrome": {
    "title": "With $25,000 bins, the 50 states and DC fall into 3 bars",
    "subtitle": "Number of states by median household income in $25,000 bins, 50 states and DC"
  }
};

const container = document.getElementById("chart");
if (!container) throw new Error('Add <div id="chart"></div> to the page first.');
createChart(container, spec);

The chart puts most states between $75,000 and $100,000 but cannot show whether those values bunch together or spread out.

Too many bins

With narrow bins most bars hold one or two states, and the outline jumps around from one bar to the next.

$50k$70k$90k$110kMedian household income, 202401234States per binWith $1,000 bins there are 27 bars, and none holds more than 4statesNumber of states by median household income in $1,000 bins, 50 states and DC$50k$70k$90k$110kMedian household income, 202401234States per binWith $1,000 bins there are 27 bars, and none holds more than 4statesNumber of states by median household income in $1,000 bins, 50 states and DC
State median household income in $1,000 bins. Source: US Census Bureau, American Community Survey 1-year estimates.

Made with OpenChart. How to build this bar chart

{
  "mark": "bar",
  "data": [
    {
      "state": "Alabama",
      "income": 66659
    },
    {
      "state": "Alaska",
      "income": 95665
    },
    {
      "state": "Arizona",
      "income": 81486
    },
    {
      "state": "Arkansas",
      "income": 62106
    },
    {
      "state": "California",
      "income": 100149
    },
    {
      "state": "Colorado",
      "income": 97113
    },
    // … 45 more rows; Copy includes them
  ],
  "transform": [
    {
      "bin": {
        "step": 1000,
        "extent": [
          50000,
          125000
        ]
      },
      "field": "income",
      "as": [
        "from",
        "to"
      ]
    },
    {
      "aggregate": [
        {
          "op": "count",
          "field": "state",
          "as": "states"
        }
      ],
      "groupby": [
        "from",
        "to"
      ]
    }
  ],
  "encoding": {
    "x": {
      "field": "from",
      "type": "quantitative",
      "scale": {
        "domain": [
          40000,
          125000
        ],
        "nice": false
      },
      "axis": {
        "title": "Median household income, 2024",
        "format": "$~s",
        "values": [
          50000,
          70000,
          90000,
          110000
        ]
      }
    },
    "x2": {
      "field": "to",
      "type": "quantitative"
    },
    "y": {
      "field": "states",
      "type": "quantitative",
      "axis": {
        "title": "States per bin",
        "tickCount": 4
      }
    }
  },
  "theme": {
    "colors": {
      "categorical": [
        {
          "light": "#0e7490",
          "dark": "#22a3c4"
        }
      ]
    }
  },
  "chrome": {
    "title": "With $1,000 bins there are 27 bars, and none holds more than 4 states",
    "subtitle": "Number of states by median household income in $1,000 bins, 50 states and DC"
  }
}
import { Chart } from "@opendata-ai/openchart-react";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 51 rows; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": "bar",
  "transform": [
    {
      "bin": {
        "step": 1000,
        "extent": [
          50000,
          125000
        ]
      },
      "field": "income",
      "as": [
        "from",
        "to"
      ]
    },
    {
      "aggregate": [
        {
          "op": "count",
          "field": "state",
          "as": "states"
        }
      ],
      "groupby": [
        "from",
        "to"
      ]
    }
  ],
  "encoding": {
    "x": {
      "field": "from",
      "type": "quantitative",
      "scale": {
        "domain": [
          40000,
          125000
        ],
        "nice": false
      },
      "axis": {
        "title": "Median household income, 2024",
        "format": "$~s",
        "values": [
          50000,
          70000,
          90000,
          110000
        ]
      }
    },
    "x2": {
      "field": "to",
      "type": "quantitative"
    },
    "y": {
      "field": "states",
      "type": "quantitative",
      "axis": {
        "title": "States per bin",
        "tickCount": 4
      }
    }
  },
  "theme": {
    "colors": {
      "categorical": [
        {
          "light": "#0e7490",
          "dark": "#22a3c4"
        }
      ]
    }
  },
  "chrome": {
    "title": "With $1,000 bins there are 27 bars, and none holds more than 4 states",
    "subtitle": "Number of states by median household income in $1,000 bins, 50 states and DC"
  }
};

export function Example() {
  return <Chart spec={spec} />;
}
<script setup lang="ts">
import { Chart } from "@opendata-ai/openchart-vue";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 51 rows; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": "bar",
  "transform": [
    {
      "bin": {
        "step": 1000,
        "extent": [
          50000,
          125000
        ]
      },
      "field": "income",
      "as": [
        "from",
        "to"
      ]
    },
    {
      "aggregate": [
        {
          "op": "count",
          "field": "state",
          "as": "states"
        }
      ],
      "groupby": [
        "from",
        "to"
      ]
    }
  ],
  "encoding": {
    "x": {
      "field": "from",
      "type": "quantitative",
      "scale": {
        "domain": [
          40000,
          125000
        ],
        "nice": false
      },
      "axis": {
        "title": "Median household income, 2024",
        "format": "$~s",
        "values": [
          50000,
          70000,
          90000,
          110000
        ]
      }
    },
    "x2": {
      "field": "to",
      "type": "quantitative"
    },
    "y": {
      "field": "states",
      "type": "quantitative",
      "axis": {
        "title": "States per bin",
        "tickCount": 4
      }
    }
  },
  "theme": {
    "colors": {
      "categorical": [
        {
          "light": "#0e7490",
          "dark": "#22a3c4"
        }
      ]
    }
  },
  "chrome": {
    "title": "With $1,000 bins there are 27 bars, and none holds more than 4 states",
    "subtitle": "Number of states by median household income in $1,000 bins, 50 states and DC"
  }
};
</script>

<template>
  <Chart :spec="spec" />
</template>
<script lang="ts">
import { Chart } from "@opendata-ai/openchart-svelte";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 51 rows; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": "bar",
  "transform": [
    {
      "bin": {
        "step": 1000,
        "extent": [
          50000,
          125000
        ]
      },
      "field": "income",
      "as": [
        "from",
        "to"
      ]
    },
    {
      "aggregate": [
        {
          "op": "count",
          "field": "state",
          "as": "states"
        }
      ],
      "groupby": [
        "from",
        "to"
      ]
    }
  ],
  "encoding": {
    "x": {
      "field": "from",
      "type": "quantitative",
      "scale": {
        "domain": [
          40000,
          125000
        ],
        "nice": false
      },
      "axis": {
        "title": "Median household income, 2024",
        "format": "$~s",
        "values": [
          50000,
          70000,
          90000,
          110000
        ]
      }
    },
    "x2": {
      "field": "to",
      "type": "quantitative"
    },
    "y": {
      "field": "states",
      "type": "quantitative",
      "axis": {
        "title": "States per bin",
        "tickCount": 4
      }
    }
  },
  "theme": {
    "colors": {
      "categorical": [
        {
          "light": "#0e7490",
          "dark": "#22a3c4"
        }
      ]
    }
  },
  "chrome": {
    "title": "With $1,000 bins there are 27 bars, and none holds more than 4 states",
    "subtitle": "Number of states by median household income in $1,000 bins, 50 states and DC"
  }
};
</script>

<Chart {spec} />
import { createChart } from "@opendata-ai/openchart-vanilla";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 51 rows; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": "bar",
  "transform": [
    {
      "bin": {
        "step": 1000,
        "extent": [
          50000,
          125000
        ]
      },
      "field": "income",
      "as": [
        "from",
        "to"
      ]
    },
    {
      "aggregate": [
        {
          "op": "count",
          "field": "state",
          "as": "states"
        }
      ],
      "groupby": [
        "from",
        "to"
      ]
    }
  ],
  "encoding": {
    "x": {
      "field": "from",
      "type": "quantitative",
      "scale": {
        "domain": [
          40000,
          125000
        ],
        "nice": false
      },
      "axis": {
        "title": "Median household income, 2024",
        "format": "$~s",
        "values": [
          50000,
          70000,
          90000,
          110000
        ]
      }
    },
    "x2": {
      "field": "to",
      "type": "quantitative"
    },
    "y": {
      "field": "states",
      "type": "quantitative",
      "axis": {
        "title": "States per bin",
        "tickCount": 4
      }
    }
  },
  "theme": {
    "colors": {
      "categorical": [
        {
          "light": "#0e7490",
          "dark": "#22a3c4"
        }
      ]
    }
  },
  "chrome": {
    "title": "With $1,000 bins there are 27 bars, and none holds more than 4 states",
    "subtitle": "Number of states by median household income in $1,000 bins, 50 states and DC"
  }
};

const container = document.getElementById("chart");
if (!container) throw new Error('Add <div id="chart"></div> to the page first.');
createChart(container, spec);

A reader will see peaks and gaps here, but each is one or two states, and moving a state by $1,000 would reshuffle them.

$10,000 bins

A $10,000 bin gives six bars. The values pile up between $70,000 and $90,000, and a thinner tail runs to the right.

$50k$70k$90k$110kMedian household income, 202405101520States per bin30 of 50 states have a median income between $70,000 and $90,000Number of states by median household income in $10,000 bins, 50 states and DC$50k$70k$90k$110kMedian household income, 202405101520States per bin30 of 50 states have a median income between $70,000 and $90,000Number of states by median household income in $10,000 bins, 50 states and DC
Live example. Source: US Census Bureau, American Community Survey 1-year estimates.

Made with OpenChart. How to build this bar chart

{
  "mark": "bar",
  "data": [
    {
      "state": "Alabama",
      "income": 66659
    },
    {
      "state": "Alaska",
      "income": 95665
    },
    {
      "state": "Arizona",
      "income": 81486
    },
    {
      "state": "Arkansas",
      "income": 62106
    },
    {
      "state": "California",
      "income": 100149
    },
    {
      "state": "Colorado",
      "income": 97113
    },
    // … 45 more rows; Copy includes them
  ],
  "transform": [
    {
      "bin": {
        "step": 10000,
        "extent": [
          50000,
          125000
        ]
      },
      "field": "income",
      "as": [
        "from",
        "to"
      ]
    },
    {
      "aggregate": [
        {
          "op": "count",
          "field": "state",
          "as": "states"
        }
      ],
      "groupby": [
        "from",
        "to"
      ]
    }
  ],
  "encoding": {
    "x": {
      "field": "from",
      "type": "quantitative",
      "scale": {
        "domain": [
          40000,
          125000
        ],
        "nice": false
      },
      "axis": {
        "title": "Median household income, 2024",
        "format": "$~s",
        "values": [
          50000,
          70000,
          90000,
          110000
        ]
      }
    },
    "x2": {
      "field": "to",
      "type": "quantitative"
    },
    "y": {
      "field": "states",
      "type": "quantitative",
      "axis": {
        "title": "States per bin",
        "tickCount": 4
      }
    }
  },
  "theme": {
    "colors": {
      "categorical": [
        {
          "light": "#0e7490",
          "dark": "#22a3c4"
        }
      ]
    }
  },
  "chrome": {
    "title": "30 of 50 states have a median income between $70,000 and $90,000",
    "subtitle": "Number of states by median household income in $10,000 bins, 50 states and DC"
  }
}
import { Chart } from "@opendata-ai/openchart-react";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 51 rows; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": "bar",
  "transform": [
    {
      "bin": {
        "step": 10000,
        "extent": [
          50000,
          125000
        ]
      },
      "field": "income",
      "as": [
        "from",
        "to"
      ]
    },
    {
      "aggregate": [
        {
          "op": "count",
          "field": "state",
          "as": "states"
        }
      ],
      "groupby": [
        "from",
        "to"
      ]
    }
  ],
  "encoding": {
    "x": {
      "field": "from",
      "type": "quantitative",
      "scale": {
        "domain": [
          40000,
          125000
        ],
        "nice": false
      },
      "axis": {
        "title": "Median household income, 2024",
        "format": "$~s",
        "values": [
          50000,
          70000,
          90000,
          110000
        ]
      }
    },
    "x2": {
      "field": "to",
      "type": "quantitative"
    },
    "y": {
      "field": "states",
      "type": "quantitative",
      "axis": {
        "title": "States per bin",
        "tickCount": 4
      }
    }
  },
  "theme": {
    "colors": {
      "categorical": [
        {
          "light": "#0e7490",
          "dark": "#22a3c4"
        }
      ]
    }
  },
  "chrome": {
    "title": "30 of 50 states have a median income between $70,000 and $90,000",
    "subtitle": "Number of states by median household income in $10,000 bins, 50 states and DC"
  }
};

export function Example() {
  return <Chart spec={spec} />;
}
<script setup lang="ts">
import { Chart } from "@opendata-ai/openchart-vue";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 51 rows; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": "bar",
  "transform": [
    {
      "bin": {
        "step": 10000,
        "extent": [
          50000,
          125000
        ]
      },
      "field": "income",
      "as": [
        "from",
        "to"
      ]
    },
    {
      "aggregate": [
        {
          "op": "count",
          "field": "state",
          "as": "states"
        }
      ],
      "groupby": [
        "from",
        "to"
      ]
    }
  ],
  "encoding": {
    "x": {
      "field": "from",
      "type": "quantitative",
      "scale": {
        "domain": [
          40000,
          125000
        ],
        "nice": false
      },
      "axis": {
        "title": "Median household income, 2024",
        "format": "$~s",
        "values": [
          50000,
          70000,
          90000,
          110000
        ]
      }
    },
    "x2": {
      "field": "to",
      "type": "quantitative"
    },
    "y": {
      "field": "states",
      "type": "quantitative",
      "axis": {
        "title": "States per bin",
        "tickCount": 4
      }
    }
  },
  "theme": {
    "colors": {
      "categorical": [
        {
          "light": "#0e7490",
          "dark": "#22a3c4"
        }
      ]
    }
  },
  "chrome": {
    "title": "30 of 50 states have a median income between $70,000 and $90,000",
    "subtitle": "Number of states by median household income in $10,000 bins, 50 states and DC"
  }
};
</script>

<template>
  <Chart :spec="spec" />
</template>
<script lang="ts">
import { Chart } from "@opendata-ai/openchart-svelte";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 51 rows; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": "bar",
  "transform": [
    {
      "bin": {
        "step": 10000,
        "extent": [
          50000,
          125000
        ]
      },
      "field": "income",
      "as": [
        "from",
        "to"
      ]
    },
    {
      "aggregate": [
        {
          "op": "count",
          "field": "state",
          "as": "states"
        }
      ],
      "groupby": [
        "from",
        "to"
      ]
    }
  ],
  "encoding": {
    "x": {
      "field": "from",
      "type": "quantitative",
      "scale": {
        "domain": [
          40000,
          125000
        ],
        "nice": false
      },
      "axis": {
        "title": "Median household income, 2024",
        "format": "$~s",
        "values": [
          50000,
          70000,
          90000,
          110000
        ]
      }
    },
    "x2": {
      "field": "to",
      "type": "quantitative"
    },
    "y": {
      "field": "states",
      "type": "quantitative",
      "axis": {
        "title": "States per bin",
        "tickCount": 4
      }
    }
  },
  "theme": {
    "colors": {
      "categorical": [
        {
          "light": "#0e7490",
          "dark": "#22a3c4"
        }
      ]
    }
  },
  "chrome": {
    "title": "30 of 50 states have a median income between $70,000 and $90,000",
    "subtitle": "Number of states by median household income in $10,000 bins, 50 states and DC"
  }
};
</script>

<Chart {spec} />
import { createChart } from "@opendata-ai/openchart-vanilla";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 51 rows; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": "bar",
  "transform": [
    {
      "bin": {
        "step": 10000,
        "extent": [
          50000,
          125000
        ]
      },
      "field": "income",
      "as": [
        "from",
        "to"
      ]
    },
    {
      "aggregate": [
        {
          "op": "count",
          "field": "state",
          "as": "states"
        }
      ],
      "groupby": [
        "from",
        "to"
      ]
    }
  ],
  "encoding": {
    "x": {
      "field": "from",
      "type": "quantitative",
      "scale": {
        "domain": [
          40000,
          125000
        ],
        "nice": false
      },
      "axis": {
        "title": "Median household income, 2024",
        "format": "$~s",
        "values": [
          50000,
          70000,
          90000,
          110000
        ]
      }
    },
    "x2": {
      "field": "to",
      "type": "quantitative"
    },
    "y": {
      "field": "states",
      "type": "quantitative",
      "axis": {
        "title": "States per bin",
        "tickCount": 4
      }
    }
  },
  "theme": {
    "colors": {
      "categorical": [
        {
          "light": "#0e7490",
          "dark": "#22a3c4"
        }
      ]
    }
  },
  "chrome": {
    "title": "30 of 50 states have a median income between $70,000 and $90,000",
    "subtitle": "Number of states by median household income in $10,000 bins, 50 states and DC"
  }
};

const container = document.getElementById("chart");
if (!container) throw new Error('Add <div id="chart"></div> to the page first.');
createChart(container, spec);

Sturges and Freedman-Diaconis rules

Two rules compute a starting width from the data. Sturges’ rule uses only the count of values: take the base-2 logarithm of 51, add one and round up, which gives 7 bins, or about $7,200 each across this range. The Freedman-Diaconis rule sets the width to twice the spread of the middle half of the values (the interquartile range) divided by the cube root of the count. Because it ignores the extremes, an outlier like DC barely moves it. It gives about $9,400.

Both rules land near the $10,000 width. Keep every bin the same width: the eye reads a bar’s area while the count sets only its height, so bars of unequal width stop meaning the same thing. Try a width on either side of the suggestion and see whether the picture changes.

Checking a feature at a second bin width

Redraw the chart at another width to tell a real feature from an artifact of the bin edges. Here is the USGS earthquake catalog for January 2026, counted in 0.1-magnitude bins, with only the quakes of magnitude 2.5 and above.

34567Magnitude020406080100120Quakes per binM3.98 quakesQuake counts drop to 8 at magnitude 3.9 and rise to 94 at 4.5Magnitude 2.5+ earthquakes in 0.1 magnitude bins, 1,584 quakes, January 202634567Magnitude020406080100120Quakes per binM3.98 quakesQuake counts drop to 8 at magnitude 3.9 and rise to 94 at 4.5Magnitude 2.5+ earthquakes in 0.1 magnitude bins, 1,584 quakes, January 2026
Earthquake counts in 0.1 magnitude bins. Source: USGS Earthquake Catalog, magnitude 2.5+, January 2026 snapshot.

Made with OpenChart. How to build this bar chart

{
  "mark": "bar",
  "data": [
    {
      "magnitude_bin": 2.5,
      "quakes": 119,
      "magnitude_to": 2.6
    },
    {
      "magnitude_bin": 2.6,
      "quakes": 96,
      "magnitude_to": 2.7
    },
    {
      "magnitude_bin": 2.7,
      "quakes": 111,
      "magnitude_to": 2.8
    },
    {
      "magnitude_bin": 2.8,
      "quakes": 85,
      "magnitude_to": 2.9
    },
    {
      "magnitude_bin": 2.9,
      "quakes": 63,
      "magnitude_to": 3
    },
    {
      "magnitude_bin": 3,
      "quakes": 71,
      "magnitude_to": 3.1
    },
    // … 34 more rows; Copy includes them
  ],
  "encoding": {
    "x": {
      "field": "magnitude_bin",
      "type": "quantitative",
      "scale": {
        "domain": [
          2.5,
          7
        ],
        "nice": false
      },
      "axis": {
        "title": "Magnitude",
        "values": [
          3,
          4,
          5,
          6,
          7
        ]
      }
    },
    "x2": {
      "field": "magnitude_to",
      "type": "quantitative"
    },
    "y": {
      "field": "quakes",
      "type": "quantitative",
      "axis": {
        "title": "Quakes per bin",
        "tickCount": 4
      }
    }
  },
  "annotations": [
    {
      "type": "text",
      "x": 3.95,
      "y": 8,
      "text": "**M3.9**\n8 quakes",
      "anchor": "top",
      "offset": {
        "dx": 0,
        "dy": -110
      },
      "connector": "curve"
    }
  ],
  "theme": {
    "colors": {
      "categorical": [
        {
          "light": "#0e7490",
          "dark": "#22a3c4"
        }
      ]
    }
  },
  "chrome": {
    "title": "Quake counts drop to 8 at magnitude 3.9 and rise to 94 at 4.5",
    "subtitle": "Magnitude 2.5+ earthquakes in 0.1 magnitude bins, 1,584 quakes, January 2026"
  }
}
import { Chart } from "@opendata-ai/openchart-react";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 40 rows; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": "bar",
  "encoding": {
    "x": {
      "field": "magnitude_bin",
      "type": "quantitative",
      "scale": {
        "domain": [
          2.5,
          7
        ],
        "nice": false
      },
      "axis": {
        "title": "Magnitude",
        "values": [
          3,
          4,
          5,
          6,
          7
        ]
      }
    },
    "x2": {
      "field": "magnitude_to",
      "type": "quantitative"
    },
    "y": {
      "field": "quakes",
      "type": "quantitative",
      "axis": {
        "title": "Quakes per bin",
        "tickCount": 4
      }
    }
  },
  "annotations": [
    {
      "type": "text",
      "x": 3.95,
      "y": 8,
      "text": "**M3.9**\n8 quakes",
      "anchor": "top",
      "offset": {
        "dx": 0,
        "dy": -110
      },
      "connector": "curve"
    }
  ],
  "theme": {
    "colors": {
      "categorical": [
        {
          "light": "#0e7490",
          "dark": "#22a3c4"
        }
      ]
    }
  },
  "chrome": {
    "title": "Quake counts drop to 8 at magnitude 3.9 and rise to 94 at 4.5",
    "subtitle": "Magnitude 2.5+ earthquakes in 0.1 magnitude bins, 1,584 quakes, January 2026"
  }
};

export function Example() {
  return <Chart spec={spec} />;
}
<script setup lang="ts">
import { Chart } from "@opendata-ai/openchart-vue";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 40 rows; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": "bar",
  "encoding": {
    "x": {
      "field": "magnitude_bin",
      "type": "quantitative",
      "scale": {
        "domain": [
          2.5,
          7
        ],
        "nice": false
      },
      "axis": {
        "title": "Magnitude",
        "values": [
          3,
          4,
          5,
          6,
          7
        ]
      }
    },
    "x2": {
      "field": "magnitude_to",
      "type": "quantitative"
    },
    "y": {
      "field": "quakes",
      "type": "quantitative",
      "axis": {
        "title": "Quakes per bin",
        "tickCount": 4
      }
    }
  },
  "annotations": [
    {
      "type": "text",
      "x": 3.95,
      "y": 8,
      "text": "**M3.9**\n8 quakes",
      "anchor": "top",
      "offset": {
        "dx": 0,
        "dy": -110
      },
      "connector": "curve"
    }
  ],
  "theme": {
    "colors": {
      "categorical": [
        {
          "light": "#0e7490",
          "dark": "#22a3c4"
        }
      ]
    }
  },
  "chrome": {
    "title": "Quake counts drop to 8 at magnitude 3.9 and rise to 94 at 4.5",
    "subtitle": "Magnitude 2.5+ earthquakes in 0.1 magnitude bins, 1,584 quakes, January 2026"
  }
};
</script>

<template>
  <Chart :spec="spec" />
</template>
<script lang="ts">
import { Chart } from "@opendata-ai/openchart-svelte";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 40 rows; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": "bar",
  "encoding": {
    "x": {
      "field": "magnitude_bin",
      "type": "quantitative",
      "scale": {
        "domain": [
          2.5,
          7
        ],
        "nice": false
      },
      "axis": {
        "title": "Magnitude",
        "values": [
          3,
          4,
          5,
          6,
          7
        ]
      }
    },
    "x2": {
      "field": "magnitude_to",
      "type": "quantitative"
    },
    "y": {
      "field": "quakes",
      "type": "quantitative",
      "axis": {
        "title": "Quakes per bin",
        "tickCount": 4
      }
    }
  },
  "annotations": [
    {
      "type": "text",
      "x": 3.95,
      "y": 8,
      "text": "**M3.9**\n8 quakes",
      "anchor": "top",
      "offset": {
        "dx": 0,
        "dy": -110
      },
      "connector": "curve"
    }
  ],
  "theme": {
    "colors": {
      "categorical": [
        {
          "light": "#0e7490",
          "dark": "#22a3c4"
        }
      ]
    }
  },
  "chrome": {
    "title": "Quake counts drop to 8 at magnitude 3.9 and rise to 94 at 4.5",
    "subtitle": "Magnitude 2.5+ earthquakes in 0.1 magnitude bins, 1,584 quakes, January 2026"
  }
};
</script>

<Chart {spec} />
import { createChart } from "@opendata-ai/openchart-vanilla";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 40 rows; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": "bar",
  "encoding": {
    "x": {
      "field": "magnitude_bin",
      "type": "quantitative",
      "scale": {
        "domain": [
          2.5,
          7
        ],
        "nice": false
      },
      "axis": {
        "title": "Magnitude",
        "values": [
          3,
          4,
          5,
          6,
          7
        ]
      }
    },
    "x2": {
      "field": "magnitude_to",
      "type": "quantitative"
    },
    "y": {
      "field": "quakes",
      "type": "quantitative",
      "axis": {
        "title": "Quakes per bin",
        "tickCount": 4
      }
    }
  },
  "annotations": [
    {
      "type": "text",
      "x": 3.95,
      "y": 8,
      "text": "**M3.9**\n8 quakes",
      "anchor": "top",
      "offset": {
        "dx": 0,
        "dy": -110
      },
      "connector": "curve"
    }
  ],
  "theme": {
    "colors": {
      "categorical": [
        {
          "light": "#0e7490",
          "dark": "#22a3c4"
        }
      ]
    }
  },
  "chrome": {
    "title": "Quake counts drop to 8 at magnitude 3.9 and rise to 94 at 4.5",
    "subtitle": "Magnitude 2.5+ earthquakes in 0.1 magnitude bins, 1,584 quakes, January 2026"
  }
};

const container = document.getElementById("chart");
if (!container) throw new Error('Add <div id="chart"></div> to the page first.');
createChart(container, spec);

Small quakes are far more common than big ones, so counts should fall steadily with magnitude. Instead they dip to 8 at magnitude 3.9 and climb back by 4.5.

34567Magnitude0100200300400500Quakes per binThe dip is still there in half-magnitude binsMagnitude 2.5+ earthquakes in 0.5 magnitude bins, 1,584 quakes, January 202634567Magnitude0100200300400500Quakes per binThe dip is still there in half-magnitude binsMagnitude 2.5+ earthquakes in 0.5 magnitude bins, 1,584 quakes, January 2026
Earthquake counts in 0.5 magnitude bins. Source: USGS Earthquake Catalog, magnitude 2.5+, January 2026 snapshot.

Made with OpenChart. How to build this bar chart

{
  "mark": "bar",
  "data": [
    {
      "magnitude_bin": 2.5,
      "quakes": 119,
      "magnitude_to": 2.6
    },
    {
      "magnitude_bin": 2.6,
      "quakes": 96,
      "magnitude_to": 2.7
    },
    {
      "magnitude_bin": 2.7,
      "quakes": 111,
      "magnitude_to": 2.8
    },
    {
      "magnitude_bin": 2.8,
      "quakes": 85,
      "magnitude_to": 2.9
    },
    {
      "magnitude_bin": 2.9,
      "quakes": 63,
      "magnitude_to": 3
    },
    {
      "magnitude_bin": 3,
      "quakes": 71,
      "magnitude_to": 3.1
    },
    // … 34 more rows; Copy includes them
  ],
  "transform": [
    {
      "bin": {
        "step": 0.5,
        "extent": [
          2.5,
          7
        ]
      },
      "field": "magnitude_bin",
      "as": [
        "from",
        "to"
      ]
    },
    {
      "aggregate": [
        {
          "op": "sum",
          "field": "quakes",
          "as": "count"
        }
      ],
      "groupby": [
        "from",
        "to"
      ]
    }
  ],
  "encoding": {
    "x": {
      "field": "from",
      "type": "quantitative",
      "scale": {
        "domain": [
          2.5,
          7
        ],
        "nice": false
      },
      "axis": {
        "title": "Magnitude",
        "values": [
          3,
          4,
          5,
          6,
          7
        ]
      }
    },
    "x2": {
      "field": "to",
      "type": "quantitative"
    },
    "y": {
      "field": "count",
      "type": "quantitative",
      "axis": {
        "title": "Quakes per bin",
        "tickCount": 4
      }
    }
  },
  "theme": {
    "colors": {
      "categorical": [
        {
          "light": "#0e7490",
          "dark": "#22a3c4"
        }
      ]
    }
  },
  "chrome": {
    "title": "The dip is still there in half-magnitude bins",
    "subtitle": "Magnitude 2.5+ earthquakes in 0.5 magnitude bins, 1,584 quakes, January 2026"
  }
}
import { Chart } from "@opendata-ai/openchart-react";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 40 rows; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": "bar",
  "transform": [
    {
      "bin": {
        "step": 0.5,
        "extent": [
          2.5,
          7
        ]
      },
      "field": "magnitude_bin",
      "as": [
        "from",
        "to"
      ]
    },
    {
      "aggregate": [
        {
          "op": "sum",
          "field": "quakes",
          "as": "count"
        }
      ],
      "groupby": [
        "from",
        "to"
      ]
    }
  ],
  "encoding": {
    "x": {
      "field": "from",
      "type": "quantitative",
      "scale": {
        "domain": [
          2.5,
          7
        ],
        "nice": false
      },
      "axis": {
        "title": "Magnitude",
        "values": [
          3,
          4,
          5,
          6,
          7
        ]
      }
    },
    "x2": {
      "field": "to",
      "type": "quantitative"
    },
    "y": {
      "field": "count",
      "type": "quantitative",
      "axis": {
        "title": "Quakes per bin",
        "tickCount": 4
      }
    }
  },
  "theme": {
    "colors": {
      "categorical": [
        {
          "light": "#0e7490",
          "dark": "#22a3c4"
        }
      ]
    }
  },
  "chrome": {
    "title": "The dip is still there in half-magnitude bins",
    "subtitle": "Magnitude 2.5+ earthquakes in 0.5 magnitude bins, 1,584 quakes, January 2026"
  }
};

export function Example() {
  return <Chart spec={spec} />;
}
<script setup lang="ts">
import { Chart } from "@opendata-ai/openchart-vue";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 40 rows; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": "bar",
  "transform": [
    {
      "bin": {
        "step": 0.5,
        "extent": [
          2.5,
          7
        ]
      },
      "field": "magnitude_bin",
      "as": [
        "from",
        "to"
      ]
    },
    {
      "aggregate": [
        {
          "op": "sum",
          "field": "quakes",
          "as": "count"
        }
      ],
      "groupby": [
        "from",
        "to"
      ]
    }
  ],
  "encoding": {
    "x": {
      "field": "from",
      "type": "quantitative",
      "scale": {
        "domain": [
          2.5,
          7
        ],
        "nice": false
      },
      "axis": {
        "title": "Magnitude",
        "values": [
          3,
          4,
          5,
          6,
          7
        ]
      }
    },
    "x2": {
      "field": "to",
      "type": "quantitative"
    },
    "y": {
      "field": "count",
      "type": "quantitative",
      "axis": {
        "title": "Quakes per bin",
        "tickCount": 4
      }
    }
  },
  "theme": {
    "colors": {
      "categorical": [
        {
          "light": "#0e7490",
          "dark": "#22a3c4"
        }
      ]
    }
  },
  "chrome": {
    "title": "The dip is still there in half-magnitude bins",
    "subtitle": "Magnitude 2.5+ earthquakes in 0.5 magnitude bins, 1,584 quakes, January 2026"
  }
};
</script>

<template>
  <Chart :spec="spec" />
</template>
<script lang="ts">
import { Chart } from "@opendata-ai/openchart-svelte";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 40 rows; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": "bar",
  "transform": [
    {
      "bin": {
        "step": 0.5,
        "extent": [
          2.5,
          7
        ]
      },
      "field": "magnitude_bin",
      "as": [
        "from",
        "to"
      ]
    },
    {
      "aggregate": [
        {
          "op": "sum",
          "field": "quakes",
          "as": "count"
        }
      ],
      "groupby": [
        "from",
        "to"
      ]
    }
  ],
  "encoding": {
    "x": {
      "field": "from",
      "type": "quantitative",
      "scale": {
        "domain": [
          2.5,
          7
        ],
        "nice": false
      },
      "axis": {
        "title": "Magnitude",
        "values": [
          3,
          4,
          5,
          6,
          7
        ]
      }
    },
    "x2": {
      "field": "to",
      "type": "quantitative"
    },
    "y": {
      "field": "count",
      "type": "quantitative",
      "axis": {
        "title": "Quakes per bin",
        "tickCount": 4
      }
    }
  },
  "theme": {
    "colors": {
      "categorical": [
        {
          "light": "#0e7490",
          "dark": "#22a3c4"
        }
      ]
    }
  },
  "chrome": {
    "title": "The dip is still there in half-magnitude bins",
    "subtitle": "Magnitude 2.5+ earthquakes in 0.5 magnitude bins, 1,584 quakes, January 2026"
  }
};
</script>

<Chart {spec} />
import { createChart } from "@opendata-ai/openchart-vanilla";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 40 rows; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": "bar",
  "transform": [
    {
      "bin": {
        "step": 0.5,
        "extent": [
          2.5,
          7
        ]
      },
      "field": "magnitude_bin",
      "as": [
        "from",
        "to"
      ]
    },
    {
      "aggregate": [
        {
          "op": "sum",
          "field": "quakes",
          "as": "count"
        }
      ],
      "groupby": [
        "from",
        "to"
      ]
    }
  ],
  "encoding": {
    "x": {
      "field": "from",
      "type": "quantitative",
      "scale": {
        "domain": [
          2.5,
          7
        ],
        "nice": false
      },
      "axis": {
        "title": "Magnitude",
        "values": [
          3,
          4,
          5,
          6,
          7
        ]
      }
    },
    "x2": {
      "field": "to",
      "type": "quantitative"
    },
    "y": {
      "field": "count",
      "type": "quantitative",
      "axis": {
        "title": "Quakes per bin",
        "tickCount": 4
      }
    }
  },
  "theme": {
    "colors": {
      "categorical": [
        {
          "light": "#0e7490",
          "dark": "#22a3c4"
        }
      ]
    }
  },
  "chrome": {
    "title": "The dip is still there in half-magnitude bins",
    "subtitle": "Magnitude 2.5+ earthquakes in 0.5 magnitude bins, 1,584 quakes, January 2026"
  }
};

const container = document.getElementById("chart");
if (!container) throw new Error('Add <div id="chart"></div> to the page first.');
createChart(container, spec);

At half-magnitude bins the small spikes at higher magnitudes smooth out, and the dip stays, so the bins did not make it. The way the catalog was collected probably did. Outside regions with dense seismometers, such as the United States, the catalog reliably includes only quakes of about magnitude 4.5 and up. Counts climb from 4.0 and peak at 4.5, where worldwide coverage begins.

To check whether the data collection made a feature, read how the data was gathered.

The spec

OpenChart’s histogram mark bins and counts raw rows for you. mark: { type: "histogram", binCount: 24 } asks for up to 24 bins and picks a round width, and encoding.x.bin: { step, extent } sets an exact width. The charts here use the bin transform instead, because the earthquake rows are already counted and have to be summed, which the mark’s count can’t do. The income charts use the same transform so the two specs match.

In the income spec, bin: { step: 10000, extent: [50000, 125000] } writes each state’s bin start and end into two new fields. aggregate with op: "count", grouped by both fields, turns 51 rows into one count per bin.

The bin start goes on x and the bin end on x2, so each bar spans its whole range. A scale.domain fixes the x range so every width is drawn on the same axis, and axis.values puts the ticks on bin edges ($50k, $70k and so on) so a reader can see where each bar starts. The bar color is a { light, dark } pair in theme.colors.categorical, which picks a lighter teal on a dark page.

The earthquake rows are already counts per 0.1 magnitude, so the first earthquake chart draws them directly, each bar ending 0.1 above its start. The 0.5 chart merges them with a bin transform and op: "sum" on the count field.

A density curve avoids bin edges entirely, in exchange for choosing a bandwidth, which sets how far each value is smeared across its neighbors.

Every field the mark takes, including binCount, is on the histogram page.