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

tickvals and ticktext don't work with subcategory axes

Open krisowski opened this issue 5 years ago • 5 comments

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:

  • tickvals and ticktext work fine with a multi-category plots
  • showticklabels should accept a list of booleans, .e.g. [True, False] to be able to hide a given subcategory if this is a multi-category plot

krisowski avatar Apr 19 '20 20:04 krisowski

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']
     )
)

samparks avatar May 20 '20 18:05 samparks

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.

zachsiegel-capsida avatar Oct 05 '22 21:10 zachsiegel-capsida

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.

insitubytes avatar Jan 18 '23 13:01 insitubytes

I have been struggling with the same issue while plotting grouped bar chart over longer horizon. I am using plotly version 5.24.1. Do we know how much progress has been made on this. Happy to contribute!

Joeljoy-65 avatar Jun 04 '25 13:06 Joeljoy-65

@Joeljoy-65 sorry it's been a struggle - unfortunately we haven't had any resources to put against this one, but if someone from the community would like to put together a PR we'd be happy to prioritize review. cheers - @gvwilson

gvwilson avatar Jun 10 '25 13:06 gvwilson