plotly / plotly/plotly.py

annotation coordinates do not respect log scaling

Open
#2,580 11 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug P3
Dominant language
Python
Stars
18.8k
Forks
2.8k
Avg merge
16h 26m
Merged PRs (30d)
21

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No source file or test is named. Start by reproducing the minimal example with log-scaled x and y axes, then trace how annotations and axis coordinates are handled; done means annotations accept actual data coordinates on log axes while date and other axis types continue to work without changing the underlying data.

Written by the indexing model from the issue text.

Assessment

Tech stack
plotly, python
Domain
data-visualization
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.