Skip to content

Commit 824a41b

Browse files
authored
Tutorial on px arguments
1 parent e9e4bc2 commit 824a41b

7 files changed

+208
-10
lines changed

python/3d-scatter-plots.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ jupyter:
3939

4040
## 3D scatter plot with plotly express
4141

42-
Like the [2D scatter plot](https://plot.ly/python/line-and-scatter/) `px.scatter`, the 3D function `px.scatter_3d` plots individual data in three-dimensional space. Note that [Plotly Express](../plotly-express/) functions take as a first argument a [tidy `pandas.DataFrame`](https://www.jeannicholashould.com/tidy-data-in-python.html) such as the ones defined in ``px.data``.
42+
Like the [2D scatter plot](https://plot.ly/python/line-and-scatter/) `px.scatter`, the 3D function `px.scatter_3d` plots individual data in three-dimensional space.
4343

4444
```python
4545
import plotly.express as px

python/bar-charts.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ jupyter:
3939

4040
### Bar chart with plotly express
4141

42-
[Plotly Express](../plotly-express/) functions take as a first argument a [tidy `pandas.DataFrame`](https://www.jeannicholashould.com/tidy-data-in-python.html).
4342

4443
In a bar plot, each row of the DataFrame is represented as a rectangular mark.
4544

python/box-plots.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,8 @@ A [box plot](https://en.wikipedia.org/wiki/Box_plot) is a statistical representa
4242

4343
## Box Plot with plotly express
4444

45-
[Plotly Express](../plotly-express/) functions take as a first argument a [tidy `pandas.DataFrame`](https://www.jeannicholashould.com/tidy-data-in-python.html). In a box plot created by `px.box`, the distribution of the column given as `y` argument is represented.
45+
In a box plot created by `px.box`, the distribution of the column given as `y` argument is represented.
4646

47-
If your data are not available as a tidy dataframe, you can use ``go.Box`` as [described below](https://plot.ly/python/box-plots/#box-plot-with-go.Box).
4847

4948
```python
5049
import plotly.express as px

python/line-and-scatter.md

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ jupyter:
66
extension: .md
77
format_name: markdown
88
format_version: '1.1'
9-
jupytext_version: 1.1.1
9+
jupytext_version: 1.2.3
1010
kernelspec:
1111
display_name: Python 3
1212
language: python
@@ -20,7 +20,7 @@ jupyter:
2020
name: python
2121
nbconvert_exporter: python
2222
pygments_lexer: ipython3
23-
version: 3.6.7
23+
version: 3.7.3
2424
plotly:
2525
description: How to make scatter plots in Python with Plotly.
2626
display_as: basic
@@ -40,11 +40,19 @@ jupyter:
4040

4141
## Scatter plot with plotly express
4242

43-
[Plotly Express](../plotly-express/) functions take as a first argument a [tidy `pandas.DataFrame`](https://www.jeannicholashould.com/tidy-data-in-python.html). With ``px.scatter``, each data point is represented as a marker point, which location is given by the `x` and `y` columns.
43+
With ``px.scatter``, each data point is represented as a marker point, which location is given by the `x` and `y` columns.
4444

4545
```python
46+
# x and y given as array_like objects
4647
import plotly.express as px
47-
iris = px.data.iris()
48+
fig = px.scatter(x=[0, 1, 2, 3, 4], y=[0, 1, 4, 9, 16])
49+
fig.show()
50+
```
51+
52+
```python
53+
# x and y given as DataFrame columns
54+
import plotly.express as px
55+
iris = px.data.iris() # iris is a pandas DataFrame
4856
fig = px.scatter(iris, x="sepal_width", y="sepal_length")
4957
fig.show()
5058
```
@@ -63,6 +71,16 @@ fig.show()
6371

6472
## Line plot with plotly express
6573

74+
```python
75+
import plotly.express as px
76+
import numpy as np
77+
78+
t = np.linspace(0, 2*np.pi, 100)
79+
80+
fig = px.line(x=t, y=np.cos(t), labels={'x':'t', 'y':'cos(t)'})
81+
fig.show()
82+
```
83+
6684
```python
6785
import plotly.express as px
6886
gapminder = px.data.gapminder().query("continent == 'Oceania'")

python/plotly-express.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ jupyter:
2020
name: python
2121
nbconvert_exporter: python
2222
pygments_lexer: ipython3
23-
version: 3.6.7
23+
version: 3.7.3
2424
plotly:
2525
description: Plotly Express is a terse, consistent, high-level API for rapid data
2626
exploration and figure generation.
@@ -44,7 +44,7 @@ Plotly Express is a terse, consistent, high-level wrapper around `plotly.graph_o
4444

4545
**Note**: Plotly Express was previously its own separately-installed `plotly_express` package but is now part of `plotly`!
4646

47-
This notebook demonstrates various `plotly.express` features. [Reference documentation](https://plotly.github.io/plotly_express/plotly_express/) is also available.
47+
This notebook demonstrates various `plotly.express` features. [Reference documentation](https://plotly.github.io/plotly_express/plotly_express/) is also available, as well as a [tutorial on input argument types](./px-arguments).
4848

4949
You can also read our original [Medium announcement article](https://medium.com/@plotlygraphs/introducing-plotly-express-808df010143d) for more information on this library.
5050

python/px-arguments.md

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
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+
```

python/sunburst-charts.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@ jupyter:
3737
---
3838

3939
### Basic Sunburst Plot ###
40+
Sunburst plot visualizes hierarchical data spanning outwards radially from root to leaves. The sunburst sectors are determined by the entries in "labels" and in "parents". The root starts from the center and children are added to the outer rings.
41+
42+
Main arguments:
43+
1. **labels**: sets the labels of sunburst sectors.
44+
2. **parents**: sets the parent sectors of sunburst sectors. An empty string '' is used for the root node in the hierarchy. In this example, the root is "Eve".
45+
3. **values**: sets the values associated with sunburst sectors, determining their width (See the "Branchvalues" section below for different modes for setting the width).
46+
4047

4148
Sunburst plots visualize hierarchical data spanning outwards radially from root to leaves. The sunburst sector hierarchy is determined by the entries in `labels` and in `parents`. The root starts from the center and children are added to the outer rings.
4249

0 commit comments

Comments
 (0)