Skip to content

Commit 79c49c1

Browse files
committed
standoff example
1 parent e909b32 commit 79c49c1

File tree

1 file changed

+75
-1
lines changed

1 file changed

+75
-1
lines changed

doc/python/marker-style.md

+75-1
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,6 @@ import pandas as pd
413413
import plotly.express as px
414414
import plotly.graph_objects as go
415415

416-
417416
df = px.data.gapminder()
418417

419418
fig = go.Figure()
@@ -437,6 +436,81 @@ fig.show()
437436

438437
```
439438

439+
### Using Standoff to Position a Marker
440+
441+
*New in 5.11*
442+
443+
When you have multiple markers at one location, you can use `standoff` on a marker to move it away from the other marker in the direction of the `angle`.
444+
In this example, we set `standoff=8` on the `arrow` marker, which is half the size of the other `circle` marker, meaning it points exactly at the `circle`.
445+
446+
```python
447+
import pandas as pd
448+
import plotly.graph_objects as go
449+
from plotly import data
450+
451+
df = data.gapminder()
452+
df = df.loc[(df.continent == "Americas") & (df.year.isin([1987, 2007]))]
453+
454+
countries = (
455+
df.loc[(df.continent == "Americas") & (df.year.isin([2007]))]
456+
.sort_values(by=["pop"], ascending=True)["country"]
457+
.unique()
458+
)[5:-10]
459+
460+
data = {"x": [], "y": [], "colors": [], "years": []}
461+
462+
for country in countries:
463+
data["x"].extend(
464+
[
465+
df.loc[(df.year == 1987) & (df.country == country)]["pop"].values[0],
466+
df.loc[(df.year == 2007) & (df.country == country)]["pop"].values[0],
467+
None,
468+
]
469+
)
470+
data["y"].extend([country, country, None]),
471+
data["colors"].extend(["cyan", "darkblue", "white"]),
472+
data["years"].extend(["1987", "2007", None])
473+
474+
fig = go.Figure(
475+
data=[
476+
go.Scatter(
477+
x=data["x"],
478+
y=data["y"],
479+
mode="markers+lines",
480+
marker=dict(
481+
symbol="arrow",
482+
color="royalblue",
483+
size=16,
484+
angleref="previous",
485+
standoff=8,
486+
),
487+
),
488+
go.Scatter(
489+
x=data["x"],
490+
y=data["y"],
491+
text=data["years"],
492+
mode="markers",
493+
marker=dict(
494+
color=data["colors"],
495+
size=16,
496+
),
497+
hovertemplate="""Country: %{y} <br> Population: %{x} <br> Year: %{text} <br><extra></extra>""",
498+
),
499+
]
500+
)
501+
502+
fig.update_layout(
503+
title="Population changes 1987 to 2007",
504+
width=1000,
505+
height=1000,
506+
showlegend=False,
507+
)
508+
509+
510+
fig.show()
511+
512+
```
513+
440514
### Reference
441515

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

0 commit comments

Comments
 (0)