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.

Bar chart with `facet_row` only rendering data for one facet

See original GitHub issue

I have a bar chart with a facet_row defined as follows, with example data.

Example code and data for `facet_row` bug
tmp_data = [
    {
        "date": "2022-04-17",
        "role_name": "Nurse",
        "work_type": "Cleaning",
        "daily_total_minutes": 45,
        "percent_of_daily_role_total_minutes": 0.41,  
    },
    {
        "date": "2022-04-17",
        "role_name": "Nurse",
        "work_type": "Food preparation",
        "percent_of_daily_role_total_minutes": 0.59,  
    },
    {
        "date": "2022-04-17",
        "role_name": "Volunteer",
        "work_type": "Cleaning",
        "percent_of_daily_role_total_minutes": 0.26,  
    },
    {
        "date": "2022-04-17",
        "role_name": "Volunteer",
        "work_type": "Food preparation",
        "percent_of_daily_role_total_minutes": 0.74,  
    },
    {
        "date": "2022-05-06",
        "role_name": "Volunteer",
        "work_type": "Food preparation",
        "percent_of_daily_role_total_minutes": 1.0,  
    },
    {
        "date": "2022-05-07",
        "role_name": "Volunteer",
        "work_type": "Cleaning",
        "percent_of_daily_role_total_minutes": 0.49,  
    },
    {
        "date": "2022-05-07",
        "role_name": "Volunteer",
        "work_type": "Food preparation",
        "percent_of_daily_role_total_minutes": 0.51,  
    },
]

daily_work_by_caregiver_role_and_type_with_percent_chart = px.bar(
    tmp_data,
    x="date",
    y="percent_of_daily_role_total_minutes",
    facet_row="role_name",
    color="work_type",
    title=_("Daily work percent by caregiver role and work type"),
    labels={
        "role_name": _("Caregiver role"),
        "percent_of_daily_role_total_minutes": _("Work percent"),
        "work_type": _("Type of work"),
    },
    text_auto=True,
)
daily_work_by_caregiver_role_and_type_with_percent_chart.layout.yaxis.tickformat = ",.0%"

# render the chart
daily_work_by_caregiver_role_and_type_with_percent_chart.to_html()

When rendering the chart, the x axis correctly determines the data are dates and the y-axis on the bottom facet is correctly formatted as a percentage with the data rendering. However, the top facet doesn’t render any data.

image

I have tried both with and without specifying the yaxis.tickformat and get a similar result, where the first facet series doesn’t render any data.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:13 (9 by maintainers)

github_iconTop GitHub Comments

2reactions
nicolaskruchtencommented, May 27, 2022

So what’s happening here is that the upper facet has only one day’s worth of data in it, and the automatic calculation of the bar width results in a single extremely thin bar being shown around midnight of April 17 (the bar is less than a millisecond wide). This is pretty counter-intuitive but that’s what’s happening. You can force the bars to be one day wide by calling .update_traces(width=24*60*60*1000) on the figure.

@alexcjohnson I can see this being more or less reasonable behaviour for a single subplot, but for matched axes, I could also see the bar width being computed while taking into account all bars on the whole set of axes, no?

1reaction
nicolaskruchtencommented, May 31, 2022

Great, happy I was able to help, sorry for the friction you’ve been encountering 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Compound Charts: Layer, HConcat, VConcat, Repeat, Facet
Like repeated charts, Faceted charts provide a more convenient API for creating multiple views of a dataset for a specific type of chart:...
Read more >
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 >
fct_reorder() only working properly for one facet - Stack Overflow
I am trying to create a faceted bar graph ...
Read more >
The seaborn.objects interface — seaborn 0.12.1 documentation
Specifying a plot and mapping data#. The objects interface should be imported with the following convention: import seaborn.objects as ...
Read more >
Introducing Plotly Express - Medium
If you want a basic scatter plot, it's just px.scatter(data, x="column_name", y="column_name") . Here's an example with the Gapminder dataset – which comes ......
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