Skip to content

Commit 8bc7e5f

Browse files
Merge pull request #2799 from plotly/doc-prod
Doc prod
2 parents 8289eb1 + 3813ee8 commit 8bc7e5f

12 files changed

+463
-241
lines changed

Diff for: doc/python/axes.md

+138-209
Large diffs are not rendered by default.

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

+4-2
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.3.4
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.7.0
23+
version: 3.7.7
2424
plotly:
2525
description: How to make Bar Charts in Python with Plotly.
2626
display_as: basic
@@ -112,6 +112,7 @@ fig.show()
112112
```python
113113
# Change the default stacking
114114
import plotly.express as px
115+
df = px.data.tips()
115116
fig = px.bar(df, x="sex", y="total_bill",
116117
color='smoker', barmode='group',
117118
height=400)
@@ -124,6 +125,7 @@ Use the keyword arguments `facet_row` (resp. `facet_col`) to create facetted sub
124125

125126
```python
126127
import plotly.express as px
128+
df = px.data.tips()
127129
fig = px.bar(df, x="sex", y="total_bill", color="smoker", barmode="group",
128130
facet_row="time", facet_col="day",
129131
category_orders={"day": ["Thur", "Fri", "Sat", "Sun"],

Diff for: doc/python/categorical-axes.md

+191
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
---
2+
jupyter:
3+
jupytext:
4+
notebook_metadata_filter: all
5+
text_representation:
6+
extension: .md
7+
format_name: markdown
8+
format_version: '1.2'
9+
jupytext_version: 1.4.2
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.7
24+
plotly:
25+
description: How to use categorical axes in Python with Plotly.
26+
display_as: basic
27+
language: python
28+
layout: base
29+
name: Categorical Axes
30+
order: 16
31+
page_type: example_index
32+
permalink: python/categorical-axes/
33+
thumbnail: thumbnail/bar.jpg
34+
---
35+
36+
37+
This page shows examples of how to configure [2-dimensional Cartesian axes](/python/figure-structure/#2d-cartesian-trace-types-and-subplots) to visualize categorical (i.e. qualitative, nominal or ordinal data as opposed to continuous numerical data). Such axes are a natural fit for bar charts, waterfall charts, funnel charts, heatmaps, violin charts and box plots, but can also be used with scatter plots and line charts. [Configuring gridlines, ticks, tick labels and axis titles](/python/axes/) on logarithmic axes is done the same was as with [linear axes](/python/axes/).
38+
39+
### 2-D Cartesian Axis Type and Auto-Detection
40+
41+
The different types of Cartesian axes are configured via the `xaxis.type` or `yaxis.type` attribute, which can take on the following values:
42+
43+
- `'linear'` (see the [linear axes tutoria](/python/axes/))
44+
- `'log'` (see the [log plot tutorial](/python/log-plots/))
45+
- `'date'` (see the [tutorial on timeseries](/python/time-series/))
46+
- `'category'` see below
47+
- `'multicategory'` see below
48+
49+
The axis type is auto-detected by looking at data from the first [trace](/python/figure-structure/) linked to this axis:
50+
51+
* First check for `multicategory`, then `date`, then `category`, else default to `linear` (`log` is never automatically selected)
52+
* `multicategory` is just a shape test: is the array nested?
53+
* `date` and `category`: require **more than twice as many distinct date or category strings** as distinct numbers or numeric strings in order to choose that axis type.
54+
* Both of these test an evenly-spaced sample of at most 1000 values
55+
* Small detail: the `category` test sorts every value into either number or category, whereas for dates, 2- and 4-digit integers count as both dates and numbers.
56+
57+
58+
### Forcing an axis to be categorical
59+
60+
If you pass string values for the `x` or `y` parameter, plotly will automatically set the corresponding axis type to `category`, *except if enough of these strings contain numbers* as detailed above, in which case the axis is automatically set to `linear`. It is however possible to force the axis type by setting explicitely `xaxis_type` to be `category`.
61+
62+
```python
63+
import plotly.express as px
64+
fig = px.bar(x=[1, 2, 4, 10], y =[8, 6, 11, 5])
65+
fig.update_xaxes(type='category')
66+
fig.show()
67+
```
68+
69+
### Controlling the Category Order with Plotly Express
70+
71+
[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/).
72+
73+
By default, Plotly Express lays out categorical data in the order in which it appears in the underlying data. Every 2-d cartesian Plotly Express function also includes a `category_orders` keyword argument which can be used to control the order in which categorical axes are drawn, but beyond that can also control [the order in which discrete colors appear in the legend](/python/discrete-color/), and [the order in which facets are laid out](/python/facet-plots/).
74+
75+
```python
76+
import plotly.express as px
77+
df = px.data.tips()
78+
fig = px.bar(df, x="day", y="total_bill", color="smoker", barmode="group", facet_col="sex",
79+
category_orders={"day": ["Thur", "Fri", "Sat", "Sun"],
80+
"smoker": ["Yes", "No"],
81+
"sex": ["Male", "Female"]})
82+
fig.show()
83+
```
84+
85+
### Automatically Sorting Categories by Name or Total Value
86+
87+
Whether using Plotly Express or not, categories can be sorted alphabetically or by value using the `categoryorder` attribute:
88+
89+
Set `categoryorder` to `"category ascending"` or `"category descending"` for the alphanumerical order of the category names or `"total ascending"` or `"total descending"` for numerical order of values. [categoryorder](https://plotly.com/python/reference/layout/xaxis/#layout-xaxis-categoryorder) for more information. Note that sorting the bars by a particular trace isn't possible right now - it's only possible to sort by the total values. Of course, you can always sort your data _before_ plotting it if you need more customization.
90+
91+
This example orders the categories **alphabetically** with `categoryorder: 'category ascending'`
92+
93+
```python
94+
import plotly.graph_objects as go
95+
96+
x=['b', 'a', 'c', 'd']
97+
fig = go.Figure(go.Bar(x=x, y=[2,5,1,9], name='Montreal'))
98+
fig.add_trace(go.Bar(x=x, y=[1, 4, 9, 16], name='Ottawa'))
99+
fig.add_trace(go.Bar(x=x, y=[6, 8, 4.5, 8], name='Toronto'))
100+
101+
fig.update_layout(barmode='stack')
102+
fig.update_xaxes(categoryorder='category ascending')
103+
fig.show()
104+
```
105+
106+
This example orders the categories **by total value** with `categoryorder: 'total descending'`
107+
108+
```python
109+
import plotly.graph_objects as go
110+
111+
x=['b', 'a', 'c', 'd']
112+
fig = go.Figure(go.Bar(x=x, y=[2,5,1,9], name='Montreal'))
113+
fig.add_trace(go.Bar(x=x, y=[1, 4, 9, 16], name='Ottawa'))
114+
fig.add_trace(go.Bar(x=x, y=[6, 8, 4.5, 8], name='Toronto'))
115+
116+
fig.update_layout(barmode='stack')
117+
fig.update_xaxes(categoryorder='total ascending')
118+
fig.show()
119+
```
120+
121+
This example shows how to control category order when using `plotly.graph_objects` by defining `categoryorder` to "array" to derive the ordering from the attribute `categoryarray`.
122+
123+
```python
124+
import plotly.graph_objects as go
125+
126+
x=['b', 'a', 'c', 'd']
127+
fig = go.Figure(go.Bar(x=x, y=[2,5,1,9], name='Montreal'))
128+
fig.add_trace(go.Bar(x=x, y=[1, 4, 9, 16], name='Ottawa'))
129+
fig.add_trace(go.Bar(x=x, y=[6, 8, 4.5, 8], name='Toronto'))
130+
131+
fig.update_layout(barmode='stack')
132+
fig.update_xaxes(categoryorder='array', categoryarray= ['d','a','c','b'])
133+
fig.show()
134+
```
135+
### Gridlines, Ticks and Tick Labels
136+
137+
138+
By default, gridlines and ticks are not shown on categorical axes but they can be activated:
139+
140+
```python
141+
import plotly.express as px
142+
143+
fig = px.bar(x=["A","B","C"], y=[1,3,2])
144+
fig.update_xaxes(showgrid=True, ticks="outside")
145+
fig.show()
146+
```
147+
148+
By default, ticks and gridlines appear on the categories but the `tickson` attribute can be used to move them to the category boundaries:
149+
150+
```python
151+
import plotly.express as px
152+
153+
fig = px.bar(x=["A","B","C"], y=[1,3,2])
154+
fig.update_xaxes(showgrid=True, ticks="outside", tickson="boundaries")
155+
fig.show()
156+
```
157+
158+
### Multi-categorical Axes
159+
160+
A two-level categorical axis (also known as grouped or hierarchical categories, or sub-categories) can be created by specifying a trace's `x` or `y` property as a 2-dimensional lists. The first sublist represents the outer categorical value while the second sublist represents the inner categorical value. This is only possible with `plotly.graph_objects` at the moment, and not Plotly Express.
161+
162+
Passing in a two-dimensional list as the `x` or `y` value of a trace causes [the `type` of the corresponding axis](/python/axes/) to be set to `multicategory`.
163+
164+
Here is an example that creates a figure with 2 `bar` traces with a 2-level categorical x-axis.
165+
166+
```python
167+
import plotly.graph_objects as go
168+
169+
fig = go.Figure()
170+
171+
fig.add_trace(go.Bar(
172+
x = [['First', 'First', 'Second', 'Second'],
173+
["A", "B", "A", "B"]],
174+
y = [2, 3, 1, 5],
175+
name = "Adults",
176+
))
177+
178+
fig.add_trace(go.Bar(
179+
x = [['First', 'First', 'Second', 'Second'],
180+
["A", "B", "A", "B"]],
181+
y = [8, 3, 6, 5],
182+
name = "Children",
183+
))
184+
185+
fig.update_layout(title_text="Multi-category axis")
186+
187+
fig.show()
188+
```
189+
### Reference
190+
191+
See https://plotly.com/python/reference/layout/xaxis/ for more information and chart attribute options!

Diff for: doc/python/facet-plots.md

+17-3
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ fig.show()
107107

108108
Since subplot figure titles are [annotations](https://plotly.com/python/text-and-annotations/#simple-annotation), you can use the `for_each_annotation` function to customize them, for example to remove the equal-sign (`=`).
109109

110-
In the following example, we pass a lambda function to `for_each_annotation` in order to change the figure subplot titles from `smoker=No` and `smoker=Yes` to just `No` and `Yes`.
110+
In the following example, we pass a lambda function to `for_each_annotation` in order to change the figure subplot titles from `smoker=No` and `smoker=Yes` to just `No` and `Yes`.
111111

112112
```python
113113
import plotly.express as px
@@ -117,9 +117,23 @@ fig.for_each_annotation(lambda a: a.update(text=a.text.split("=")[-1]))
117117
fig.show()
118118
```
119119

120+
### Controlling Facet Ordering
121+
122+
By default, Plotly Express lays out categorical data in the order in which it appears in the underlying data. Every 2-d cartesian Plotly Express function also includes a `category_orders` keyword argument which can be used to control [the order in which categorical axes are drawn](/python/categorical-axes/), but beyond that can also control [the order in which discrete colors appear in the legend](/python/discrete-color/), and the order in which facets are laid out.
123+
124+
```python
125+
import plotly.express as px
126+
df = px.data.tips()
127+
fig = px.bar(df, x="day", y="total_bill", color="smoker", barmode="group", facet_col="sex",
128+
category_orders={"day": ["Thur", "Fri", "Sat", "Sun"],
129+
"smoker": ["Yes", "No"],
130+
"sex": ["Male", "Female"]})
131+
fig.show()
132+
```
133+
120134
### Controlling Facet Spacing
121135

122-
The `facet_row_spacing` and `facet_col_spacing` arguments can be used to control the spacing between rows and columns. These values are specified in fractions of the plotting area in paper coordinates and not in pixels, so they will grow or shrink with the `width` and `height` of the figure.
136+
The `facet_row_spacing` and `facet_col_spacing` arguments can be used to control the spacing between rows and columns. These values are specified in fractions of the plotting area in paper coordinates and not in pixels, so they will grow or shrink with the `width` and `height` of the figure.
123137

124138
The defaults work well with 1-4 rows or columns at the default figure size with the default font size, but need to be reduced to around 0.01 for very large figures or figures with many rows or columns. Conversely, if activating tick labels on all facets, the spacing will need to be increased.
125139

@@ -140,7 +154,7 @@ fig.show()
140154

141155
### Synchronizing axes in subplots with `matches`
142156

143-
Using `facet_col` from `plotly.express` let [zoom](https://help.plotly.com/zoom-pan-hover-controls/#step-3-zoom-in-and-zoom-out-autoscale-the-plot) and [pan](https://help.plotly.com/zoom-pan-hover-controls/#step-6-pan-along-axes) each facet to the same range implicitly. However, if the subplots are created with `make_subplots`, the axis needs to be updated with `matches` parameter to update all the subplots accordingly.
157+
Using `facet_col` from `plotly.express` let [zoom](https://help.plotly.com/zoom-pan-hover-controls/#step-3-zoom-in-and-zoom-out-autoscale-the-plot) and [pan](https://help.plotly.com/zoom-pan-hover-controls/#step-6-pan-along-axes) each facet to the same range implicitly. However, if the subplots are created with `make_subplots`, the axis needs to be updated with `matches` parameter to update all the subplots accordingly.
144158

145159
Zoom in one trace below, to see the other subplots zoomed to the same x-axis range. To pan all the subplots, click and drag from the center of x-axis to the side:
146160

Diff for: doc/python/figure-factories.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ The following types of plots are still difficult to create with Graph Objects or
5151
Deprecated "legacy" Figure Factories include:
5252

5353
* [County Choropleth Maps](/python/county-choropleth/), deprecated by regular [Choropleth maps with GeoJSON input](/python/choropleth-maps/)
54-
* [Distplots](/python/distplot/), mostly deprecated by [`px.histogram`](/python/histograms/)
54+
* [Distplots](/python/distplot/), mostly deprecated by [`px.histogram`](/python/histograms/) except for KDE plots, which `px.histogram` doesn't support yet
5555
* [Gantt Charts](/python/gantt/), deprecated by [`px.timeline`](/python/gantt/)
5656

5757
#### Reference
5858

59-
For more information about the contents of `plotly.figure_factory`, including deprecated methods, please refer to our [API Reference documentation](https://plotly.com/python-api-reference/plotly.figure_factory.html).
59+
For more information about the contents of `plotly.figure_factory`, including deprecated methods, please refer to our [API Reference documentation](https://plotly.com/python-api-reference/plotly.figure_factory.html).

Diff for: doc/python/figure-structure.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ The following trace types are compatible with 2d-cartesian subplots via the `xax
118118
* scatter-like trace types: [`scatter`](/python/line-and-scatter/) and [`scattergl`](/python/webgl-vs-svg/), which can be used to draw [scatter plots](/python/line-and-scatter/), [line plots and curves](/python/line-charts/), [time-series plots](/python/time-series/), [bubble charts](/python/bubble-charts/), [dot plots](/python/dot-plots/) and [filled areas](/python/filled-area-plots/) and also support [error bars](/python/error-bars/)
119119
* [`bar`](/python/bar-charts/), [`funnel`](/python/funnel-charts/), [`waterfall`](/python/waterfall-charts/): bar-like trace types which can also be used to draw [timelines and Gantt charts](/python/gantt/)
120120
* [`histogram`](/python/histograms/): an *aggregating* bar-like trace type
121-
* [`box`](/python/box-plots/) and `violin`: 1-dimensional distribution-like trace types
121+
* [`box`](/python/box-plots/) and [`violin`](/python/box-plots/): 1-dimensional distribution-like trace types
122122
* [`histogram2d`](/python/2D-Histogram/) and [`histogram2dcontour`](/python/2d-histogram-contour/): 2-dimensional distribution-like density trace types
123123
* [`image`](/python/imshow/), [`heatmap`](/python/heatmaps/) and [`contour`](/python/contour-plots/): matrix trace types
124124
* [`ohlc`](/python/ohlc-charts/) and [`candlestick`](/python/candlestick-charts/): stock-like trace types

Diff for: doc/python/histograms.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ jupyter:
3030
order: 3
3131
page_type: example_index
3232
permalink: python/histograms/
33-
redirect_from: /python/histogram-tutorial/
33+
redirect_from:
34+
- /python/histogram-tutorial/
35+
- /python/histogram/
3436
thumbnail: thumbnail/histogram.jpg
3537
---
3638

@@ -392,4 +394,4 @@ fig.show()
392394

393395
#### Reference
394396

395-
See https://plotly.com/python/reference/histogram/ for more information and chart attribute options!
397+
See https://plotly.com/python/reference/histogram/ for more information and chart attribute options!

Diff for: doc/python/legend.md

+41-1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,46 @@ fig = px.scatter(df, x="total_bill", y="tip", color="sex", symbol="smoker", face
5757
fig.show()
5858
```
5959

60+
### Legend Order
61+
62+
By default, Plotly Express lays out legend items in the order in which values appear in the underlying data. Every Plotly Express function also includes a `category_orders` keyword argument which can be used to control [the order in which categorical axes are drawn](/python/categorical-axes/), but beyond that can also control the order in which legend items appear, and [the order in which facets are laid out](/python/facet-plots/).
63+
64+
```python
65+
import plotly.express as px
66+
df = px.data.tips()
67+
fig = px.bar(df, x="day", y="total_bill", color="smoker", barmode="group", facet_col="sex",
68+
category_orders={"day": ["Thur", "Fri", "Sat", "Sun"],
69+
"smoker": ["Yes", "No"],
70+
"sex": ["Male", "Female"]})
71+
fig.show()
72+
```
73+
74+
When using stacked bars, the bars are stacked from the bottom in the same order as they appear in the legend, so it can make sense to set `layout.legend.traceorder` to `"reversed"` to get the legend and stacks to match:
75+
76+
```python
77+
import plotly.express as px
78+
df = px.data.tips()
79+
fig = px.bar(df, x="day", y="total_bill", color="smoker", barmode="stack", facet_col="sex",
80+
category_orders={"day": ["Thur", "Fri", "Sat", "Sun"],
81+
"smoker": ["Yes", "No"],
82+
"sex": ["Male", "Female"]})
83+
fig.update_layout(legend_traceorder="reversed")
84+
fig.show()
85+
```
86+
87+
When using [`plotly.graph_objects`](/python/graph-objects/) rather than Plotly Express, legend items will appear in the order that traces appear in the `data`:
88+
89+
```python
90+
import plotly.graph_objects as go
91+
92+
fig = go.Figure()
93+
fig.add_trace(go.Bar(name="first", x=["a", "b"], y=[1,2]))
94+
fig.add_trace(go.Bar(name="second", x=["a", "b"], y=[2,1]))
95+
fig.add_trace(go.Bar(name="third", x=["a", "b"], y=[1,2]))
96+
fig.add_trace(go.Bar(name="fourth", x=["a", "b"], y=[2,1]))
97+
fig.show()
98+
```
99+
60100
#### Showing and Hiding the Legend
61101

62102
By default the legend is displayed on Plotly charts with multiple traces, and this can be explicitly set with the `layout.showlegend` attribute:
@@ -419,4 +459,4 @@ fig.show()
419459

420460
#### Reference
421461

422-
See https://plotly.com/python/reference/layout/#layout-legend for more information!
462+
See https://plotly.com/python/reference/layout/#layout-legend for more information!

0 commit comments

Comments
 (0)