-
Notifications
You must be signed in to change notification settings - Fork 65
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
Improve plot clarity with combined mean and HDI legend elements #145
Conversation
Check out this pull request on See visual diffs & provide feedback on Jupyter Notebooks. Powered by ReviewNB |
|
I originally tried to do this on a better way, with everything handled internally in the plot function. But I ran into problems. It may be that I discovered a bug in matplotlib. See the solution here https://stackoverflow.com/a/74935897 If this can be made to work, it would be much cleaner. |
causalpy/plot_utils.py
Outdated
include_label: bool = True, | ||
) -> None: | ||
): |
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.
type hints are missing
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.
updated in an upcoming commit
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 added some small suggestions. Mostly regarded commented code. This is very hard to maintain and we always have the git history to come back to them anyway. In addtion, what if we put the legends outside the figure as described in https://stackoverflow.com/questions/4700614/how-to-put-the-legend-outside-the-plot ?
causalpy/plot_utils.py
Outdated
@@ -13,28 +14,46 @@ def plot_xY( | |||
ax: plt.Axes, | |||
plot_hdi_kwargs: Optional[Dict[str, Any]] = None, | |||
hdi_prob: float = 0.94, | |||
label: Optional[str] = "", |
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.
Remember Optional[str]
is equivalent to Union[str, None
. I think we probably do not want this so we should remove Optional
. See #117
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.
Changed to Union[str, None]
. This also allowed me to remove a kwarg
causalpy/plot_utils.py
Outdated
x, | ||
Y, | ||
hdi_prob=hdi_prob, | ||
fill_kwargs={ | ||
"alpha": 0.25, | ||
"label": f"{hdi_prob*100}% HDI" if include_label else None, | ||
"label": " ", # f"{hdi_prob*100}% HDI" if include_label else None, |
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.
shall we start removing commented code? this makes the code hard to maintain.
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.
Fixed in an upcoming commit
causalpy/plot_utils.py
Outdated
# if include_label: | ||
# handles, labels = ax.get_legend_handles_labels() | ||
# ax.legend( | ||
# handles=[(h1, h2) for h1, h2 in zip(handles[::2], handles[1::2])], | ||
# # labels=[l1 + " + " + l2 for l1, l2 in zip(labels[::2], labels[1::2])], | ||
# labels=[l1 for l1 in labels[::2]], | ||
# ) |
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.
same here regarding commented code
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.
Fixed in an upcoming commit
causalpy/pymc_experiments.py
Outdated
alpha=0.25, | ||
label="Causal impact", | ||
# label="Causal impact", |
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.
remove this duplicated line
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.
Fixed in an upcoming commit
causalpy/pymc_experiments.py
Outdated
@@ -189,10 +209,14 @@ def plot(self): | |||
ls="-", | |||
lw=3, | |||
color="r", | |||
label="Treatment time", | |||
# label="Treatment time", |
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.
remove commented line?
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.
Fixed in an upcoming commit
Thanks for the comments @juanitorduz. I believe I've dealt with all those changes except for an external axis legend. I agree that it's suboptimal for the synthetic control plots, but I'll improve it in a separate PR. I also re-ran some notebooks because I was not fond of the green shaded region for the causal impact. |
causalpy/pymc_experiments.py
Outdated
@@ -137,7 +136,6 @@ def plot(self): | |||
self.datapost.index, | |||
self.post_pred["posterior_predictive"].mu, | |||
ax=ax[0], | |||
include_label=False, | |||
# label="Synthetic control", |
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.
shall we remove this line?
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.
will do
Just left a comment regarding a commented line: either remove or uncomment ;) Feel free to merge. |
Closes #142 and #143.
ax.legend
once at the end of the plot functions. See my StackOverflow question and post on Twitter.Examples for synthetic control plots...
Before
After
Note the combined line+shaded region in the legend
Examples for difference in differences
Before
After