Skip to content
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

fix: properly treat Bar.base with stacked Bars #1423

Merged
merged 2 commits into from
Oct 4, 2023

Conversation

maartenbreddels
Copy link
Member

This also allows the use of bars with log scales

bug

We currently subtract base (baseValue in js code) each time from the value, which caused stacked bars to not show correct values. See demonstration of the bug:
bqplot-bars-base-bug

Code to reproduce:

import bqplot
import numpy as np
import bqplot.pyplot as plt
N = 1000
np.random.seed(42)
log = False
sx = bqplot.LinearScale(min=0, max=4)
sy = bqplot.LinearScale(min=-2, max=6)
if log:
    sy = bqplot.LogScale(min=0.1, max=10)

fig = plt.figure(scale_x=sx, scale_y=sy)
x, y, c = np.random.normal(size=(3, N))
# mark = plt.scatter(x, y**2)
# h = plt.bar(x, y**2)
if log:
    mark = plt.bar([1,2,3], [[1, 1, 1], [2, 3, 4]], base=0.1, color=["orange", "green"], scales={'x': sx, 'y': sy})
else:
    mark = plt.bar([1,2,3], [[1, -1, 1], [2, -3, 4]], base=0, color=["orange", "green"], scales={'x': sx, 'y': sy})
plt.show()
import ipywidgets as widgets
if log:
    slider = widgets.FloatLogSlider(min=-1, max=1.5, step=0.01)
else:
    slider = widgets.FloatSlider(min=-1, max=7)
    
widgets.jslink((mark, 'base'), (slider, 'value'))
slider

solution

I've refactored the code a bit, which makes it easier to comprehend I think, and corrected the double subtraction.

Bars now behave as I expect:
bqplot-bars-base

There are a few issues/cornercase:

  • If base is larger then the first bar value, there is no proper way to display it, so we skip it and print a warning to the console.
  • Using a log scale, negative bars (i.e. where base is larger then the cumulate sum of y) makes totally no sense, but one could argue that's the user error.

@maartenbreddels maartenbreddels changed the title fix: property treat Bar.base with stacked Bars fix: properly treat Bar.base with stacked Bars Dec 7, 2021
This also allows the use of bars with log scales
Copy link
Member

@martinRenou martinRenou left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the top of the bars actually be equal to base + y?

IIUC, that is what plotly does:
base

This would fix your issue with stacked bars, because bars would always have the same height, whatever the base is.

@maartenbreddels
Copy link
Member Author

I think the current behaviour makes sense, given that if you plot in log space, and base is 10, and y value is 100, you want a bar from 10 to 100, not till 110

@ibdafna
Copy link
Member

ibdafna commented Feb 3, 2022

@maartenbreddels Are you still looking to pursue this? Looks not not based on the last comment, but I thought I'd ask 😃

@maartenbreddels
Copy link
Member Author

Yeah, I think this is ready for review/merge. I just expressed I disagreed with Martin. Also, as it is now, the behaviour is the same as before, so non-breaking.

@maartenbreddels
Copy link
Member Author

@mariobuikhuizen and I looked at this, we think this is an improvement, replaces #1375 and fixes #1300

Good to merge @martinRenou ?

@maartenbreddels
Copy link
Member Author

It fixes a 'subset' of #1300, which is LogScale with non-negative values and a non-negative base (which the user has to set).

Copy link
Member

@martinRenou martinRenou left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@martinRenou martinRenou merged commit c186fcd into bqplot:master Oct 4, 2023
@martinRenou martinRenou deleted the fix_bars_base branch October 4, 2023 07:40
@maartenbreddels
Copy link
Member Author

Thank you @martinRenou !

meeseeksdev backport to 0.12.x ?

@lumberbot-app
Copy link

lumberbot-app bot commented Oct 10, 2023

Something went wrong ... Please have a look at my logs.

It seems that the branch you are trying to backport to does not exist.

maartenbreddels added a commit to maartenbreddels/bqplot that referenced this pull request Oct 10, 2023
* fix: property treat Bar.base with stacked Bars

This also allows the use of bars with log scales

* treat positive and negative bins separately
maartenbreddels added a commit to maartenbreddels/bqplot that referenced this pull request Oct 10, 2023
* fix: property treat Bar.base with stacked Bars

This also allows the use of bars with log scales

* treat positive and negative bins separately
martinRenou pushed a commit that referenced this pull request Oct 10, 2023
…1631)

* fix: properly treat Bar.base with stacked Bars (#1423)

* fix: property treat Bar.base with stacked Bars

This also allows the use of bars with log scales

* treat positive and negative bins separately

* chore: typos found by codespell

* chore: flake8 lint errors for type checking
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

Successfully merging this pull request may close these issues.

None yet

3 participants