plotly / plotly.py

The interactive graphing library for Python :sparkles: This project now includes Plotly Express!
https://plotly.com/python/
MIT License
16k stars 2.53k forks source link

tickvals and ticktext don't work with subcategory axes #2395

Open krisowski opened 4 years ago

krisowski commented 4 years ago

If a plot has multi-category axes there is no way to hide a subcategory labels only and the plot doesn't render at all if tickvals and ticktext are used.

Look at the code without multi-categories:

x_names = ['very.long.name.1', 'very.long.name.2', 'very.long.name.3']
fig = go.Figure(go.Bar(
    x=x_names,
    y=[10, 20, 30]))
fig.update_layout(
    xaxis=dict(
        tickvals=x_names,
        ticktext=['', '', ''],
    ))

This works fine and all x-axes labels are hidden. I could achieve the same thing with this: fig.update_layout(xaxis=dict(showticklabels=False))

However, if this is a multi-category plot it doesn't work at all:

x_names = [['A', 'A', 'B'], ['very.long.name.1', 'very.long.name.2', 'very.long.name.3']]
fig = go.Figure(go.Bar(
    x=x_names,
    y=[10, 20, 30]))
fig.update_layout(
    xaxis=dict(
        tickvals=x_names,
        ticktext=[['A', 'A', 'B'], ['', '', '']],
    ))

Setting showticklabels=False hides everything and I didn't find a way to hide just a single category labels.

What is really surprising this doesn't work either even if this doesn't change anything:

x_names = [['A', 'A', 'B'], ['very.long.name.1', 'very.long.name.2', 'very.long.name.3']]
fig = go.Figure(go.Bar(
    x=x_names,
    y=[10, 20, 30]))
fig.update_layout(
    xaxis=dict(
        tickvals=x_names,
        ticktext=x_names,
    ))

I believe it should work like this:

samparks commented 4 years ago

Seconded. To add a bit of variation to the example above, I would expect to select tickvals using the same nested list structure that's initially passed to the x argument of go.Bar(). For example, creating a figure with a multicategory xaxis:

fig = go.FigureWidget()
fig.add_trace(
    go.Scatter(
        x = [['a', 'a', 'a', 'a', 'b', 'b', 'b','b'], [1,2,3,4,5,6,7,8]],
        y = [50, 55, 56, 57, 35, 12, 45, 40]
    )
)

Then, I'd like to use tickvals in this way to select the specific x values:

fig.update_layout(
     xaxis = dict(
         tickvals = [['a', 'a', 'b', 'b'], [1, 3, 5, 7]],
         ticktext = ['A', 'B', 'C', 'D']
     )
)
zachsiegel-capsida commented 1 year ago

I also have this issue. I found that updating tickvals and ticktext with a subcategory axis always resulted in no tick values or labels in the resulting figure.

insitubytes commented 1 year ago

A similar (or possibly the same) issue exists when trying to set tickvals and ticktext while setting type="category". This results in an empty Figure. Setting only one of tickvals or ticktext results in a figure but without any consequence. Imho, it should be possible to set the ticktext of a categorical axis.