|
| 1 | +--- |
| 2 | +jupyter: |
| 3 | + jupytext: |
| 4 | + notebook_metadata_filter: all |
| 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 | + 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.3 |
| 24 | + plotly: |
| 25 | + description: Arguments accepted by plotly express functions |
| 26 | + display_as: file_settings |
| 27 | + has_thumbnail: true |
| 28 | + ipynb: ~notebook_demo/253 |
| 29 | + language: python |
| 30 | + layout: user-guide |
| 31 | + name: Plotly Express Arguments |
| 32 | + order: 17 |
| 33 | + page_type: u-guide |
| 34 | + permalink: python/px-arguments/ |
| 35 | + thumbnail: thumbnail/plotly-express.png |
| 36 | + title: Plotly Express Arguments |
| 37 | + v4upgrade: true |
| 38 | +--- |
| 39 | + |
| 40 | +## The different ways to call plotly express functions |
| 41 | + |
| 42 | +See also the [introduction to plotly express](./plotly-express). |
| 43 | + |
| 44 | +### pandas DataFrame input data |
| 45 | + |
| 46 | +`px` functions supports natively pandas DataFrame. Arguments can either be passed as dataframe columns, or as column names if the `data_frame` argument is provided. |
| 47 | + |
| 48 | +#### Passing columns as arguments |
| 49 | + |
| 50 | +```python |
| 51 | +import plotly.express as px |
| 52 | +iris = px.data.iris() |
| 53 | +# Use directly Columns as argument. You can use tab completion for this! |
| 54 | +fig = px.scatter(iris, x=iris.sepal_length, y=iris.sepal_width, color=iris.species, size=iris.petal_length) |
| 55 | +fig.show() |
| 56 | +``` |
| 57 | +#### Passing name strings as arguments |
| 58 | + |
| 59 | +```python |
| 60 | +import plotly.express as px |
| 61 | +iris = px.data.iris() |
| 62 | +# Use column names instead. This is the same chart as above. |
| 63 | +fig = px.scatter(iris, x='sepal_length', y='sepal_width', color='species', size='petal_length') |
| 64 | +fig.show() |
| 65 | +``` |
| 66 | + |
| 67 | +#### Using the index of a DataFrame |
| 68 | + |
| 69 | +In addition to columns, it is also possible to pass the index of a DataFrame as argument. In the example below the index is displayed in the hover data. |
| 70 | + |
| 71 | +```python |
| 72 | +import plotly.express as px |
| 73 | +iris = px.data.iris() |
| 74 | +fig = px.scatter(iris, x=iris.sepal_length, y=iris.sepal_width, size=iris.petal_length, |
| 75 | + hover_data=[iris.index]) |
| 76 | +fig.show() |
| 77 | +``` |
| 78 | + |
| 79 | +### Columns not in the data_frame argument |
| 80 | + |
| 81 | +In the addition to columns from the `data_frame` argument, one may also pass columns from a different DataFrame, *as long as all columns have the same length*. It is also possible to pass columns without passing the `data_frame` argument. |
| 82 | + |
| 83 | +However, column names are used only if they correspond to columns in the `data_frame` argument, in other cases, the name of the keyword argument is used. As explained below, the `labels` argument can be used to set names. |
| 84 | + |
| 85 | +```python |
| 86 | +import plotly.express as px |
| 87 | +import pandas as pd |
| 88 | +df1 = pd.DataFrame(dict(time=[10, 20, 30], sales=[10, 8, 30])) |
| 89 | +df2 = pd.DataFrame(dict(market=[4, 2, 5])) |
| 90 | +fig = px.bar(df1, x=df1.time, y=df2.market, color=df1.sales) |
| 91 | +fig.show() |
| 92 | +``` |
| 93 | + |
| 94 | +### Using labels to pass names |
| 95 | + |
| 96 | +The `labels` argument can be used to override the names used for axis titles, legend entries and hovers. |
| 97 | + |
| 98 | +```python |
| 99 | +import plotly.express as px |
| 100 | +import pandas as pd |
| 101 | + |
| 102 | +gapminder = px.data.gapminder() |
| 103 | +gdp = gapminder['pop'] * gapminder['gdpPercap'] |
| 104 | +fig = px.bar(gapminder, x='year', y=gdp, color='continent', labels={'y':'gdp'}, |
| 105 | + hover_data=['country'], |
| 106 | + title='Evolution of world GDP') |
| 107 | +fig.show() |
| 108 | +``` |
| 109 | + |
| 110 | +### Using array-like arguments: NumPy arrays, lists... |
| 111 | + |
| 112 | +`px` arguments can also be array-like objects such as lists, NumPy arrays. |
| 113 | + |
| 114 | +```python |
| 115 | +import plotly.express as px |
| 116 | + |
| 117 | +# List arguments |
| 118 | +fig = px.line(x=[1, 2, 3, 4], y=[3, 5, 4, 8]) |
| 119 | +fig.show() |
| 120 | +``` |
| 121 | + |
| 122 | +```python |
| 123 | +import numpy as np |
| 124 | +import plotly.express as px |
| 125 | + |
| 126 | +t = np.linspace(0, 10, 100) |
| 127 | +# NumPy arrays arguments |
| 128 | +fig = px.scatter(x=t, y=np.sin(t), labels={'x':'t', 'y':'sin(t)'}) # override keyword names with labels |
| 129 | +fig.show() |
| 130 | +``` |
| 131 | + |
| 132 | +### Passing dictionaries or array-likes as the data_frame argument |
| 133 | + |
| 134 | +The column-based argument `data_frame` can also be passed with a `dict` or `array`. Using a dictionary can be a convenient way to pass column names used in axis titles, legend entries and hovers without creating a pandas DataFrame. |
| 135 | + |
| 136 | +```python |
| 137 | +import plotly.express as px |
| 138 | +import numpy as np |
| 139 | +N = 10000 |
| 140 | +np.random.seed(0) |
| 141 | +fig = px.density_contour(dict(effect_size=5 + np.random.randn(N), |
| 142 | + waiting_time=np.random.poisson(size=N)), |
| 143 | + x="effect_size", y="waiting_time") |
| 144 | +fig.show() |
| 145 | +``` |
| 146 | + |
| 147 | +#### Integer column names |
| 148 | + |
| 149 | +When the `data_frame` argument is a NumPy array, column names are integer corresponding to the columns of the array. In this case, keyword names are used in axis, legend and hovers. This is also the case for a pandas DataFrame with integer column names. Use the `labels` argument to override these names. |
| 150 | + |
| 151 | +```python |
| 152 | +import numpy as np |
| 153 | +import plotly.express as px |
| 154 | + |
| 155 | +ar = np.arange(100).reshape((10, 10)) |
| 156 | +fig = px.scatter(ar, x=2, y=6, size=1, color=5) |
| 157 | +fig.show() |
| 158 | +``` |
| 159 | + |
| 160 | +### Mixing dataframes and other types |
| 161 | + |
| 162 | +It is possible to mix DataFrame columns, NumPy arrays and lists as arguments. Remember that the only column names to be used correspond to columns in the `data_frame` argument, use `labels` to override names displayed in axis titles, legend entries or hovers. |
| 163 | + |
| 164 | +```python |
| 165 | +import plotly.express as px |
| 166 | +import numpy as np |
| 167 | +import pandas as pd |
| 168 | + |
| 169 | +gapminder = px.data.gapminder() |
| 170 | +gdp = np.log(gapminder['pop'] * gapminder['gdpPercap']) # NumPy array |
| 171 | +fig = px.bar(gapminder, x='year', y=gdp, color='continent', labels={'y':'log gdp'}, |
| 172 | + hover_data=['country'], |
| 173 | + title='Evolution of world GDP') |
| 174 | +fig.show() |
| 175 | +``` |
0 commit comments