Skip to content

Sorting issues with multicategories #1491

Closed
@ThibTrip

Description

@ThibTrip

Hello,

as you can see here my chart is not correctly sorted by the x-axis and the line goes back which is really not an expected behavior.

troubleshoot_axis_sort_multicat

I found out that a workaround is to add "missing" x labels (as in missing months in this case, see below) with None in y values but in my case it is very impractical.

troubleshoot_axis_sort_multicat_workaround

First of all it will make the graph overly large (spans over 5 years in some cases but I do not have data for all months, far from that). Also I'm using this graph with dash and the data gets transformed a lot which makes adding missing labels quite tedious.

EDIT:

Another option you might think is to just use the month names in this case. Turns out that does not work either:

multicategories_sorting_issues_update

Thanks in advance for checking out the issue,

Thibault

Reproducible example with version 3.7.1 in offline mode

import plotly.graph_objs as go



x_axis = [[2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2018, 2018,2018, 2018], 
             [4, 5, 6, 8, 9, 10, 11, 12, 1, 2, 3, 4]]
y1 = [ 57246.,   1587.,  63189.,  30680.,  26071., 117519.,  27519.,  42643., 8559.,  22355.,  59363.,  62955.]
y2 = [0.22991999, 0.27284184, 0.18438336, 0.24866362, 0.18280848, 0.19382398, 0.17268069, 0.18802617, 0.17560463, 0.13415343, 0.14773512, 0.31319196]


# all arrays must have the same length
assert len(x_axis[0]) == len(x_axis[1]) == len(y1) == len(y2)


test_figure = go.Figure({
    'data': [{'name': 'Fruits',
              'type': 'bar',
              'x': x_axis,
              'y': y1},
             
             {'name': 'Price',
              'type': 'scatter',
              'x': x_axis,
              'y': y2,
              'yaxis': 'y2'}],
    
    
    'layout': {'height': 600,
               'showlegend': True,
               'yaxis': {'showgrid': True, 'title': {'text': 'Fruits volume'}},
               'yaxis2': {'anchor': 'x',
                          'overlaying': 'y',
                          'showgrid': False,
                          'side': 'right',
                          'title': {'text': 'Price per unit'}}}
})


from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
iplot(test_figure)

Related issue on plotly.js

The issue I'm describing here should have been fixed in version 1.43.1 of plotly.js.
See https://github.com/plotly/plotly.js/blob/master/CHANGELOG.md#1431----2018-12-21
(issue 3362)

Even though plotly.py uses a superior version as you can see there is still an issue.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugsomething broken

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions