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

labelalias in Scatter.marker.colorbar can't handle negative values #4924

Open
stoney95 opened this issue Dec 3, 2024 · 2 comments
Open

labelalias in Scatter.marker.colorbar can't handle negative values #4924

stoney95 opened this issue Dec 3, 2024 · 2 comments

Comments

@stoney95
Copy link

stoney95 commented Dec 3, 2024

I am building a line-chart, which uses a colorgradient. The values of the line chart can be negative and positive. I want to add a colorbar to explain the meaning of the colors in the gradient. For this I use labelalias. The labels only appear for ticks >= 0.

Example

trace = go.Scatter(
  x = x,
  y = y,
  marker = dict(
    color=y,
    colorscale=[(0.0, "royalblue"), (0.5, "rgba(230, 230,230,0.5)"), (1.0, "orange") ]
    colorbar=dict(
                title="Score",
                tickmode="array",
                tickvals=[-10, 0, 10],
                labelalias={-10: "Negative", "0": "Neutral", 10: "Positive"},
                ticks="",
    )
  )
)

The output looks like this:
Image

Expected behavior

The label -10 is replaced by Negative

Context

  • python==3.11
  • plotly==5.24.1
@stoney95
Copy link
Author

stoney95 commented Dec 4, 2024

Using ticktext can be used as a workaround:

trace = go.Scatter(
  x = x,
  y = y,
  marker = dict(
    color=y,
    colorscale=[(0.0, "royalblue"), (0.5, "rgba(230, 230,230,0.5)"), (1.0, "orange") ]
    colorbar=dict(
                title="Score",
                tickmode="array",
                tickvals=[-10, 0, 10],
                ticktext=["Negative", "Neutral", "Positive"],
                # labelalias={-10: "Negative", "0": "Neutral", 10: "Positive"},
                ticks="",
    )
  )
)

@adityaraute
Copy link

Have you checked this @stoney95 ?

    def labelalias(self):
        """
        Replacement text for specific tick or hover labels. For example
        using {US: 'USA', CA: 'Canada'} changes US to USA and CA to
        Canada. The labels we would have shown must match the keys
        exactly, after adding any tickprefix or ticksuffix. For
        negative numbers the minus sign symbol used (U+2212) is wider
        than the regular ascii dash. That means you need to use1
        instead of -1. labelalias can be used with any axis type, and
        both keys (if needed) and values (if desired) can include html-
        like tags or MathJax.

Try replacing the minus sign in your code with this wider one and see if it works.

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

No branches or pull requests

2 participants