plotly.py icon indicating copy to clipboard operation
plotly.py copied to clipboard

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

Open stoney95 opened this issue 1 year ago • 3 comments

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 avatar Dec 03 '24 18:12 stoney95

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="",
    )
  )
)

stoney95 avatar Dec 04 '24 07:12 stoney95

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 use −1
        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.

adityaraute avatar Dec 04 '24 21:12 adityaraute

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 use −1
    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.

This worked for me, using strings as keys however, otherwise it wouldn't run due to an invalud character in the code. Here is my working code (notice the wider - sign in the key string): "labelalias": {"0": "0 (usual treshold of hearing)", "30": "30 (clearly audible)", "−20": "-20 (definitely not audible)"}

NielsMortensen avatar Apr 11 '25 15:04 NielsMortensen