I’m trying to modify (hide) some values in a plot with tickvals and ticktext on my x axis. The problem is that my x axis is multicategory (x=[x1,x2])
Let’s say my data is:
# Making a dummy dataframe
data = pd.DataFrame(
{
"company": ["company a", "company b", "company c"],
"cost": [10, 20, 33],
"year": [2020, 2021, 2020],
}
)
# Adding dummy barname column
data["barname"] = [" ", "company b", " "]
So with this, I want to only show company b, and hide company a and c. (The reason I use " " instead of “” is that the latter makes the graph placement weird for some reason)
A working example without multicategory:
fig = go.Figure([go.Bar(x=data.company, y=data.cost)])
fig.update_layout(
xaxis=dict(
tickmode="array",
tickvals=[*range(0, len(data.company), 1)],
ticktext=data.barname,
)
)
fig.show()
Trying to hide the company name in a multicategory x-axis:
fig = go.Figure([go.Bar(x=[data.company, data.year], y=data.cost)])
fig.update_layout(
xaxis=dict(
tickmode="array",
tickvals=[*range(0, len(data.company), 1)],
ticktext=[data.barname, data.year],
)
)
fig.show()
That doesn’t work.
I found this on Github, where it looks like what I’m trying to do is a dead end:
opened 08:04PM - 19 Apr 20 UTC
If a plot has multi-category axes there is no way to hide a subcategory labels o… nly 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
But I also found this, where it looks like there could be a way around it, I just can’t wrap my head around it:
Hi all,
I’m trying to develop a dynamic bar chart for time series data. The chart has a stacked bar for every day–see picture.
I would like the information the x-axis to change depending on the horizontal zoom level (defined as the number of bars in the x-axis range). At the highest zoom level (say, less than 10 bars) the graph should look like the picture. At lower zoom levels (>10 bars) there would be no space for the day-labels so I would like to show week numbers (i.e. one number for every…
Is this possible to do?