-
Couldn't load subscription status.
- Fork 326
Align y-axes in mmm.plot.budget_allocation #1967
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
base: main
Are you sure you want to change the base?
Align y-axes in mmm.plot.budget_allocation #1967
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1967 +/- ##
==========================================
+ Coverage 92.87% 92.89% +0.02%
==========================================
Files 68 68
Lines 9201 9227 +26
==========================================
+ Hits 8545 8571 +26
Misses 656 656 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
still needs tests to go through the combinations of negative positive and ensure that the axis limits are correct after the call. |
pymc_marketing/mmm/plot.py
Outdated
| if ax.axes.get_ylim()[0] < 0 or ax2.axes.get_ylim()[0] < 0: | ||
| ylims1 = ax.axes.get_ylim() | ||
| ylims2 = ax2.axes.get_ylim() | ||
| # Find the ratio of negative vs. positive part of the axes. | ||
| if ylims1[1]: | ||
| ax1_yratio = ylims1[0] / ylims1[1] | ||
| else: | ||
| # Fully negative axis. | ||
| ax1_yratio = -1 | ||
|
|
||
| if ylims2[1]: | ||
| ax2_yratio = ylims2[0] / ylims2[1] | ||
| else: | ||
| # Fully negative axis, may need to reflect the other | ||
| ax2_yratio = -1 | ||
|
|
||
| # Make axis adjustments. If both axes fully negative, no adjustment. | ||
| if ax1_yratio < ax2_yratio: | ||
| ax2.set_ylim(bottom=ylims2[1] * ax1_yratio) | ||
| if ax1_yratio == -1: | ||
| # if the axis is fully negative, center zero. | ||
| ax.set_ylim(top=-ylims1[0]) | ||
| elif ax2_yratio < ax1_yratio: | ||
| ax.set_ylim(bottom=ylims1[1] * ax2_yratio) | ||
| if ax2_yratio == -1: | ||
| # if the axis is fully negative, center zero. | ||
| ax2.set_ylim(top=-ylims2[0]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you use NamedTuple or something. Quite confusing to read
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking at this now, I fully agree. I also think that this is not capturing every possible combination the axes can get odd. I'll move this to a draft and rework it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I completely rewrote the function, and added tests that go through the possible cases.
Not the most compact solution, but I hope it exposes its logic better.
|
Check out this pull request on See visual diffs & provide feedback on Jupyter Notebooks. Powered by ReviewNB |
2a16e3d to
772756f
Compare
…Sailynoja/plot-budget-allocation-align-y-axes
Description
Ensure that y=0 is aligned between the primary and secondary y-axis in
mmm.plot.budget_allocation.Solution
If one axis includes negative, values the other is extended down so that the y=0 is aligned.
If all contributions would be fully negative (I don't know when this would happen...), the y=0 level is centered vertically on both axes.
Related Issue
Checklist
pre-commit.ci autofixto auto-fix.📚 Documentation preview 📚: https://pymc-marketing--1967.org.readthedocs.build/en/1967/