Skip to content
Tutorials Center
My learning

Topics

Lesson 15 of 42 ยท Statistical and Scientific Charts

Distribution Plots and ECDFs

Go beyond histograms in Plotly: draw empirical cumulative distribution (ECDF) plots, overlay a KDE curve on a histogram, add rug marks, and build a normal Q-Q plot with SciPy.

Once you can draw a histogram, you will soon hit its weaknesses: the shape depends on the bin width, and overlaid histograms get messy. This lesson adds the tools statisticians use to look at one distribution (or compare several) more honestly: the ECDF, the kernel density estimate (KDE), rug marks and the Q-Q plot.

All of them work from the same raw samples. We use NumPy to generate seeded sample data and SciPy for the statistics, and Plotly to draw.

ECDF: the empirical cumulative distribution

For each value x on the horizontal axis, the ECDF shows the fraction of observations that are less than or equal to x. It starts at 0 on the left and rises to 1 on the right. It needs no bins, shows every data point and makes it easy to read percentiles: find 0.5 on the y axis and read the median on the x axis.

px.ecdf draws it directly:

Python px.ecdf, and the numbers behind it

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

An ECDF is a step function, so Plotly draws it with line_shape="hv". Some useful arguments:

  • ecdfnorm: "probability" (default, 0 to 1), "percent" (0 to 100) or None (counts).
  • ecdfmode: "standard" (default), "complementary" (fraction above x, a survival curve) or "reversed".
  • marginal="histogram" or "rug": add a second small plot.
  • markers=True, lines=False: show the step positions as points.

With groups, ECDFs overlay cleanly. Where one curve is to the right of another, that group has larger values at every percentile.

KDE: a smooth density estimate

A kernel density estimate replaces the blocky bars with a smooth curve. It places a small bell (kernel) on every data point and adds them up. Plotly has no KDE trace of its own (a violin is a mirrored KDE), but the curve is easy to draw: compute it with SciPy's gaussian_kde and plot it as a line, on top of a histogram normalized to density.

Python Histogram + KDE curve + rug

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Setting histnorm="probability density" makes the histogram's total area 1, the same scale as the KDE curve, so they can share an axis. The rug is a row of short vertical ticks at the observed values, drawn as markers with the line-ns-open symbol and a y value just below zero.

Here is the same figure in plotly.js, with the KDE computed in JavaScript, so you can see it. Try changing the bandwidth h to see how smoothing changes the shape:

A KDE with an adjustable bandwidth

A small bandwidth follows every bump of the sample (wiggly); a large one smooths everything into a single hump and may hide the two groups. SciPy chooses a default with Scott's rule, which you can change with bw_method.

Q-Q plots: is my data normal?

A quantile-quantile (Q-Q) plot compares the quantiles of your data with the quantiles of a theoretical distribution, usually the normal distribution. If the points fall on a straight line, the data follow that distribution. S-shaped curves point to heavy or light tails, and curved shapes to skewness. Many statistical methods assume normal residuals, so Q-Q plots are a regular check.

Python A normal Q-Q plot

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The correlation r of the Q-Q points is very close to 1 for the normal sample and clearly lower for the skewed one, and the Shapiro-Wilk test agrees. On the chart, the exponential data curves away from its straight reference line at the right end, the sign of a long right tail.

Which plot when?

Question Plot
What does the distribution look like? Histogram (density-normalized) with KDE
What fraction is below a threshold? What is the median or 90th percentile? ECDF
Do two groups differ? ECDFs overlaid, or violins
Is the data normal? Q-Q plot
Where are individual values? Rug, strip plot

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What does an ECDF value of 0.8 at x = 30 mean?

    The ECDF at x is the fraction of observations that are at or below x.
  2. 2. Which px.ecdf argument draws the fraction of values ABOVE each x?

    The complementary ECDF (a survival function) shows the share larger than x.
  3. 3. Why set histnorm="probability density" when overlaying a KDE curve on a histogram?

    A density histogram and a KDE are both on the density scale, so they line up.
  4. 4. In a normal Q-Q plot, what do points that follow a straight line mean?

    A straight Q-Q line means the sample quantiles match the normal quantiles.
  5. 5. What happens to a KDE curve when the bandwidth is made very large?

    Larger bandwidth means more smoothing, which can merge real peaks.

Summary

  • The ECDF needs no bins, shows every point and makes percentiles easy; use px.ecdf with ecdfnorm and ecdfmode.
  • A KDE is a smooth density curve: compute it with SciPy and draw it next to a density-normalized histogram.
  • Rug marks show raw values; Q-Q plots test whether data follow a normal distribution.
  • Pick the plot for the question: shape, thresholds and percentiles, group comparison or normality.

Next: Heatmaps and Contour Plots