|
| 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 marginal distribution plots. |
| 26 | + display_as: statistical |
| 27 | + language: python |
| 28 | + layout: base |
| 29 | + name: Marginal Distribution Plots |
| 30 | + order: 13 |
| 31 | + page_type: u-guide |
| 32 | + permalink: python/marginal-plots/ |
| 33 | + thumbnail: thumbnail/figure-labels.png |
| 34 | +--- |
| 35 | + |
| 36 | +### Overview |
| 37 | + |
| 38 | +Marginal distribution plots are small subplots above or to the right of a main plot, which show the distribution of data along only one dimension. Marginal distribution plot capabilities are built into various Plotly Express functions such as `scatter` and `histogram`. [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 | +### Scatter Plot Marginals |
| 41 | + |
| 42 | +The `marginal_x` and `marginal_y` arguments accept one of `"histogram"`, `"rug"`, `"box"`, or `"violin"` (see also how to create [histograms](/python/histograms/), [box plots](/python/box-plots/) and [violin plots](/python/violin-plots/) as the main figure). |
| 43 | + |
| 44 | +Marginal plots are linked to the main plot: try zooming or panning on the main plot. |
| 45 | + |
| 46 | +Marginal plots also support hover, including per-point hover as with the rug-plot on the right: try hovering over the points on the right marginal plot. |
| 47 | + |
| 48 | +```python |
| 49 | +import plotly.express as px |
| 50 | +df = px.data.iris() |
| 51 | +fig = px.scatter(df, x="sepal_length", y="sepal_width", marginal_x="histogram", marginal_y="rug") |
| 52 | +fig.show() |
| 53 | +``` |
| 54 | + |
| 55 | +```python |
| 56 | +import plotly.express as px |
| 57 | +df = px.data.iris() |
| 58 | +fig = px.density_heatmap(df, x="sepal_length", y="sepal_width", marginal_x="box", marginal_y="violin") |
| 59 | +fig.show() |
| 60 | +``` |
| 61 | + |
| 62 | +### Marginal Plots and Color |
| 63 | + |
| 64 | +Marginal plots respect the `color` argument as well, and are linked to the respective legend elements. Try clicking on the legend items. |
| 65 | + |
| 66 | +```python |
| 67 | +import plotly.express as px |
| 68 | +df = px.data.iris() |
| 69 | +fig = px.scatter(df, x="sepal_length", y="sepal_width", color="species", |
| 70 | + marginal_x="box", marginal_y="violin", |
| 71 | + title="Click on the legend items!") |
| 72 | +fig.show() |
| 73 | +``` |
| 74 | + |
| 75 | +### Marginal Plots on Histograms |
| 76 | + |
| 77 | +[Histograms](/python/histograms/) are often used to show the distribution of a variable, and they also support marginal plots in Plotly Express, with the `marginal` argument: |
| 78 | + |
| 79 | +```python |
| 80 | +import plotly.express as px |
| 81 | +df = px.data.iris() |
| 82 | +fig = px.histogram(df, x="sepal_length", color="species", marginal="box") |
| 83 | +fig.show() |
| 84 | +``` |
| 85 | + |
| 86 | +Try hovering over the rug plot points to identify individual country values in the histogram below: |
| 87 | + |
| 88 | +```python |
| 89 | +import plotly.express as px |
| 90 | +df = px.data.gapminder().query("year == 2007") |
| 91 | +fig = px.histogram(df, x="lifeExp", color="continent", marginal="rug", hover_name="country", |
| 92 | + title="Hover over the rug plot!") |
| 93 | +fig.show() |
| 94 | +``` |
| 95 | + |
| 96 | +### Marginal Plots and Facets |
| 97 | + |
| 98 | +Marginal plots can be used in conjunction with [Plotly Express facets](/python/facet-plots/) so long as they go along different directions: |
| 99 | + |
| 100 | +```python |
| 101 | +import plotly.express as px |
| 102 | +df = px.data.tips() |
| 103 | +fig = px.scatter(df, x="total_bill", y="tip", color="sex", facet_col="day", |
| 104 | + marginal_x="box") |
| 105 | +fig.show() |
| 106 | +``` |
| 107 | + |
| 108 | +```python |
| 109 | +import plotly.express as px |
| 110 | +df = px.data.tips() |
| 111 | +fig = px.scatter(df, x="total_bill", y="tip", color="sex", facet_row="time", |
| 112 | + marginal_y="box") |
| 113 | +fig.show() |
| 114 | +``` |
| 115 | + |
| 116 | +```python |
| 117 | +import plotly.express as px |
| 118 | +df = px.data.tips() |
| 119 | +fig = px.histogram(df, x="total_bill", y="tip", color="sex", facet_col="day", |
| 120 | + marginal="box") |
| 121 | +fig.show() |
| 122 | +``` |
0 commit comments