question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

fig.update_layout() does not iterate on all facets

See original GitHub issue

I am trying to make a faceted plot where I need to treat the x axis as categorical, even though it is an integer. On a single chart (that is not faceted), the following works:

fig.update_layout(xaxis_type='category')

However, on a faceted chart, only one of the facets is changed. I would think that updating the figure layout would iterate on all facets.

Is there an an alternate way to do this using one of the fig.for_each_*() methods? (as in for_each_xaxis() . I played around with it but could not seem to find a way to do it.

Using plotly 4.6.0 on windows

Thanks

import pandas as pd
import plotly.express as px

df = pd.DataFrame({
    'x': [1, 2, 3, 5, 6],
    'y1': [1, 2, 3, 4, 5],
    'y2': [10, 11, 12, 13, 15]
}).melt('x')

bars = px.bar(
    data_frame=df,
    x = 'x',
    y = 'value',
    facet_col = 'variable'
)

bars.update_layout(xaxis_type='category')
bars.show()

2020-06-05_19-45-25

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

8reactions
nicolaskruchtencommented, Jun 6, 2020

This is as intended: layout.xaxis refers to one x axis, and layout.xaxis2 refers to the other. If you want to update all x axes, you can use .update_xaxes(type="category").

Furthermore, you can use .update_xaxes(row=1, col=2, type="category") to target your update to a specific facet if you so choose, so you don’t really need to keep track of xaxis vs xaxis1 etc yourself if you choose not to 😃

0reactions
rsgoodwincommented, Dec 14, 2022

Yup fig.update_xaxes(type='category'), as documented here.

Thanks for the posterity post!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Facet and trellis plots in Python - Plotly
Facet plots, also known as trellis plots or small multiples, are figures made up of multiple subplots which have the same set of...
Read more >
Editing facet headings on a Plotly chart - Stack Overflow
So my graphs are printing out the correct data but for one of the graphs the axis does not match the hover text....
Read more >
Python Plotly: How to set the range of the y axis?
Figure() function takes in data as input where we set the mode as ... Example 2: Using update_layout() function to set the y-axis...
Read more >
Taking Another Look at Plotly - Practical Business Python
You did not have to post your visualizations to the Plotly servers but ... When I create visualizations, I iterate through many different ......
Read more >
Visualization with Plotly.Express: Comprehensive guide
All plotly express charts can be done by one line of code. Image by author ... fig.update_layout(layout_parameters or add annotations)
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found