annotation coordinates do not respect log scaling
See original GitHub issueWhen adding annotations to a plot that uses log scaling, the annotation coordinates need to be rescaled to represent the log10 of the actual coordinates. While this is documented behavior, it is rather counterintuitive and should be fixed. Seemingly no other axis-types (date, for instance) needs special treatment but works without modifying the underlying data.
Minimum example demonstrating the point, i.e. label_y is 4 but plotted at 10^4:
df = pd.DataFrame(data={'x':[1,2,3,4],
'y':[1,4,9,16],
})
fig = px.line(df,
x='x',
y='y',
log_y=True,
log_x=True,
)
annotations = []
fig.for_each_trace(
lambda trace: annotations.append(dict(x=np.log10(np.sqrt(trace.x[0]*trace.x[-1])),
y=np.sqrt(trace.y[0]*trace.y[-1]),
text=f'({np.sqrt(trace.x[0]*trace.x[-1])},{np.sqrt(trace.y[0]*trace.y[-1])})',))
)
fig.update_layout(annotations=annotations,
width=300,
height=300,
)
fig.show()
Issue Analytics
- State:
- Created 3 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
Specify annotation position with respect to x- and y-axes values?
I have already considered to simply "normalize" those positions myself (e.g. 90 s/180 s = 0.5 --> x-position in normalized coordinates). This does,...
Read more >Scaling (and annotating) images in a rotated matrix of tikz ...
The \node s in the \matrix do scale according to page height (as they are rotated, with their width) - but the rotated...
Read more >Annotation Label Offset Explanation - NI Community
The position elements locate the annotation point in unscaled units of the axes. The Label offset is in the same units, and is...
Read more >Data Labeling: The Authoritative Guide - Scale AI
Data labeling is one of the most critical activities in the machine learning lifecycle, though it is often overlooked in its importance.
Read more >Annotation box does not appear in matplotlib - Stack Overflow
The annotation is appearing far above your plot because you have given a 'y' coordinate of 60, whereas your ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I agree that this is a pretty bad aspect of our API. Specifying
layout.(x|y)axis.range
on log axes works the same way.Unfortunately, we will not be able to change this without a major version bump to both Plotly.py and Plotly.js, as it would likely break many people’s code in the wild. The best we can do in the short term is to add a flag to the layout (default
False
, so folks would have to opt in) that would enable us to accept coordinates directly.This seems to also wreak some havoc with with
figure.add_hline()
function and the use of theannotation_position="top left"
argument (and similar), as forplotly
version5.4.0
it doesn’t seem to take account of thelog
axis type.I found myself needing to make the following adjustment: