Skip to content
← All questions

What does `density=True` do in `plt.hist` in this Monte Carlo example?

Setting `density=True` in `plt.hist` normalizes the bar heights so that the plotted histogram represents an empirical probability density estimate of the simulated total durations, rather than raw counts.

Conditions

  • Used when the goal is to compare the sample distribution with probability-density scale.
  • The plotted bars are normalized by bin width and sample size.

Reasoning, step by step

  1. Plot the empirical distribution of the total durations using `plt.hist(duration, density = True)`.
  2. Interpret the vertical scale as probability density rather than raw counts.

Example

The typed code is `plt.hist(duration, density = True)`. The speaker says this is done "so that we can plot everything in the form of probability densities." The resulting y-axis is labeled with values such as 0.0, 0.1, 0.2 rather than counts.

Common misconceptions

  • A histogram is not automatically a count chart if `density=True` is used. One might assume the plotted bar heights are raw frequencies of simulations, but the clip states and shows that `density=True` rescales the histogram to probability densities.

Watch the explanation

Explore next

Answers are generated from source material and independently checked. Consult the original video or creator if something is unclear.