Skip to content

parallel_coordinates is incorrect when using color and non-sorted index #3576

Closed

Description

@shane-kercheval

This is somewhat of a weird bug, but I don't think it would be that uncommon of a use-case.

First let's see it working as expected.

If you run:

df = pd.DataFrame({
        'A': [1, 2, 3, 4],
        'B': [1, 2, 3, 4],
        'C': [1, 2, 3, 4],
    },
    index=[0, 1, 2, 3]
    #index=[3, 2, 1, 0]
)
df

px.parallel_coordinates(df)

you'll see

Screen Shot 2022-01-30 at 11 43 31 AM

Then run:

px.parallel_coordinates(df, color='B')

and you'll see

Screen Shot 2022-01-30 at 11 43 58 AM

Everything looks good so far.

But then change the index of the DataFrame from index=[0, 1, 2, 3] to index=[3, 2, 1, 0]

df = pd.DataFrame({
        'A': [1, 2, 3, 4],
        'B': [1, 2, 3, 4],
        'C': [1, 2, 3, 4],
    },
    #index=[0, 1, 2, 3]
    index=[3, 2, 1, 0]
)
df

px.parallel_coordinates(df)

This will return exactly the same graph as before. But when you use the color param, you get this

px.parallel_coordinates(df, color='B')

Screen Shot 2022-01-30 at 11 45 58 AM

Which is incorrect. For example, instead of 1-1-1 (which is still a single row, despite the change in index), the line goes from 1-4-1.

Activity

  1. shane-kercheval commented on Jan 30, 2022

    @shane-kercheval
    Author

    So it seems like the code corresponding to the color variable is doing a sort or something that isn't retaining the correct order. A and C still have the correct associations, but B is reversed.

  2. Adrien-LUDWIG commented on Apr 11, 2023

    @Adrien-LUDWIG

    I have come into a similar case. I do not think it needs a new issue but this one may need a slight title update.

    In my case, the index is sorted. When I plot the original dataframe, there is no problem. But when I plot a slice of the same dataset (keeping the index sorted), the values are disturbed. The problematic values are not necessarily the ones of the column used for coloring.

    To reproduce

    df = pd.DataFrame({
        'id': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
        'value': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
    })
    
    df2 = df[df["id"] % 2 == 0]
    
    # With color
    px.parallel_coordinates(df2, color="value")

    image

    #Without color
    px.parallel_coordinates(df2)

    image

  3. self-assigned this
    on Jul 11, 2024
  4. removed their assignment
    on Aug 2, 2024
  5. added
    P3
    bug
    on Aug 12, 2024
  6. MarcoGorelli commented on Dec 1, 2024

    @MarcoGorelli
    Contributor

    hi @shane-kercheval

    is this your expected output?

    Image

  7. shane-kercheval commented on Dec 2, 2024

    @shane-kercheval
    Author

    Yes, thank you!

  8. MarcoGorelli commented on Dec 2, 2024

    @MarcoGorelli
    Contributor

    Awesome! You should already be able to get the fix by using the 6.0 pre-release with the latest Narwhals version

    pip install -U --pre plotly narwhals>=1.15.1
    

    I'd be amazing if you could try it out and report any issues you encounter so they can be fixed before the final 6.0 release 🙏 ⭐

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions