Skip to content
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

add_vline is broken for categorical scatter plots with categories that convert to numbers #3013

Open
mpaulonis opened this issue Jan 8, 2021 · 2 comments
Assignees
Labels

Comments

@mpaulonis
Copy link

Thanks for your interest in plotly.py!!

Before opening an issue, please search for existing and closed issues. If your problem or idea is not addressed yet, please open a new issue.

Bug reports must be accompanied with a reproducible example. Please use the latest version of plotly.py in your report unless not applicable and specify if you're plotting online or offline.

Note that GitHub issues are reserved for bug reports and feature requests only. Implementation questions should be asked on community.plot.ly (tagged python) or on Stack Overflow (tagged plotly).

Comments on GitHub issues or pull requests should add content to the discussions. Approbation comments such as +1 or I would like this feature to be implemented as well will be deleted by the maintainers. Please use GitHub reactions instead.

@mpaulonis
Copy link
Author

add_vline works fine for categorical scatter plots where the category labels are strings that cannot convert to a number. The line is placed as expected at the requested position on the x axis corresponding to the label. However, if the x axis labels can convert to numbers, add_vline will convert the requested string label to a number and plot the line on the (n+1)th major tick of the x axis (Python zero-based) rather than at the position corresponding to the label.

Here is a simple example:

fig = go.Figure()
fig.add_trace(go.Scatter(x=['2', '4', '6', '8'],
                         y=[1, 2, 3, 2]))
fig.update_xaxes(type='category')
fig.add_vline(x='6', line_dash='dot')
fig.add_vline(x='2', line_dash='dash')
fig.add_hline(y=2, line_dash='dot')
fig.update_layout(width=400, height=400)

The result of this is:
1-8-2021 6-45-32 PM
The dotted line should be at 6, but instead is plotted way out to the right where the 7th major tick would be. The dashed line should be plotted at 2, but is plotted on the 3rd major tick, which is where the 6 is.

Note that the x data are provided as strings and the x axis is specifically set to category rather than allowing an auto-select.
Somewhat remarkably, the problem persists even if the x values are padded with spaces before and after, in an attempt to really denote them as strings.

fig = go.Figure()
fig.add_trace(go.Scatter(x=[' 2 ', ' 4 ', ' 6 ', ' 8 '],
                         y=[1, 2, 3, 2]))
fig.update_xaxes(type='category')
fig.add_vline(x=' 6 ', line_dash='dot')
fig.add_vline(x=' 2 ', line_dash='dash')
fig.add_hline(y=2, line_dash='dot')
fig.update_layout(width=400, height=400)

The expected behavior is only possible if the x values cannot be converted to numbers, even with trimming white space.

fig = go.Figure()
fig.add_trace(go.Scatter(x=['2*', '4*', '6*', '8*'],
                         y=[1, 2, 3, 2]))
fig.update_xaxes(type='category')
fig.add_vline(x='6*', line_dash='dot')
fig.add_vline(x='2*', line_dash='dash')
fig.add_hline(y=2, line_dash='dot')
fig.update_layout(width=400, height=400)

1-8-2021 6-57-02 PM

@nicholas-esterer
Copy link
Contributor

nicholas-esterer commented Aug 2, 2021

I believe this is due to a problem with plotly.js, related to plotly/plotly.js#5767

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants