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.

Non-deterministic behavior with `mark_boxplot`

See original GitHub issue

The following code will not order the categories on the x-axis the same way on repeated runs:

import pandas as pd
import numpy as np
import altair as alt

d = pd.DataFrame({
    'x': np.repeat(np.arange(3), 10),
    'y': np.concatenate([np.random.normal(i, 0.1, 10) for i in range(3)]),
    'z': np.repeat(np.arange(3, 0, -1), 10),
}).sort_values(
    'z',
)

alt.Chart(
    d,
).mark_boxplot(
    opacity=1.0,
).encode(
    x=alt.X('x:N', sort=None),
    y='y:Q',
    color='z:N',
)

This does not seem to happen with mark_circle.

My understanding was that using sort=None with alt.X would respect the order in the data (i.e., z).

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:22 (11 by maintainers)

github_iconTop GitHub Comments

1reaction
jakevdpcommented, Jul 14, 2019

I just switched to my mac and tried on safari, and I can see the behavior you reported (it works fine on Chrome and Firefox). It’s not an Altair issue, but rather a Vega-Lite issue (You can see it here in the vega editor).

I spent a while trying to find Safari’s developer tools to attempt to diagnose the issue, but gave up because it’s Saturday night 😄

I would report this issue on the Vega-Lite issue tracker.

1reaction
jakevdpcommented, Jul 14, 2019

As to other question about identifying a plotting library that shows plotted boxes placed mathematically instead of categorically in a box-plot, if you would like to force bins without data to be part of the x scale, in Altair you can use the scale domain argument:

subsample = source.query("age!= 25")
alt.Chart(subsample).mark_boxplot(extent='min-max').encode(
    x=alt.X('age:O', scale=alt.Scale(domain=np.arange(0, 95, 5).tolist())),
    y='people:Q'
)

visualization - 2019-07-13T192517 182

Read more comments on GitHub >

github_iconTop Results From Across the Web

No results found

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