-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Adding an example of source lines or notes on the bottom of graphs #4873
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
Changes from 7 commits
a81e418
16ebf5e
f978ef6
335695c
a72b693
64e16d7
25d9a5b
8caa5d1
f34d4ed
0e5108b
cdc68a7
b3a3ead
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -782,6 +782,69 @@ fig.add_annotation( | |||||||||||||
|
||||||||||||||
fig.show() | ||||||||||||||
``` | ||||||||||||||
### Specifying Source Lines or Figure Notes on the Bottom of a Figure | ||||||||||||||
|
||||||||||||||
This example shows how to put a terse note about the data source or interpretation at the bottom of the figure. This example achieves the desired alignment in the bottom right corner using the title element and container coordinates and then uses an annotation to add a figure title. A near zero container coordinate is an easy and robust way to put text -- such as a source line or figure note -- at the bottom of a figure. It is easier to specify the bottom of the figure in container coordinates than in e.g. a paper coordinate since uncertainty about the size of legends and x-axis labels make the paper coordinate of the bottom of the figure uncertain. Making the y container coordinate very slightly positive avoids cutting off the descending strokes of letters like y, p, and q. Only the title command supports container coordinates, so this example repurposes the title element to insert the note and repurposes an annotation element for the title. The top of the figure is typically less cluttered and more predictable, so an annotation with its bottom at a paper y-coordinate slightly greater than 1 is a reasonable title location on many graphs. | ||||||||||||||
|
||||||||||||||
```import plotly.express as px | ||||||||||||||
df_iris = px.data.iris() | ||||||||||||||
fig = px.scatter(df_iris, x="sepal_width", y="sepal_length", color="species", | ||||||||||||||
size='petal_length', hover_data=['petal_width']) | ||||||||||||||
|
||||||||||||||
#Use the title for the source / note line | ||||||||||||||
rl-utility-man marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||
fig.update_layout( | ||||||||||||||
title=dict(text="Note: this is the Plotly title element.", | ||||||||||||||
# keeping this title string short avoids getting the text cut off in small windows | ||||||||||||||
# if you need longer text, consider 1) embedding your graphic on a web page and | ||||||||||||||
# putting the note in the HTML to use the browser's automated word wrap or | ||||||||||||||
# 2) using this approach and also specifying a graph width that shows the whole title. | ||||||||||||||
yref="container", | ||||||||||||||
y=0.005, | ||||||||||||||
# The "paper" x-coordinates lets us align this with either the right or left | ||||||||||||||
# edge of the plot region. | ||||||||||||||
# Aligning this flush with the right edge of the plot area is | ||||||||||||||
# predictable and easy to code. | ||||||||||||||
# Putting the title in the lower left corner, aligned with the left edge of the axis labeling would | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Would this also be true? I think it might make it more precise. What do you think? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is fine! I batch accepted, so your edit is now shown in red; and the original is now the option. |
||||||||||||||
# require graph specific coordinate adjustments. | ||||||||||||||
xref="paper", | ||||||||||||||
xanchor="right", | ||||||||||||||
x=1, | ||||||||||||||
font=dict(size=12)), | ||||||||||||||
|
||||||||||||||
rl-utility-man marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||
plot_bgcolor="white", | ||||||||||||||
|
||||||||||||||
# We move the legend out of the right margin so the right-aligned note is | ||||||||||||||
# flush with the right most element of the graph. | ||||||||||||||
# Here we put the legend in a corner of the graph region | ||||||||||||||
# because it has consistent coordinates at all screen resolutions. | ||||||||||||||
legend=dict( | ||||||||||||||
yanchor="top", | ||||||||||||||
y=1, | ||||||||||||||
xanchor="right", | ||||||||||||||
x=1, | ||||||||||||||
borderwidth=1) | ||||||||||||||
) | ||||||||||||||
|
||||||||||||||
# Insert a title by repurposing an annotation | ||||||||||||||
fig.add_annotation( | ||||||||||||||
yref="paper", | ||||||||||||||
yanchor="bottom", | ||||||||||||||
y=1.025, # y = 1 is the top of the plot area; the top is typically uncluttered, so placing | ||||||||||||||
# the bottom of the title slightly above the graph region works on a wide variety of graphs | ||||||||||||||
text="This title is a Plotly annotation", | ||||||||||||||
|
||||||||||||||
# Center the title horizontally over the plot area | ||||||||||||||
xref="paper", | ||||||||||||||
xanchor="center", | ||||||||||||||
x=0.5, | ||||||||||||||
|
||||||||||||||
showarrow=False, | ||||||||||||||
font=dict(size=18) | ||||||||||||||
) | ||||||||||||||
|
||||||||||||||
fig.show() | ||||||||||||||
``` | ||||||||||||||
|
||||||||||||||
|
||||||||||||||
### Customize Displayed Text with a Text Template | ||||||||||||||
|
||||||||||||||
|
Uh oh!
There was an error while loading. Please reload this page.