Skip to content

Add docs updates for Plotly.py v6.3 #5269

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 34 additions & 2 deletions doc/python/axes.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ jupyter:
extension: .md
format_name: markdown
format_version: '1.3'
jupytext_version: 1.16.3
jupytext_version: 1.17.2
kernelspec:
display_name: Python 3 (ipykernel)
language: python
Expand All @@ -20,7 +20,7 @@ jupyter:
name: python
nbconvert_exporter: python
pygments_lexer: ipython3
version: 3.10.14
version: 3.9.0
plotly:
description: How to adjust axes properties in Python - axes titles, styling and
coloring axes and grid lines, ticks, tick labels and more.
Expand Down Expand Up @@ -619,6 +619,38 @@ fig.update_yaxes(zeroline=True, zerolinewidth=2, zerolinecolor='LightPink')
fig.show()
```

##### Controlling Zero Line Layer

*New in 6.3*

By default, zero lines are displayed below traces. Set `zerolinelayer="above traces"` on an axis to display its zero line above traces:

```python
import plotly.graph_objects as go

x = ['A', 'B', 'C', 'D', 'A']
y = [2, 0, 4, -3, 2]

fig = go.Figure(
data=[
go.Scatter(
x=x,
y=y,
fill='toself',
mode='none',
fillcolor='lightpink'
)
],
layout=dict(
yaxis=dict(
zerolinelayer="above traces" # Change to "below traces" to see the difference
),
)
)

fig.show()
```

#### Setting the Range of Axes Manually

The visible x and y axis range can be configured manually by setting the `range` axis property to a list of two values, the lower and upper bound.
Expand Down
44 changes: 40 additions & 4 deletions doc/python/configuration-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ jupyter:
text_representation:
extension: .md
format_name: markdown
format_version: '1.2'
jupytext_version: 1.4.2
format_version: '1.3'
jupytext_version: 1.17.2
kernelspec:
display_name: Python 3
display_name: Python 3 (ipykernel)
language: python
name: python3
language_info:
Expand All @@ -20,7 +20,7 @@ jupyter:
name: python
nbconvert_exporter: python
pygments_lexer: ipython3
version: 3.7.7
version: 3.12.4
plotly:
description: How to set the configuration options of figures using the Plotly
Python graphing library.
Expand Down Expand Up @@ -323,6 +323,42 @@ fig.update_layout(xaxis={'type': 'date'})
fig.show(config=config)
```

### Disabling Buttons for Specific Axes

*New in 6.3*

Disabling the zoom in, zoom out, and autoscale buttons for specific axes is supported on cartesian axes using the `modebardisable` attribute. In the following example, the zoom in and zoom out buttons are disabled on the `xaxis`, meaning these buttons only zoom in and out on the `yaxis`. Disable the autoscale button using `modebardisable='autoscale'`. You can also disable both autoscaling and zoom buttons using `modebardisable='zoominout+autoscale'`.

```python
import plotly.graph_objects as go
import plotly.data

df = plotly.data.stocks()

fig = go.Figure(
data=[
go.Scatter(
x=df['date'],
y=df['GOOG'],
mode='lines+markers',
name='Google Stock Price'
)
],
layout=go.Layout(
title='Google Stock Price Over Time with Mode Bar Disabled',
xaxis=dict(
title='Date',
# Try zooming in or out using the modebar buttons. These only apply to the yaxis in this exampe.
modebardisable='zoominout'
),
yaxis=dict(
title='Stock Price (USD)',
)
)
)
fig.show()
```

### Configuring Figures in Dash Apps

The same configuration dictionary that you pass to the `config` parameter of the `show()` method can also be passed to the [`config` property of a `dcc.Graph` component](https://dash.plotly.com/dash-core-components/graph).
Expand Down
55 changes: 53 additions & 2 deletions doc/python/hover-text-and-formatting.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ jupyter:
extension: .md
format_name: markdown
format_version: '1.3'
jupytext_version: 1.16.1
jupytext_version: 1.17.2
kernelspec:
display_name: Python 3 (ipykernel)
language: python
Expand All @@ -20,7 +20,7 @@ jupyter:
name: python
nbconvert_exporter: python
pygments_lexer: ipython3
version: 3.10.11
version: 3.12.4
plotly:
description: How to use hover text and formatting in Python with Plotly.
display_as: file_settings
Expand Down Expand Up @@ -83,6 +83,57 @@ fig.update_layout(hovermode="x unified")
fig.show()
```

#### Customize Title in Unified Hovermode

*New in 6.3*

Customize the title shown in unified hovermode, by specifing `unifiedhovertitle.text`.

The unified hover title is a template string that supports using variables from the data. Numbers are formatted using d3-format's syntax `%{variable:d3-format}`, for `example \"Price: %{y:$.2f}\"`. Dates are formatted using d3-time-format's syntax `%{variable|d3-time-format}`, for example `\"Day: %{2019-01-01|%A}\"`.

The following example uses `'x unified'` hover and specifies a unified hover title that shows the full weekday, month, day, and year.

```python
import plotly.graph_objects as go
import plotly.express as px

df = px.data.stocks()

fig = go.Figure(
data=[
go.Scatter(
x=df['date'],
y=df['GOOG'],
mode='lines',
name='Google'
),
go.Scatter(
x=df['date'],
y=df['AAPL'],
mode='lines',
name='Apple'
)
],
layout=go.Layout(
title_text="Stock Prices with Custom Unified Hover Title",
hovermode='x unified',
xaxis=dict(
title_text='Date',
unifiedhovertitle=dict(
text='<b>%{x|%A, %B %d, %Y}</b>'
)
),
yaxis=dict(
title_text='Price (USD)',
tickprefix='$'
)
)
)

fig.show()

```

#### Control hovermode with Dash

[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`.
Expand Down
44 changes: 42 additions & 2 deletions doc/python/legend.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ jupyter:
extension: .md
format_name: markdown
format_version: '1.3'
jupytext_version: 1.16.1
jupytext_version: 1.17.2
kernelspec:
display_name: Python 3 (ipykernel)
language: python
Expand All @@ -20,7 +20,7 @@ jupyter:
name: python
nbconvert_exporter: python
pygments_lexer: ipython3
version: 3.10.11
version: 3.9.0
plotly:
description: How to configure and style the legend in Plotly with Python.
display_as: file_settings
Expand Down Expand Up @@ -254,6 +254,46 @@ fig.update_layout(legend=dict(
fig.show()
```

#### Legend Max Height

*New in 6.3*

By default, a legend can expand to fill up to half of the layout area height for a horizontal legend and the full height for a vertical legend. You can change this by specifying a `maxheight` for the legend. In the following plot with many legend items, we set `maxheight` to a ratio of 0.10, giving the plot more space.

```python
import plotly.express as px
from plotly import data

df = data.gapminder().query("year==2007 and continent == 'Europe'")

fig = px.scatter(df,
x="gdpPercap",
y="lifeExp",
color="country",
size="pop",
size_max=45,
title="Life Expectancy vs. GDP per Capita in 2007 (by Country)",
labels={"gdpPercap": "GDP per Capita"},
)

fig.update_layout(
xaxis=dict(
side="top"
),
legend=dict(
orientation="h",
yanchor="bottom",
y=-0.35,
xanchor="center",
x=0.5,
maxheight=0.1, # Comment maxheight to see legend take up 0.5 of plotting area
title_text="Country"
),
)

fig.show()
```

#### Styling Legends

Legends support many styling options.
Expand Down
36 changes: 34 additions & 2 deletions doc/python/log-plot.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ jupyter:
extension: .md
format_name: markdown
format_version: '1.3'
jupytext_version: 1.13.7
jupytext_version: 1.17.2
kernelspec:
display_name: Python 3 (ipykernel)
language: python
Expand All @@ -20,7 +20,7 @@ jupyter:
name: python
nbconvert_exporter: python
pygments_lexer: ipython3
version: 3.9.7
version: 3.12.4
plotly:
description: How to make Log plots in Python with Plotly.
display_as: scientific
Expand Down Expand Up @@ -80,6 +80,38 @@ fig.update_xaxes(minor=dict(ticks="inside", ticklen=6, showgrid=True))
fig.show()
```

#### Controlling Minor Log Labels

*New in 6.3*

By default, minor log labels use small digits, as shown in the previous example. You can control how minor log labels are displayed using the `minorloglabels` attribute. Set to `"complete"` to show complete digits, or `None` for no labels.

```python
import plotly.express as px

df = px.data.gapminder().query("year == 2007")

fig = px.scatter(
df, x="gdpPercap",
y="lifeExp",
hover_name="country",
log_x=True,
range_x=[1,100000],
range_y=[0,100]
)

fig.update_xaxes(
minor=dict(
ticks="inside",
ticklen=6,
showgrid=True
),
minorloglabels="complete"
)

fig.show()
```

### Logarithmic Axes with Graph Objects

If Plotly Express does not provide a good starting point, it is also possible to use [the more generic `go.Figure` class from `plotly.graph_objects`](/python/graph-objects/).
Expand Down
53 changes: 51 additions & 2 deletions doc/python/pattern-hatching-texture.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ jupyter:
extension: .md
format_name: markdown
format_version: '1.3'
jupytext_version: 1.14.6
jupytext_version: 1.17.2
kernelspec:
display_name: Python 3 (ipykernel)
language: python
Expand All @@ -20,7 +20,7 @@ jupyter:
name: python
nbconvert_exporter: python
pygments_lexer: ipython3
version: 3.10.11
version: 3.9.0
plotly:
description: How to use patterns (also known as hatching or texture) with bar
charts.
Expand Down Expand Up @@ -150,6 +150,55 @@ fig.add_trace(go.Bar(x=["a","b"], y=[2,3], marker_pattern_shape="+"))
fig.show()
```

### Patterns Using SVG Paths

*New in 6.3*

You can make custom patterns for graphs by using an SVG path. Set `marker.pattern.path` to the SVG path to use:

```python
import plotly.graph_objects as go
import plotly.data

df = plotly.data.gapminder().query("year == 2007 and continent == 'Europe'").copy()
df['gdp'] = df['gdpPercap'] * df['pop']
df = df.sort_values('gdp', ascending=False).head(4)

fig = go.Figure(
data=[go.Bar(
x=df['country'],
y=df['gdp'],
marker=dict(
color=["lightsteelblue", "mistyrose", "palegreen", "thistle"],
pattern=dict(
path=[
"M0,0H4V4H0Z",
"M0,0H6V6Z",
"M0,0V4H4Z",
"M0,0C0,2,4,2,4,4C4,6,0,6,0,8H2C2,6,6,6,6,4C6,2,2,2,2,0Z"
],
fgcolor=["midnightblue", "crimson", "seagreen", "indigo"],
bgcolor=["mintcream", "lavenderblush", "azure", "honeydew"],
size=20,
solidity=0.7
)
),
name="GDP (2007)"
)],
layout=dict(
title="Top 4 European Countries by GDP (Gapminder 2007) with Custom SVG Path Patterns",
xaxis_title="Country",
yaxis_title="GDP (USD)",
yaxis_tickformat="$.2s",
width=800,
height=500,
bargap=0.3
)
)

fig.show()
```

#### Reference

See https://plotly.com/python/reference/bar/ for more information and chart attribute options!