Skip to content

Commit c67f800

Browse files
Merge pull request #3326 from plotly/px_line_marker
ability to show markers on lines
2 parents ce1bf36 + 3befb2c commit c67f800

File tree

12 files changed

+265
-36
lines changed

12 files changed

+265
-36
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
77

88
### Added
99
- Extra flags were added to the `gapminder` and `stocks` dataset to facilitate testing, documentation and demos [#3305](https://github.com/plotly/plotly.py/issues/3305)
10+
- All line-like Plotly Express functions now accept `markers` argument to display markers, and all but `line_mapbox` accept `symbol` to map a field to the symbol attribute, similar to scatter-like functions [#3326](https://github.com/plotly/plotly.py/issues/3326)
1011

1112
### Fixed
1213
- Fixed regression introduced in version 5.0.0 where pandas/numpy arrays with `dtype` of Object were being converted to `list` values when added to a Figure ([#3292](https://github.com/plotly/plotly.py/issues/3292), [#3293](https://github.com/plotly/plotly.py/pull/3293))

doc/python/axes.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ Other kinds of subplots and axes are described in other tutorials:
5151
The different types of Cartesian axes are configured via the `xaxis.type` or `yaxis.type` attribute, which can take on the following values:
5252

5353
- `'linear'` as described in this page
54-
- `'log'` (see the [log plot tutorial](/python/log-plots/))
54+
- `'log'` (see the [log plot tutorial](/python/log-plot/))
5555
- `'date'` (see the [tutorial on timeseries](/python/time-series/))
5656
- `'category'` (see the [categorical axes tutorial](/python/categorical-axes/))
5757
- `'multicategory'` (see the [categorical axes tutorial](/python/categorical-axes/))

doc/python/bubble-charts.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ jupyter:
55
text_representation:
66
extension: .md
77
format_name: markdown
8-
format_version: '1.1'
9-
jupytext_version: 1.1.1
8+
format_version: '1.2'
9+
jupytext_version: 1.4.2
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.7
2424
plotly:
2525
description: How to make bubble charts in Python with Plotly.
2626
display_as: basic
@@ -36,7 +36,7 @@ jupyter:
3636

3737
## Bubble chart with plotly.express
3838

39-
A [bubble chart](https://en.wikipedia.org/wiki/Bubble_chart) is a scatter plot in which a third dimension of the data is shown through the size of markers. For other types of scatter plot, see the [line and scatter page](https://plotly.com/python/line-and-scatter/).
39+
A [bubble chart](https://en.wikipedia.org/wiki/Bubble_chart) is a scatter plot in which a third dimension of the data is shown through the size of markers. For other types of scatter plot, see the [scatter plot documentation](https://plotly.com/python/line-and-scatter/).
4040

4141
We first show a bubble chart example using Plotly Express. [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/). The size of markers is set from the dataframe column given as the `size` parameter.
4242

@@ -222,4 +222,4 @@ fig.show()
222222

223223
### Reference
224224

225-
See https://plotly.com/python/reference/scatter/ for more information and chart attribute options!
225+
See https://plotly.com/python/reference/scatter/ for more information and chart attribute options!

doc/python/categorical-axes.md

+34-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ This page shows examples of how to configure [2-dimensional Cartesian axes](/pyt
4141
The different types of Cartesian axes are configured via the `xaxis.type` or `yaxis.type` attribute, which can take on the following values:
4242

4343
- `'linear'` (see the [linear axes tutorial](/python/axes/))
44-
- `'log'` (see the [log plot tutorial](/python/log-plots/))
44+
- `'log'` (see the [log plot tutorial](/python/log-plot/))
4545
- `'date'` (see the [tutorial on timeseries](/python/time-series/))
4646
- `'category'` see below
4747
- `'multicategory'` see below
@@ -64,6 +64,39 @@ fig.update_xaxes(type='category')
6464
fig.show()
6565
```
6666

67+
### Categorical Axes and Trace Types
68+
69+
Every cartesian trace type is compatible with categorical axes, not just `bar`.
70+
71+
Scatter plots where one axis is categorical are often known as [dot plots](https://plotly.com/python/dot-plots/).
72+
73+
```python
74+
import plotly.express as px
75+
df = px.data.medals_long()
76+
77+
fig = px.scatter(df, y="nation", x="count", color="medal", symbol="medal")
78+
fig.update_traces(marker_size=10)
79+
fig.show()
80+
```
81+
82+
[Box plots]() and [violin plots]() are often shown with one categorical and one continuous axis.
83+
84+
```python
85+
import plotly.express as px
86+
df = px.data.tips()
87+
88+
fig = px.box(df, x="sex", y="total_bill", color="smoker")
89+
fig.show()
90+
```
91+
92+
```python
93+
import plotly.express as px
94+
df = px.data.tips()
95+
96+
fig = px.violin(df, x="sex", y="total_bill", color="smoker")
97+
fig.show()
98+
```
99+
67100
### Controlling the Category Order with Plotly Express
68101

69102
[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/).

doc/python/dot-plots.md

+13-4
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ jupyter:
55
text_representation:
66
extension: .md
77
format_name: markdown
8-
format_version: '1.1'
9-
jupytext_version: 1.1.1
8+
format_version: '1.2'
9+
jupytext_version: 1.4.2
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.7
2424
plotly:
2525
description: How to make dot plots in Python with Plotly.
2626
display_as: basic
@@ -35,12 +35,21 @@ jupyter:
3535

3636
#### Basic Dot Plot
3737

38-
Dot plots (also known as [Cleveland dot plots](<https://en.wikipedia.org/wiki/Dot_plot_(statistics)>)) show changes between two (or more) points in time or between two (or more) conditions. Compared to a [bar chart](/python/bar-charts/), dot plots can be less cluttered and allow for an easier comparison between conditions.
38+
Dot plots (also known as [Cleveland dot plots](<https://en.wikipedia.org/wiki/Dot_plot_(statistics)>)) are [scatter plots](https://plotly.com/python/line-and-scatter/) with one categorical axis and one continuous axis. They can be used to show changes between two (or more) points in time or between two (or more) conditions. Compared to a [bar chart](/python/bar-charts/), dot plots can be less cluttered and allow for an easier comparison between conditions.
3939

4040
For the same data, we show below how to create a dot plot using either `px.scatter` or `go.Scatter`.
4141

4242
[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/).
4343

44+
```python
45+
import plotly.express as px
46+
df = px.data.medals_long()
47+
48+
fig = px.scatter(df, y="nation", x="count", color="medal", symbol="medal")
49+
fig.update_traces(marker_size=10)
50+
fig.show()
51+
```
52+
4453
```python
4554
import plotly.express as px
4655
import pandas as pd

doc/python/line-and-scatter.md

+99-7
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,7 +34,7 @@ jupyter:
3434
thumbnail: thumbnail/line-and-scatter.jpg
3535
---
3636

37-
## Scatter plot with Plotly Express
37+
## Scatter 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/).
4040

@@ -55,9 +55,9 @@ fig = px.scatter(df, x="sepal_width", y="sepal_length")
5555
fig.show()
5656
```
5757

58-
#### Set size and color with column names
58+
#### Setting size and color with column names
5959

60-
Note that `color` and `size` data are added to hover information. You can add other columns to hover data with the `hover_data` argument of `px.scatter`.
60+
Scatter plots with variable-sized circular markers are often known as [bubble charts](https://plotly.com/python/bubble-charts/). Note that `color` and `size` data are added to hover information. You can add other columns to hover data with the `hover_data` argument of `px.scatter`.
6161

6262
```python
6363
import plotly.express as px
@@ -67,7 +67,16 @@ fig = px.scatter(df, x="sepal_width", y="sepal_length", color="species",
6767
fig.show()
6868
```
6969

70-
## Scatter plot in Dash
70+
The `symbol` argument can be mapped to a column as well. A [wide variety of symbols](https://plotly.com/python/marker-style/) are available.
71+
72+
```python
73+
import plotly.express as px
74+
df = px.data.iris()
75+
fig = px.scatter(df, x="sepal_width", y="sepal_length", color="species", symbol="species")
76+
fig.show()
77+
```
78+
79+
## Scatter plots in Dash
7180

7281
[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`.
7382

@@ -80,7 +89,22 @@ snippet_url = 'https://dash-gallery.plotly.host/python-docs-dash-snippets/'
8089
IFrame(snippet_url + 'line-and-scatter', width='100%', height=630)
8190
```
8291

83-
## Line plot with Plotly Express
92+
### Scatter plots and Categorical Axes
93+
94+
Scatters 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](https://plotly.com/python/time-series/) axes.
95+
96+
Scatter plots where one axis is categorical are often known as [dot plots](https://plotly.com/python/dot-plots/).
97+
98+
```python
99+
import plotly.express as px
100+
df = px.data.medals_long()
101+
102+
fig = px.scatter(df, y="nation", x="count", color="medal", symbol="medal")
103+
fig.update_traces(marker_size=10)
104+
fig.show()
105+
```
106+
107+
## Line plots with Plotly Express
84108

85109
```python
86110
import plotly.express as px
@@ -99,7 +123,75 @@ fig = px.line(df, x='year', y='lifeExp', color='country')
99123
fig.show()
100124
```
101125

102-
## Scatter and line plot with go.Scatter
126+
The `markers` argument can be set to `True` to show markers on lines.
127+
128+
```python
129+
import plotly.express as px
130+
df = px.data.gapminder().query("continent == 'Oceania'")
131+
fig = px.line(df, x='year', y='lifeExp', color='country', markers=True)
132+
fig.show()
133+
```
134+
135+
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.
136+
137+
```python
138+
import plotly.express as px
139+
df = px.data.gapminder().query("continent == 'Oceania'")
140+
fig = px.line(df, x='year', y='lifeExp', color='country', symbol="country")
141+
fig.show()
142+
```
143+
144+
### Line plots on Date axes
145+
146+
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/).
147+
148+
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).
149+
150+
```python
151+
import plotly.express as px
152+
153+
df = px.data.stocks()
154+
fig = px.line(df, x='date', y="GOOG")
155+
fig.show()
156+
```
157+
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+
194+
## Scatter and line plots with go.Scatter
103195

104196
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/).
105197

0 commit comments

Comments
 (0)