|
| 1 | +--- |
| 2 | +jupyter: |
| 3 | + jupytext: |
| 4 | + formats: ipynb,md |
| 5 | + text_representation: |
| 6 | + extension: .md |
| 7 | + format_name: markdown |
| 8 | + format_version: "1.1" |
| 9 | + jupytext_version: 1.1.1 |
| 10 | + kernelspec: |
| 11 | + display_name: Python 3 |
| 12 | + language: python |
| 13 | + name: python3 |
| 14 | +--- |
| 15 | + |
| 16 | +# Plotly Express Gallery |
| 17 | + |
| 18 | +Plotly Express is a terse, consistent, high-level wrapper around [Plotly.py](https://plot.ly/python) for rapid data exploration and figure generation. |
| 19 | + |
| 20 | +This notebook demonstrates various `plotly_express` features. [Reference documentation](https://plotly.github.io/plotly_express/plotly_express/) and a [step by step walkthrough notebook](https://nbviewer.jupyter.org/github/plotly/plotly_express/blob/gh-pages/walkthrough.ipynb) are also available. |
| 21 | + |
| 22 | +You can also read our [Medium announcement article](https://medium.com/@plotlygraphs/introducing-plotly-express-808df010143d) for more information on this library. |
| 23 | + |
| 24 | +## A single import |
| 25 | + |
| 26 | +```python |
| 27 | +import plotly_express as px |
| 28 | +``` |
| 29 | + |
| 30 | +## Built-in sample datasets |
| 31 | + |
| 32 | +```python |
| 33 | +print(px.data.iris.__doc__) |
| 34 | +iris = px.data.iris() |
| 35 | +``` |
| 36 | + |
| 37 | +```python |
| 38 | +tips = px.data.tips() |
| 39 | +gapminder = px.data.gapminder() |
| 40 | +election = px.data.election() |
| 41 | +wind = px.data.wind() |
| 42 | +carshare = px.data.carshare() |
| 43 | +``` |
| 44 | + |
| 45 | +## Scatter and Line plots |
| 46 | + |
| 47 | +```python |
| 48 | +px.scatter(iris, x="sepal_width", y="sepal_length") |
| 49 | +``` |
| 50 | + |
| 51 | +```python |
| 52 | +px.scatter(iris, x="sepal_width", y="sepal_length", color="species") |
| 53 | +``` |
| 54 | + |
| 55 | +```python |
| 56 | +px.scatter(iris, x="sepal_width", y="sepal_length", color="species", marginal_y="rug", marginal_x="histogram") |
| 57 | +``` |
| 58 | + |
| 59 | +```python |
| 60 | +px.scatter(iris, x="sepal_width", y="sepal_length", color="species", marginal_y="violin", |
| 61 | + marginal_x="box", trendline="ols") |
| 62 | +``` |
| 63 | + |
| 64 | +```python |
| 65 | +iris["e"] = iris["sepal_width"]/100 |
| 66 | +px.scatter(iris, x="sepal_width", y="sepal_length", color="species", error_x="e", error_y="e") |
| 67 | +``` |
| 68 | + |
| 69 | +```python |
| 70 | +del iris["e"] |
| 71 | +``` |
| 72 | + |
| 73 | +```python |
| 74 | +px.scatter(tips, x="total_bill", y="tip", facet_row="time", facet_col="day", color="smoker", trendline="ols", |
| 75 | + category_orders={"day": ["Thur", "Fri", "Sat", "Sun"], "time": ["Lunch", "Dinner"]}) |
| 76 | +``` |
| 77 | + |
| 78 | +```python |
| 79 | +px.scatter_matrix(iris) |
| 80 | +``` |
| 81 | + |
| 82 | +```python |
| 83 | +px.scatter_matrix(iris, dimensions=["sepal_width", "sepal_length", "petal_width", "petal_length"], color="species") |
| 84 | +``` |
| 85 | + |
| 86 | +```python |
| 87 | +px.parallel_coordinates(iris, color="species_id", labels={"species_id": "Species", |
| 88 | + "sepal_width": "Sepal Width", "sepal_length": "Sepal Length", |
| 89 | + "petal_width": "Petal Width", "petal_length": "Petal Length", }, |
| 90 | + color_continuous_scale=px.colors.diverging.Tealrose, color_continuous_midpoint=2) |
| 91 | +``` |
| 92 | + |
| 93 | +```python |
| 94 | +px.parallel_categories(tips, color="size", color_continuous_scale=px.colors.sequential.Inferno) |
| 95 | +``` |
| 96 | + |
| 97 | +```python |
| 98 | +px.scatter(tips, x="total_bill", y="tip", color="size", facet_col="sex", |
| 99 | + color_continuous_scale=px.colors.sequential.Viridis, render_mode="webgl") |
| 100 | +``` |
| 101 | + |
| 102 | +```python |
| 103 | +px.scatter(gapminder.query("year==2007"), x="gdpPercap", y="lifeExp", size="pop", color="continent", |
| 104 | + hover_name="country", log_x=True, size_max=60) |
| 105 | +``` |
| 106 | + |
| 107 | +```python |
| 108 | +px.scatter(gapminder, x="gdpPercap", y="lifeExp", animation_frame="year", animation_group="country", |
| 109 | + size="pop", color="continent", hover_name="country", facet_col="continent", |
| 110 | + log_x=True, size_max=45, range_x=[100,100000], range_y=[25,90]) |
| 111 | +``` |
| 112 | + |
| 113 | +```python |
| 114 | +px.line(gapminder, x="year", y="lifeExp", color="continent", line_group="country", hover_name="country", |
| 115 | + line_shape="spline") |
| 116 | +``` |
| 117 | + |
| 118 | +## Visualize Distributions |
| 119 | + |
| 120 | +```python |
| 121 | +px.density_contour(iris, x="sepal_width", y="sepal_length") |
| 122 | +``` |
| 123 | + |
| 124 | +```python |
| 125 | +px.density_contour(iris, x="sepal_width", y="sepal_length", color="species", marginal_x="rug", marginal_y="histogram") |
| 126 | +``` |
| 127 | + |
| 128 | +```python |
| 129 | +px.bar(tips, x="sex", y="total_bill", color="smoker", barmode="group") |
| 130 | +``` |
| 131 | + |
| 132 | +```python |
| 133 | +px.bar(tips, x="sex", y="total_bill", color="smoker", barmode="group", facet_row="time", facet_col="day", |
| 134 | + category_orders={"day": ["Thur", "Fri", "Sat", "Sun"], "time": ["Lunch", "Dinner"]}) |
| 135 | +``` |
| 136 | + |
| 137 | +```python |
| 138 | +px.histogram(tips, x="total_bill", y="tip", color="sex", marginal="rug", hover_data=tips.columns) |
| 139 | +``` |
| 140 | + |
| 141 | +```python |
| 142 | +px.histogram(tips, x="sex", y="tip", histfunc="avg", color="smoker", barmode="group", |
| 143 | + facet_row="time", facet_col="day", category_orders={"day": ["Thur", "Fri", "Sat", "Sun"], |
| 144 | + "time": ["Lunch", "Dinner"]}) |
| 145 | +``` |
| 146 | + |
| 147 | +```python |
| 148 | +px.box(tips, x="day", y="total_bill", color="smoker", notched=True) |
| 149 | +``` |
| 150 | + |
| 151 | +```python |
| 152 | +px.violin(tips, y="tip", x="smoker", color="sex", box=True, points="all", hover_data=tips.columns) |
| 153 | +``` |
| 154 | + |
| 155 | +## Ternary Coordinates |
| 156 | + |
| 157 | +```python |
| 158 | +px.scatter_ternary(election, a="Joly", b="Coderre", c="Bergeron", color="winner", size="total", hover_name="district", |
| 159 | + size_max=15, color_discrete_map = {"Joly": "blue", "Bergeron": "green", "Coderre":"red"} ) |
| 160 | +``` |
| 161 | + |
| 162 | +```python |
| 163 | +px.line_ternary(election, a="Joly", b="Coderre", c="Bergeron", color="winner", line_dash="winner") |
| 164 | +``` |
| 165 | + |
| 166 | +## 3D Coordinates |
| 167 | + |
| 168 | +```python |
| 169 | +px.scatter_3d(election, x="Joly", y="Coderre", z="Bergeron", color="winner", size="total", hover_name="district", |
| 170 | + symbol="result", color_discrete_map = {"Joly": "blue", "Bergeron": "green", "Coderre":"red"}) |
| 171 | +``` |
| 172 | + |
| 173 | +```python |
| 174 | +px.line_3d(election, x="Joly", y="Coderre", z="Bergeron", color="winner", line_dash="winner") |
| 175 | +``` |
| 176 | + |
| 177 | +## Polar Coordinates |
| 178 | + |
| 179 | +```python |
| 180 | +px.scatter_polar(wind, r="value", theta="direction", color="strength", symbol="strength", |
| 181 | + color_discrete_sequence=px.colors.sequential.Plotly[-2::-1]) |
| 182 | +``` |
| 183 | + |
| 184 | +```python |
| 185 | +px.line_polar(wind, r="value", theta="direction", color="strength", line_close=True, |
| 186 | + color_discrete_sequence=px.colors.sequential.Plotly[-2::-1]) |
| 187 | +``` |
| 188 | + |
| 189 | +```python |
| 190 | +px.bar_polar(wind, r="value", theta="direction", color="strength", template="plotly_dark", |
| 191 | + color_discrete_sequence= px.colors.sequential.Plotly[-2::-1]) |
| 192 | +``` |
| 193 | + |
| 194 | +## Maps |
| 195 | + |
| 196 | +```python |
| 197 | +px.set_mapbox_access_token(open(".mapbox_token").read()) |
| 198 | +px.scatter_mapbox(carshare, lat="centroid_lat", lon="centroid_lon", color="peak_hour", size="car_hours", |
| 199 | + color_continuous_scale=px.colors.cyclical.IceFire, size_max=15, zoom=10) |
| 200 | +``` |
| 201 | + |
| 202 | +```python |
| 203 | +px.set_mapbox_access_token(open(".mapbox_token").read()) |
| 204 | +px.line_mapbox(carshare, lat="centroid_lat", lon="centroid_lon", color="peak_hour") |
| 205 | +``` |
| 206 | + |
| 207 | +```python |
| 208 | +px.scatter_geo(gapminder, locations="iso_alpha", color="continent", hover_name="country", size="pop", |
| 209 | + animation_frame="year", projection="natural earth") |
| 210 | +``` |
| 211 | + |
| 212 | +```python |
| 213 | +px.line_geo(gapminder.query("year==2007"), locations="iso_alpha", color="continent", projection="orthographic") |
| 214 | +``` |
| 215 | + |
| 216 | +```python |
| 217 | +px.choropleth(gapminder, locations="iso_alpha", color="lifeExp", hover_name="country", animation_frame="year", |
| 218 | + color_continuous_scale=px.colors.sequential.Plasma) |
| 219 | +``` |
| 220 | + |
| 221 | +## Built-in Color Scales and Sequences (and a way to see them!) |
| 222 | + |
| 223 | +```python |
| 224 | +px.colors.qualitative.swatches() |
| 225 | +``` |
| 226 | + |
| 227 | +```python |
| 228 | +px.colors.sequential.swatches() |
| 229 | +``` |
| 230 | + |
| 231 | +```python |
| 232 | +px.colors.diverging.swatches() |
| 233 | +``` |
| 234 | + |
| 235 | +```python |
| 236 | +px.colors.cyclical.swatches() |
| 237 | +``` |
| 238 | + |
| 239 | +```python |
| 240 | +px.colors.colorbrewer.swatches() |
| 241 | +``` |
| 242 | + |
| 243 | +```python |
| 244 | +px.colors.cmocean.swatches() |
| 245 | +``` |
| 246 | + |
| 247 | +```python |
| 248 | +px.colors.carto.swatches() |
| 249 | +``` |
| 250 | + |
| 251 | +# Next steps |
| 252 | + |
| 253 | +Phew, you've made it this far! If you want to use Plotly Express yourself, just `pip install plotly_express` to install it and head on over to our [reference documentation](https://plotly.github.io/plotly_express/plotly_express/) or just copy-paste from the examples above! |
0 commit comments