Skip to content

Single bar datum does not display with barmode='overlay' #2191

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
radumas opened this issue Dec 3, 2017 · 6 comments
Closed

Single bar datum does not display with barmode='overlay' #2191

radumas opened this issue Dec 3, 2017 · 6 comments

Comments

@radumas
Copy link

radumas commented Dec 3, 2017

This was originally a plotly.py issue I was told to reopen here. Forgive the lack of js reproducible example

I'm creating a bar plot with multiple categories and I wanted to colour each category differently, even though the categories don't overlap. Multiple rows display fine using barmode='overlay', but if there's only one row in a category, it doesn't display:

image

If I don't split the data, it displays fine (different street, same issue):

image

If I change barmode='relative' then it displays fine:
image

@etpinard
Copy link
Contributor

etpinard commented Dec 4, 2017

Can you at least share a reproducible python example?

Debugging from screenshots is very hard. Thanks.

@alexcjohnson
Copy link
Collaborator

This is probably covered by #1308 and #1346 - the issue is that with no other information (such as a second bar) to provide a scale we can't figure out a reasonable width automatically. So we default to 1, which for dates means 1 millisecond (so your single bar probably is being displayed, just invisibly thin).

I suppose 1 day might be more useful as a default (it would work for you anyway) but I don't think it's appropriate to use the axis range as part of the default logic, that would make the meaning of the trace dependent on how you happen to be viewing it (zoom). Normally in overlay mode we don't allow subsequent bar traces to influence each other, because comparing traces with differing bar widths is an important use case for overlay mode. I suppose we could make an exception for single-bar traces, but the cleanest solution is to use the width attribute. Here you want a width of 1 day, which is 1000*60*60*24 (in milliseconds).

@radumas
Copy link
Author

radumas commented Dec 4, 2017

Here's a Dash example:

from datetime import date 

import dash
import dash_core_components as dcc
import dash_html_components as html
import pandas as pd
import plotly.graph_objs as go

data = [(date(2017, 11, 4), 9, 1),
        (date(2017, 11, 5), 8, 1),
        (date(2017, 11, 11), 12, 1),
        (date(2017, 11, 12), 9, 2)
]

data = pd.DataFrame(data, columns=['Date', 'Metric', 'Category'])

def generate_graph(data, barmode='overlay'):
    data1 = data[data['Category']==1]
    data2 = data[data['Category']==2]
    graph_data1 = go.Bar(x=data1['Date'],
                         y=data1['Metric'],
                         hoverinfo='x+y'
                         )
    graph_data2 = go.Bar(x=data2['Date'],
                         y=data2['Metric'],
                         hoverinfo='x+y'
                         )    
    layout = go.Layout(barmode=barmode,
                       title=barmode)
    return go.Figure(data=[graph_data1, graph_data2], layout=layout)

        
        
        


app = dash.Dash()

app.layout = html.Div([dcc.Graph(id='overlay', figure=generate_graph(data, barmode='overlay')),
                       dcc.Graph(id='relative', figure=generate_graph(data, barmode='relative'))
])

if __name__ == '__main__':
    app.run_server(debug=True)

image

@alexcjohnson
Copy link
Collaborator

@radumas from the fact that half a day showed up in autorange for the overlay graph it definitely looks like you've got a 1-ms-wide bar there. Did you try my suggestion of adding an explicit width to the orange trace? I guess if you go that route you need to explicitly account for the gap, which by default is 20%, so it would be width=1000*60*60*24*0.8

@radumas
Copy link
Author

radumas commented Dec 4, 2017

Ah, no I just used the relative mode instead, since it suits what we were trying to accomplish

@alexcjohnson
Copy link
Collaborator

OK. Closing as this does seem covered by the other two issues I mentioned.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants