Skip to content

Commit a81e418

Browse files
Adding an example showing how to put source lines or notes on the bottom of graphs
1 parent 193a4b7 commit a81e418

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

doc/python/text-and-annotations.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -782,6 +782,47 @@ fig.add_annotation(
782782

783783
fig.show()
784784
```
785+
### Specifying Source Lines or Figure Notes on the Bottom of a Figure
786+
787+
Container coordinates are an easy and robust way to put text -- such as a source line or figure note -- at the bottom of a figure. Only the title command supports container coordinates. If you have access to the HTML of the webpage in which you are embedding the graphic, either the source line, title, or both could go there instead.
788+
789+
```
790+
import plotly.express as px
791+
df_iris = px.data.iris()
792+
fig = px.scatter(df_iris, x="sepal_width", y="sepal_length", color="species",
793+
size='petal_length', hover_data=['petal_width'])
794+
795+
#Use the title for the source line
796+
fig.update_layout(
797+
title=dict(text="Note: a near zero container coordinate is the most robust way to position this on the bottom. Only the 'title' supports container coordinates.",
798+
yref="container",
799+
# A small positive y coordinate avoids cutting off the descending strokes of letters like y, p, and q.
800+
y=0.005,
801+
# Paper coordinates let us align this at either edge of the plot region
802+
# (use x=0, xanchor="left" for the lower left corner; that yields a slightly busier result since it's
803+
# not obvious whether we should align with the lower left corner of the plot area or the left edge of the axis labeling )
804+
xref="paper",
805+
xanchor="right",
806+
x=1,
807+
font=dict(size=12))
808+
)
809+
810+
#Repurpose an annotation to insert a title
811+
fig.add_annotation(
812+
xref="paper",
813+
yref="paper",
814+
xanchor="center",
815+
x=0.5,
816+
yanchor="bottom",
817+
y=1.025, # y = 1 is the top of the plot area
818+
text="We can use an annotation to provide the title",
819+
showarrow=False,
820+
font=dict(size=18)
821+
)
822+
823+
fig.show()
824+
```
825+
785826

786827
### Customize Displayed Text with a Text Template
787828

0 commit comments

Comments
 (0)