Skip to content

Commit 7dd2b59

Browse files
checkpoint
1 parent 5dcec66 commit 7dd2b59

File tree

2 files changed

+79
-10
lines changed

2 files changed

+79
-10
lines changed

doc/python/figure-labels.md

+64-7
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.2.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.7.3
23+
version: 3.7.7
2424
plotly:
2525
description: How to set the global font, title, legend-entries, and axis-titles
2626
in python.
@@ -34,6 +34,62 @@ jupyter:
3434
thumbnail: thumbnail/figure-labels.png
3535
---
3636

37+
### Automatic Labelling with Plotly Express
38+
39+
[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/).
40+
41+
When using Plotly Express, your axes and legend are automatically labelled, and it's easy to override the automation for a customized figure using the `labels` keyword argument. The title of your figure is up to you though!
42+
43+
Here's a figure with automatic labels and then the same figure with overridden labels. Note the fact that when overriding labels, the axes, legend title *and hover labels* reflect the specified labels automatically.
44+
45+
```python
46+
import plotly.express as px
47+
48+
df = px.data.iris()
49+
fig = px.scatter(df, x="sepal_length", y="sepal_width", color="species",
50+
title="Automatic Labels Based on Data Frame Column Names")
51+
fig.show()
52+
```
53+
54+
```python
55+
import plotly.express as px
56+
57+
df = px.data.iris()
58+
fig = px.scatter(df, x="sepal_length", y="sepal_width", color="species",
59+
labels={
60+
"sepal_length": "Sepal Length (cm)",
61+
"sepal_width": "Sepal Width (cm)",
62+
"species": "Species of Iris"
63+
},
64+
title="Manually Specified Labels")
65+
fig.show()
66+
```
67+
68+
### Global and Local Font Specification
69+
70+
You can set the figure-wide font with the `layout.font` attribute, which will apply to all titles and tick labels, but this can be overridden for specific plot items like individual axes and legend titles etc. In the following figure, we set the figure-wide font to Courier New in blue, and then override this for certain parts of the figure.
71+
72+
```python
73+
import plotly.express as px
74+
75+
df = px.data.iris()
76+
fig = px.scatter(df, x="sepal_length", y="sepal_width", color="species",
77+
title="Playing with Fonts")
78+
fig.update_layout(
79+
font_family="Courier New",
80+
font_color="blue",
81+
title_font_family="Times New Roman",
82+
title_font_color="red",
83+
legend_title_font_color="green"
84+
)
85+
fig.update_xaxes(title_font_family="Arial")
86+
fig.show()
87+
```
88+
89+
### Manual Labelling with Graph Objects
90+
91+
When using (graph objects)[/python/graph-objects/] rather than [Plotly Express](/python/plotly-express/), you will need to explicitly label traces and axes:
92+
3793
```python
3894
import plotly.graph_objects as go
3995

@@ -54,12 +110,13 @@ fig.add_trace(go.Scatter(
54110

55111
fig.update_layout(
56112
title="Plot Title",
57-
xaxis_title="x Axis Title",
58-
yaxis_title="y Axis Title",
113+
xaxis_title="X Axis Title",
114+
yaxis_title="X Axis Title",
115+
legend_title="Legend Title",
59116
font=dict(
60117
family="Courier New, monospace",
61118
size=18,
62-
color="#7f7f7f"
119+
color="RebeccaPurple"
63120
)
64121
)
65122

@@ -90,4 +147,4 @@ fig.show()
90147
```
91148

92149
#### Reference
93-
See https://plotly.com/python/reference/#layout for more information!
150+
See https://plotly.com/python/reference/#layout for more information!

doc/python/legend.md

+15-3
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.0
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.3
23+
version: 3.7.7
2424
plotly:
2525
description: How to configure and style the legend in Plotly with Python.
2626
display_as: file_settings
@@ -33,6 +33,18 @@ jupyter:
3333
thumbnail: thumbnail/legends.gif
3434
---
3535

36+
### Legends versus Color Bars
37+
38+
Plotly figures can contain two different legend-like guides: legends and color bars. Legends are used to provide a key for discrete visual variables such as [discrete colors](/python/discrete-color/) or [symbols](/python/marker-style/), whereas [color bars are used to provide a key for continuous color, and are configured differently](/python/colorscales/).
39+
40+
41+
### Legends with Plotly Express
42+
43+
[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/).
44+
45+
Plotly Express will automatically construct and label a legend for any figure that uses [discrete color](/python/discrete-color/) or marker [symbols](/python/marker-style/)
46+
47+
3648
#### Show Legend
3749

3850
By default the legend is displayed on Plotly charts with multiple traces.
@@ -434,4 +446,4 @@ fig.show()
434446

435447
#### Reference
436448

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

0 commit comments

Comments
 (0)