|
| 1 | +--- |
| 2 | +jupyter: |
| 3 | + jupytext: |
| 4 | + notebook_metadata_filter: all |
| 5 | + text_representation: |
| 6 | + extension: .md |
| 7 | + format_name: markdown |
| 8 | + format_version: '1.2' |
| 9 | + jupytext_version: 1.4.2 |
| 10 | + kernelspec: |
| 11 | + display_name: Python 3 |
| 12 | + language: python |
| 13 | + name: python3 |
| 14 | + language_info: |
| 15 | + codemirror_mode: |
| 16 | + name: ipython |
| 17 | + version: 3 |
| 18 | + file_extension: .py |
| 19 | + mimetype: text/x-python |
| 20 | + name: python |
| 21 | + nbconvert_exporter: python |
| 22 | + pygments_lexer: ipython3 |
| 23 | + version: 3.7.7 |
| 24 | + plotly: |
| 25 | + description: How to add empirical cumulative distribution function (ECDF) plots. |
| 26 | + display_as: statistical |
| 27 | + language: python |
| 28 | + layout: base |
| 29 | + name: Empirical Cumulative Distribution Plots |
| 30 | + order: 16 |
| 31 | + page_type: u-guide |
| 32 | + permalink: python/ecdf-plots/ |
| 33 | + thumbnail: thumbnail/figure-labels.png |
| 34 | +--- |
| 35 | + |
| 36 | +### Overview |
| 37 | + |
| 38 | +[Empirical cumulative distribution function plots](https://en.wikipedia.org/wiki/Empirical_distribution_function) are a way to visualize the distribution of a variabl, and Plotly Express has a built-in function, `px.ecdf()` to generate such plots. [Plotly Express](/python/plotly-express/) is the easy-to-use, high-level interface to Plotly, which [operates on a variety of types of data](/python/px-arguments/) and produces [easy-to-style figures](/python/styling-plotly-express/). |
| 39 | + |
| 40 | +### Simple ECDF Plots |
| 41 | + |
| 42 | +Providing a single column to the `x` variable yields a basic ECDF plot. |
| 43 | + |
| 44 | +```python |
| 45 | +import plotly.express as px |
| 46 | +df = px.data.tips() |
| 47 | +fig = px.ecdf(df, x="total_bill") |
| 48 | +fig.show() |
| 49 | +``` |
| 50 | + |
| 51 | +Providing multiple columns leverage's Plotly Express' [wide-form data support](https://plotly.com/python/wide-form/) to show multiple variables on the same plot. |
| 52 | + |
| 53 | +```python |
| 54 | +import plotly.express as px |
| 55 | +df = px.data.tips() |
| 56 | +fig = px.ecdf(df, x=["total_bill", "tip"]) |
| 57 | +fig.show() |
| 58 | +``` |
| 59 | + |
| 60 | +It is also possible to map another variable to the color dimension of a plot. |
| 61 | + |
| 62 | +```python |
| 63 | +import plotly.express as px |
| 64 | +df = px.data.tips() |
| 65 | +fig = px.ecdf(df, x="total_bill", color="sex") |
| 66 | +fig.show() |
| 67 | +``` |
| 68 | + |
| 69 | +### Configuring the Y axis |
| 70 | + |
| 71 | +By default, the Y axis shows probability, but it is also possible to show raw counts by setting the `ecdfnorm` argument to `None` or to show percentages by setting it to `percent`. |
| 72 | + |
| 73 | +```python |
| 74 | +import plotly.express as px |
| 75 | +df = px.data.tips() |
| 76 | +fig = px.ecdf(df, x="total_bill", color="sex", ecdfnorm=None) |
| 77 | +fig.show() |
| 78 | +``` |
| 79 | + |
| 80 | +If a `y` value is provided, the Y axis is set to the sum of `y` rather than counts. |
| 81 | + |
| 82 | +```python |
| 83 | +import plotly.express as px |
| 84 | +df = px.data.tips() |
| 85 | +fig = px.ecdf(df, x="total_bill", y="tip", color="sex", ecdfnorm=None) |
| 86 | +fig.show() |
| 87 | +``` |
| 88 | + |
| 89 | +### Reversed and Complementary CDF plots |
| 90 | + |
| 91 | +By default, the Y value represents the fraction of the data that is *at or below* the value on on the X axis. Setting `ecdfmode` to `"reversed"` reverses this, with the Y axis representing the fraction of the data *at or above* the X value. Setting `ecdfmode` to `"complementary"` plots `1-ECDF`, meaning that the Y values represent the fraction of the data *above* the X value. |
| 92 | + |
| 93 | +```python |
| 94 | +import plotly.express as px |
| 95 | +fig = px.ecdf(df, x=[1,2,3,4], markers=True, ecdfmode="standard", |
| 96 | + title="ecdfmode='standard' (at or below X value, the default)") |
| 97 | +fig.show() |
| 98 | +``` |
| 99 | + |
| 100 | +```python |
| 101 | +import plotly.express as px |
| 102 | +fig = px.ecdf(df, x=[1,2,3,4], markers=True, ecdfmode="reversed", |
| 103 | + title="ecdfmode='reversed' (at or above X value)") |
| 104 | +fig.show() |
| 105 | +``` |
| 106 | + |
| 107 | +```python |
| 108 | +import plotly.express as px |
| 109 | +fig = px.ecdf(df, x=[1,2,3,4], markers=True, ecdfmode="complementary", |
| 110 | + title="ecdfmode='complementary' (above X value)") |
| 111 | +fig.show() |
| 112 | +``` |
| 113 | + |
| 114 | +### Orientation |
| 115 | + |
| 116 | +By default, plots are oriented vertically (i.e. the variable is on the X axis and counted/summed upwards), but this can be overridden with the `orientation` argument. |
| 117 | + |
| 118 | +```python |
| 119 | +import plotly.express as px |
| 120 | +df = px.data.tips() |
| 121 | +fig = px.ecdf(df, x="total_bill", y="tip", color="sex", ecdfnorm=None, orientation="h") |
| 122 | +fig.show() |
| 123 | +``` |
| 124 | + |
| 125 | +### Markers and/or Lines |
| 126 | + |
| 127 | +ECDF Plots can be configured to show lines and/or markers. |
| 128 | + |
| 129 | +```python |
| 130 | +import plotly.express as px |
| 131 | +df = px.data.tips() |
| 132 | +fig = px.ecdf(df, x="total_bill", color="sex", markers=True) |
| 133 | +fig.show() |
| 134 | +``` |
| 135 | + |
| 136 | +```python |
| 137 | +import plotly.express as px |
| 138 | +df = px.data.tips() |
| 139 | +fig = px.ecdf(df, x="total_bill", color="sex", markers=True, lines=False) |
| 140 | +fig.show() |
| 141 | +``` |
| 142 | + |
| 143 | +```python |
| 144 | + |
| 145 | +``` |
0 commit comments