question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

annotation coordinates do not respect log scaling

See original GitHub issue

When 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()

image

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
nicolaskruchtencommented, Jun 21, 2020

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.

0reactions
maegulcommented, Dec 2, 2022

This seems to also wreak some havoc with with figure.add_hline() function and the use of the annotation_position="top left" argument (and similar), as for plotly version 5.4.0 it doesn’t seem to take account of the log axis type.

I found myself needing to make the following adjustment:

(
   fig
    .add_hline(
        y=line_value, 
        annotation_text=f'This is an annotation',
        annotation_position='top left',
        # This is the adjustment --V
        annotation_y=np.log10(line_value)
    )
)
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found