Skip to content

Conversation

@TeemuSailynoja
Copy link
Contributor

@TeemuSailynoja TeemuSailynoja commented Sep 30, 2025

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

  • Closes #
  • Related to #

Checklist


📚 Documentation preview 📚: https://pymc-marketing--1967.org.readthedocs.build/en/1967/

@github-actions github-actions bot added the MMM label Sep 30, 2025
@codecov
Copy link

codecov bot commented Sep 30, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 92.89%. Comparing base (aafd5ed) to head (10dd924).
⚠️ Report is 1 commits behind head on main.

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@TeemuSailynoja
Copy link
Contributor Author

still needs tests to go through the combinations of negative positive and ensure that the axis limits are correct after the call.

Comment on lines 248 to 274
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])
Copy link
Contributor

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

Copy link
Contributor Author

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.

Copy link
Contributor Author

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.

@TeemuSailynoja TeemuSailynoja marked this pull request as draft September 30, 2025 21:33
@github-actions github-actions bot added the tests label Oct 20, 2025
@review-notebook-app
Copy link

Check out this pull request on  ReviewNB

See visual diffs & provide feedback on Jupyter Notebooks.


Powered by ReviewNB

@github-actions github-actions bot added docs Improvements or additions to documentation CLV customer choice Related to customer choice module labels Oct 20, 2025
@TeemuSailynoja TeemuSailynoja force-pushed the TeemuSailynoja/plot-budget-allocation-align-y-axes branch from 2a16e3d to 772756f Compare October 20, 2025 21:09
@TeemuSailynoja TeemuSailynoja marked this pull request as ready for review October 20, 2025 21:20
@TeemuSailynoja TeemuSailynoja added plots and removed docs Improvements or additions to documentation CLV customer choice Related to customer choice module labels Oct 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants