Skip to content

Commit 8390632

Browse files
more line docs
1 parent e399c89 commit 8390632

File tree

2 files changed

+110
-15
lines changed

2 files changed

+110
-15
lines changed

Diff for: doc/python/line-and-scatter.md

+36
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,42 @@ fig = px.line(df, x='date', y="GOOG")
155155
fig.show()
156156
```
157157

158+
### Data Order in Scatter and Line Charts
159+
160+
Plotly line charts are implemented as [connected scatterplots](https://www.data-to-viz.com/graph/connectedscatter.html) (see below), meaning that the points are plotted and connected with lines **in the order they are provided, with no automatic reordering**.
161+
162+
This makes it possible to make charts like the one below, but also means that it may be required to explicitly sort data before passing it to Plotly to avoid lines moving "backwards" across the chart.
163+
164+
```python
165+
import plotly.express as px
166+
import pandas as pd
167+
168+
df = pd.DataFrame(dict(
169+
x = [1, 3, 2, 4],
170+
y = [1, 2, 3, 4]
171+
))
172+
fig = px.line(df, x="x", y="y", title="Unsorted Input")
173+
fig.show()
174+
175+
df = df.sort_values(by="x")
176+
fig = px.line(df, x="x", y="y", title="Sorted Input")
177+
fig.show()
178+
```
179+
180+
### Connected Scatterplots
181+
182+
In a connected scatterplot, two continuous variables are plotted against each other, with a line connecting them in some meaningful order, usually a time variable. In the plot below, we show the "trajectory" of a pair of countries through a space defined by GDP per Capita and Life Expectancy. Botswana's life expectancy
183+
184+
```python
185+
import plotly.express as px
186+
187+
df = px.data.gapminder().query("country in ['Canada', 'Botswana']")
188+
189+
fig = px.line(df, x="lifeExp", y="gdpPercap", color="country", text="year")
190+
fig.update_traces(textposition="bottom right")
191+
fig.show()
192+
```
193+
158194
## Scatter and line plots with go.Scatter
159195

160196
If Plotly Express does not provide a good starting point, it is possible to use [the more generic `go.Scatter` class from `plotly.graph_objects`](/python/graph-objects/). Whereas `plotly.express` has two functions `scatter` and `line`, `go.Scatter` can be used both for plotting points (makers) or lines, depending on the value of `mode`. The different options of `go.Scatter` are documented in its [reference page](https://plotly.com/python/reference/scatter/).

Diff for: doc/python/line-charts.md

+74-15
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ jupyter:
66
extension: .md
77
format_name: markdown
88
format_version: '1.2'
9-
jupytext_version: 1.6.0
9+
jupytext_version: 1.4.2
1010
kernelspec:
1111
display_name: Python 3
1212
language: python
@@ -34,14 +34,12 @@ jupyter:
3434
thumbnail: thumbnail/line-plot.jpg
3535
---
3636

37-
### Line Plot with plotly.express
37+
### Line Plots with plotly.express
3838

3939
[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/). With `px.line`, each data point is represented as a vertex (which location is given by the `x` and `y` columns) of a **polyline mark** in 2D space.
4040

4141
For more examples of line plots, see the [line and scatter notebook](https://plotly.com/python/line-and-scatter/).
4242

43-
#### Simple Line Plot with plotly.express
44-
4543
```python
4644
import plotly.express as px
4745

@@ -50,7 +48,7 @@ fig = px.line(df, x="year", y="lifeExp", title='Life expectancy in Canada')
5048
fig.show()
5149
```
5250

53-
### Line Plot with column encoding color
51+
### Line Plots with column encoding color
5452

5553
```python
5654
import plotly.express as px
@@ -60,26 +58,87 @@ fig = px.line(df, x="year", y="lifeExp", color='country')
6058
fig.show()
6159
```
6260

61+
### Line charts in Dash
62+
63+
[Dash](https://plotly.com/dash/) is the best way to build analytical apps in Python using Plotly figures. To run the app below, run `pip install dash`, click "Download" to get the code and run `python app.py`.
64+
65+
Get started with [the official Dash docs](https://dash.plotly.com/installation) and **learn how to effortlessly [style](https://plotly.com/dash/design-kit/) & [deploy](https://plotly.com/dash/app-manager/) apps like this with <a class="plotly-red" href="https://plotly.com/dash/">Dash Enterprise</a>.**
66+
67+
68+
```python hide_code=true tags=[]
69+
from IPython.display import IFrame
70+
snippet_url = 'https://dash-gallery.plotly.host/python-docs-dash-snippets/'
71+
IFrame(snippet_url + 'line-charts', width='100%', height=630)
72+
```
73+
74+
### Data Order in Line Charts
75+
76+
Plotly line charts are implemented as [connected scatterplots](https://www.data-to-viz.com/graph/connectedscatter.html) (see below), meaning that the points are plotted and connected with lines **in the order they are provided, with no automatic reordering**.
77+
78+
This makes it possible to make charts like the one below, but also means that it may be required to explicitly sort data before passing it to Plotly to avoid lines moving "backwards" across the chart.
79+
6380
```python
6481
import plotly.express as px
82+
import pandas as pd
83+
84+
df = pd.DataFrame(dict(
85+
x = [1, 3, 2, 4],
86+
y = [1, 2, 3, 4]
87+
))
88+
fig = px.line(df, x="x", y="y", title="Unsorted Input")
89+
fig.show()
6590

66-
df = px.data.gapminder().query("continent != 'Asia'") # remove Asia for visibility
67-
fig = px.line(df, x="year", y="lifeExp", color="continent",
68-
line_group="country", hover_name="country")
91+
df = df.sort_values(by="x")
92+
fig = px.line(df, x="x", y="y", title="Sorted Input")
6993
fig.show()
7094
```
7195

72-
### Line chart in Dash
96+
### Connected Scatterplots
7397

74-
[Dash](https://plotly.com/dash/) is the best way to build analytical apps in Python using Plotly figures. To run the app below, run `pip install dash`, click "Download" to get the code and run `python app.py`.
98+
In a connected scatterplot, two continuous variables are plotted against each other, with a line connecting them in some meaningful order, usually a time variable. In the plot below, we show the "trajectory" of a pair of countries through a space defined by GDP per Capita and Life Expectancy. Botswana's life expectancy
7599

76-
Get started with [the official Dash docs](https://dash.plotly.com/installation) and **learn how to effortlessly [style](https://plotly.com/dash/design-kit/) & [deploy](https://plotly.com/dash/app-manager/) apps like this with <a class="plotly-red" href="https://plotly.com/dash/">Dash Enterprise</a>.**
100+
```python
101+
import plotly.express as px
77102

103+
df = px.data.gapminder().query("country in ['Canada', 'Botswana']")
78104

79-
```python hide_code=true
80-
from IPython.display import IFrame
81-
snippet_url = 'https://dash-gallery.plotly.host/python-docs-dash-snippets/'
82-
IFrame(snippet_url + 'line-charts', width='100%', height=630)
105+
fig = px.line(df, x="lifeExp", y="gdpPercap", color="country", text="year")
106+
fig.update_traces(textposition="bottom right")
107+
fig.show()
108+
```
109+
110+
### Line charts with markers
111+
112+
The `markers` argument can be set to `True` to show markers on lines.
113+
114+
```python
115+
import plotly.express as px
116+
df = px.data.gapminder().query("continent == 'Oceania'")
117+
fig = px.line(df, x='year', y='lifeExp', color='country', markers=True)
118+
fig.show()
119+
```
120+
121+
The `symbol` argument can be used to map a data field to the marker symbol. A [wide variety of symbols](https://plotly.com/python/marker-style/) are available.
122+
123+
```python
124+
import plotly.express as px
125+
df = px.data.gapminder().query("continent == 'Oceania'")
126+
fig = px.line(df, x='year', y='lifeExp', color='country', symbol="country")
127+
fig.show()
128+
```
129+
130+
### Line plots on Date axes
131+
132+
Line plots can be made on using any type of cartesian axis, including [linear](https://plotly.com/python/axes/), [logarithmic](https://plotly.com/python/log-plot/), [categorical](https://plotly.com/python/categorical-axes/) or date axes. Line plots on date axes are often called [time-series charts](https://plotly.com/python/time-series/).
133+
134+
Plotly auto-sets the axis type to a date format when the corresponding data are either ISO-formatted date strings or if they're a [date pandas column](https://pandas.pydata.org/pandas-docs/stable/user_guide/timeseries.html) or [datetime NumPy array](https://docs.scipy.org/doc/numpy/reference/arrays.datetime.html).
135+
136+
```python
137+
import plotly.express as px
138+
139+
df = px.data.stocks()
140+
fig = px.line(df, x='date', y="GOOG")
141+
fig.show()
83142
```
84143

85144
### Sparklines with Plotly Express

0 commit comments

Comments
 (0)