Issue #3755 Bug Fix for: Shapes and Annotations With yref Parameter Not Drawing on Correct Axis#4177
Conversation
…bug on the previous commit
alexcjohnson
left a comment
There was a problem hiding this comment.
@zfoltz this looks great - thanks!! Very nice tests 🎉
💃 Will merge once the tests pass.
|
Hmm the doc build test looks like it might point to a bug here? import plotly.express as px
df = px.data.iris()
fig = px.scatter(df, x="sepal_length", y="sepal_width", facet_col="species")
# sources of images
sources = [
"https://upload.wikimedia.org/wikipedia/commons/thumb/f/fe/Iris_setosa_var._setosa_%282595031014%29.jpg/360px-Iris_setosa_var._setosa_%282595031014%29.jpg",
"https://upload.wikimedia.org/wikipedia/commons/thumb/3/38/Iris_versicolor_quebec_1.jpg/320px-Iris_versicolor_quebec_1.jpg",
"https://upload.wikimedia.org/wikipedia/commons/thumb/f/f8/Iris_virginica_2.jpg/480px-Iris_virginica_2.jpg",
]
# add images
for col, src in enumerate(sources):
fig.add_layout_image(
row=1,
col=col + 1,
source=src,
xref="x domain",
yref="y domain",
x=1,
y=1,
xanchor="right",
yanchor="top",
sizex=0.2,
sizey=0.2,
)
fig.show()Gives the error: |
|
Also we should add a changelog entry for this fix 🎉 |
|
Yes, you are right! From the looks of it, I think there is a pretty straightforward fix, I'll try to verify and update later today. Thanks for taking a look at this! |
…ithub.com/zfoltz/plotly.py into axis_spanning_layout_object_xref_yref_bug
…ithub.com/zfoltz/plotly.py into axis_spanning_layout_object_xref_yref_bug
|
The bug showing in the build-doc is fixed and I updated changelog! 👍
|
|
I tried the bugfix, and while the hline is now working if set to yref=y3 for example, an annotation_text is not displayed at all! |
|
Hey @endursa, can you post what you are working with? I tried to reproduce the error you mentioned but the following test I ran is working correctly: This results in the following plot: So maybe I'm misunderstanding what you mean by an |
|
yes of course, my problem looks like this: If one changes the yaxis range to the annotation and another hline appear sideinfo: |
…layed on the primary y instead of the yref supplied with the axis_spanning_shape
…ithub.com/zfoltz/plotly.py into axis_spanning_layout_object_xref_yref_bug
|
Thanks for pointing this out and the code @endursa! Looks like I've got it figured out. It appears that when using the |
|
Thank you very much! |
|
Hey @alexcjohnson anything else needed before merge? |



This PR is related to issue #3755 which, to put more generally, is that shapes and annotations when supplied with a
yrefparameter are automatically drawn on the primary y-axis despite the suppliedyrefvalue. The only way that a shape or annotation can currently be drawn on an axis that differs from the primary axes is when the figure object is created withsubplots.make_subplots()with thespecslist parameter set to'secondary_y':Truefor that specific subplot. In the case of a non-subplotted figure object, it is impossible as the following line:plotly.py/packages/python/plotly/plotly/basedatatypes.py
Line 4051 in 956ab2c
yref = 'y'when there is not more than one subplot. It is also possible to make a figure object with subplot grid of (1,1) when using plotly express. Using that method there is no way to allow thesecondary_y:Trueto the figure object therefore specifyingyrefwill result in the shape or annotation plotting on the primary y axis regarless of the specifiedyrefand if thesecondary_y=Trueparameter is supplied in the method to add the shape or annotation, that will result in the following error:plotly.py/packages/python/plotly/plotly/basedatatypes.py
Lines 1562 to 1567 in 956ab2c
This error is also missing it's
string.format()method and parameters which are fixed in this as well.For subplots that use the
subplots.make_subplots()with thespecslist parameter set to'secondary_y':Truethis change does not cause the loss of any functionality it just extends the ability to draw shapes and annotations on additional y-axes when generating figure objects without thesubplots.make_subplots()method. Specifyingyrefand if thesecondary_yparameters will not result in an error when used with plotly express (a figure object with a (1,1) subplot grid) or a non subplotted figure object (such as when usinggo.Figure()), it will just proceed with applying it to the specifiedyref.Code PR
plotly.graph_objects, my modifications concern thecodegenfiles and not generated files.modified existing tests.
new tutorial notebook (please see the doc checklist as well).